diff --git a/power.ino b/power.ino new file mode 100644 index 0000000..5152455 --- /dev/null +++ b/power.ino @@ -0,0 +1,87 @@ +/* + * Power: Activate a relay to simulate pressing the power button of a PC + * A Raspberry Pi or clone must be attached to I2C. + * TODO: add IR support + */ + +#include +#include + +const int RECV_PIN = 11; +const int ledPin = 13; +int incomingByte = 0; + +IRrecv irrecv(RECV_PIN); + +decode_results results; + +void setup() +{ + Serial.begin(9600); + // In case the interrupt driver crashes on setup, give a clue + // to the user what's going on. + Serial.println("Enabling IRin"); + irrecv.enableIRIn(); // Start the receiver + Serial.println("Enabled IRin"); + pinMode(12, OUTPUT); + digitalWrite(12, LOW); + + pinMode(7, INPUT); + + // i2c code + Wire.begin(0x8); // join i2c bus with address #8 + Wire.onReceive(receiveEvent); // register event + pinMode(ledPin, OUTPUT); + digitalWrite(ledPin, LOW); // turn it off +} + +void loop() { + if (irrecv.decode(&results)) { + Serial.println(results.value); + if(results.value == 16580863) { + pressTheButton(); + } + irrecv.resume(); // Receive the next value + } + delay(100); + if(digitalRead(7)) { // In case the user presses the original power button + pressTheButton(); + delay(1000); + } + if (Serial.available() > 0) { + // read the incoming byte: + incomingByte = Serial.read(); + if(incomingByte == 120) { + pressTheButton(); + } + } + //pressTheButton(); +} + + + +void receiveEvent(int howMany) { + Serial.println("event"); + while (Wire.available()) { // loop through all but the last + int c = Wire.read(); // receive byte as a character + if(c == 120) { + pressTheButton(); + } + + } + +} + +void pressTheButton() { + if(millis() > 5000) { // Avoid any bootup false inputs + digitalWrite(12, HIGH); // toggle relay for power button for 0.5 sec + digitalWrite(ledPin, HIGH); + sei(); //Enables interrupts + delay(500); //Wait 1 second + //cli(); + //delay(500); + digitalWrite(12, LOW); + digitalWrite(ledPin, LOW); + Serial.println("button pressed!"); + } +}