#include <IRremote.h>
#define IR_RECEIVER_PIN 6
#define OUTPUT_ENABLED_PIN 10
#define PARALEL_S0_PIN 1
#define PARALEL_S1_PIN 2
#define PARALEL_CLOCK_PIN 3
#define SERIAL_DATA_PIN 4
#define SERIAL_CLOCK_PIN 5
#define SWITCH_PIN 8
#define SWITCH_DELAY 400
#define KEYBOARD_SPY_D_MINUS_PIN 1
#define KEYBOARD_SPY_D_PLUS_PIN 2
#define DEFAULT_CHANNEL 0
#define NUMBER_OF_CHANNELS 6
int8_t channel = 0;
bool outputEnabled = false;
unsigned long timePress = 0;
IRrecv receiver(IR_RECEIVER_PIN);
void setChannel(int8_t number) {
int8_t lastChannel = NUMBER_OF_CHANNELS - 1;
if (number > lastChannel) {
channel = 0;
return;
}
if (number < 0) {
channel = lastChannel;
return;
}
channel = number;
}
void printDebugInfo() {
Serial.print("Channel: ");
Serial.println(channel);
Serial.print("Enabled: ");
Serial.println(outputEnabled ? "YES" : "NO");
}
void handleIrCommand(int16_t command) {
switch(command) {
case 162:
outputEnabled = !outputEnabled;
break;
case 226: // Menu button
printDebugInfo();
break;
case 224: // Prev button
setChannel(channel - 1);
break;
case 144: // Next button
setChannel(channel + 1);
break;
case 104: // num 0
setChannel(9);
break;
case 48: // num 1
setChannel(0);
break;
case 24: // num 2
setChannel(1);
break;
case 122: // num 3
setChannel(2);
break;
case 16: // num 4
setChannel(3);
break;
case 56: // num 5
setChannel(4);
break;
case 90: // num 6
setChannel(5);
break;
case 66: // num 7
setChannel(6);
break;
case 74: // num 8
setChannel(7);
break;
case 82: // num 9
setChannel(8);
break;
default:
Serial.println("Command not recognized!");
}
}
bool switchPressed() {
if (digitalRead(SWITCH_PIN) == HIGH) {
if (timePress == 0) {
timePress = millis();
}
return false;
}
if (timePress != 0 && (millis() - timePress) > SWITCH_DELAY) {
timePress = 0;
return true;
}
return false;
}
void setup() {
pinMode(OUTPUT_ENABLED_PIN, OUTPUT);
pinMode(SWITCH_PIN, INPUT);
Serial.begin(115200);
receiver.enableIRIn();
Serial.println("Switch KVM 1.0!");
outputEnabled = true;
setChannel(DEFAULT_CHANNEL);
}
void loop() {
if (receiver.decode()) {
Serial.print("IR command: ");
Serial.println(receiver.decodedIRData.command, HEX);
handleIrCommand(receiver.decodedIRData.command);
receiver.resume();
}
if (switchPressed()) {
Serial.println("Switch pressed!");
setChannel(channel + 1);
}
}