This repository has been archived on 2024-01-30. You can view files and clone it, but cannot push or open issues or pull requests.
trev/arduino/src/sketch.ino

114 lines
2.3 KiB
Arduino
Raw Normal View History

2016-01-12 20:05:43 +01:00
#include <RCSwitch.h>
2016-01-18 22:30:36 +01:00
uint8_t message[8];
int index = 0;
boolean com = false;
2016-01-11 21:09:53 +01:00
void setup()
{
2016-01-12 20:05:43 +01:00
Serial.begin(9600);
2016-01-11 21:09:53 +01:00
}
void loop()
{
2016-01-18 22:30:36 +01:00
while(Serial.available()) {
2016-01-18 23:17:34 +01:00
uint8_t x = Serial.read();
if(index > 0 || (x >= 80 && x <= 95)) {
message[index++] = x;
2016-01-18 22:30:36 +01:00
if(index > 7){
decodeMessage();
index = 0;
}
2016-01-12 20:05:43 +01:00
}
}
}
2016-01-18 22:30:36 +01:00
void decodeMessage() {
2016-01-20 16:46:11 +01:00
int cmd = message[0];
int pin = (message[1] - 97 < 0 || message[1] - 97 > 19)? -1 : message[1] - 97 < 0;
2016-01-18 22:30:36 +01:00
if(com) {
2016-01-18 23:17:34 +01:00
uint8_t val[6];
2016-01-18 22:30:36 +01:00
memcpy(val, message + 2, 6);
switch (cmd) {
//case 80: break;
2016-01-20 16:46:11 +01:00
case 81: setMode(val[0], pin); break;
2016-01-20 17:00:46 +01:00
case 82: digitalW(val[0], pin) break;
2016-01-18 22:30:36 +01:00
//case 83: break;
//case 84: break;
//case 85: break;
//case 86: break;
2016-01-20 16:46:11 +01:00
case 87: sendRCTristate(val, pin); break;
2016-01-18 22:30:36 +01:00
//case 88: break;
//case 89: break;
default: break;
}
2016-01-20 16:46:11 +01:00
} else if(cmd] == 90) {
2016-01-18 22:30:36 +01:00
com = true;
2016-01-20 16:46:11 +01:00
setMode(0, pin);
digitalWrite(pin, HIGH);
2016-01-18 22:30:36 +01:00
Serial.println("Ready!");
} else {
Serial.println("Arduino not configured for communication!");
}
}
2016-01-20 16:46:11 +01:00
void setMode(uint8_t val, int pin) {
if(pin == -1) { if(debug) Serial.println("badpin"); return; }
if (val == 0) {
pinMode(pin, OUTPUT);
} else {
pinMode(pin, INPUT);
}
}
2016-01-20 17:00:46 +01:00
void digitalW(uint8_t val, int pin) {
if(pin == -1) { if(debug) Serial.println("badpin"); return; }
pinMode(pin, OUTPUT);
if(val == 0) {
digitalWrite(pin, LOW);
} else {
digitalWrite(pin, HIGH);
}
}
2016-01-20 16:46:11 +01:00
void sendRCTristate (uint8_t val[], int pin) {
if(pin == -1) { if(debug) Serial.println("badpin"); return; }
2016-01-18 22:30:36 +01:00
String triStateCode = "";
2016-01-20 16:46:11 +01:00
RCSwitch rc = RCSwitch();
mySwitch.enableTransmit(pin);
2016-01-18 22:30:36 +01:00
for(int i = 0; i < 6; i++)
{
String triStatePart = String(val[i], HEX);
triStatePart = (triStatePart.length() < 2) ? String("0" + triStatePart) : triStatePart;
triStateCode.concat(triStatePart);
}
2016-01-18 23:17:34 +01:00
char triState[triStateCode.length() + 1];
triStateCode.toUpperCase();
triStateCode.toCharArray(triState, triStateCode.length() + 1);
2016-01-18 22:30:36 +01:00
2016-01-18 23:17:34 +01:00
controlRCOutlets(triState);
Serial.println("RC-Tristate send!");
2016-01-18 22:30:36 +01:00
}
2016-01-12 20:05:43 +01:00
void controlRCOutlets(const char* sCodeWord) {
mySwitch.sendTriState(sCodeWord);
delay(1000);
2016-01-13 09:55:37 +01:00
}