You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.0 KiB
C++

/*
* 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 <IRremote.h>
#include <Wire.h>
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!");
}
}