Create a gist now

Instantly share code, notes, and snippets.

@netmaniac /irpower.ino Secret
Created Sep 22, 2015

#include "Storage.h"
TinyDebugSerial mySerial = TinyDebugSerial();
bool power = false;
#define IRpin_PIN PINB
#define IRpin 1
#define STATUS_LED 2
#define SETUP_BUTTON 8
#define POWER_INFO 9
#define POWER_PIN 10
#define POWER_OFF_DELAY 45*1000
#define RESOLUTION 45
#define MAXPULSE 95000/RESOLUTION
#define ARR_SIZE 80
uint16_t pulses[ARR_SIZE][2]; // pair is high and low pulse
uint8_t savedSize = 0;
uint8_t currentpulse = 0; // index for pulses we're storing
boolean saveArray = false;
uint8_t readFromEEPROM(void) {
uint16_t ret = 0;
mySerial.print(F("In EEPROM "));
storageRead(0, savedSize);
mySerial.println(savedSize, DEC);
}
void cellFromEEPROM(uint8_t row, uint8_t col, uint16_t &ret) {
if (row >= savedSize) {
ret = -1;
return;
}
storageRead(1 + (2 * row + col)*sizeof(uint16_t), ret);
}
void setup(void) {
mySerial.begin(9600);
readFromEEPROM();
printpulses();
mySerial.println(F("Boot!"));
// DDRB = B11111101;
cli();
pinMode(SETUP_BUTTON, INPUT_PULLUP);
pinMode(STATUS_LED, OUTPUT);
pinMode(POWER_PIN, OUTPUT);
digitalWrite(POWER_PIN, LOW);
power = false;
pinMode(POWER_INFO,OUTPUT);
digitalWrite(POWER_INFO,HIGH);
if (digitalRead(SETUP_BUTTON) == LOW) {
mySerial.println(F("Store new signal"));
storeNewSignal();
}
mySerial.println(F("LISTENING..."));
}
void storeNewSignal() {
saveArray = true;
digitalWrite(STATUS_LED, HIGH);
while (1) {
readPulses();
}
}
void save2EEPROM(uint8_t &tabSize) {
if (!saveArray) return ;
storageWrite(0, tabSize);
for (int i = 0; i < tabSize; i++) {
storageWrite(1 + 2 * i * sizeof(uint16_t), pulses[i][0]);
storageWrite(1 + (2 * i + 1)*sizeof(uint16_t), pulses[i][1]);
}
}
uint8_t isEqual(uint8_t arrSize) {
if (arrSize < savedSize) return ARR_SIZE + 1;
for (int i = 0; i < arrSize - 1; i++) {
uint16_t saved;
cellFromEEPROM(i, 1, saved);
int d1 = pulses[i][1] - saved;
int err1 = pulses[i][1] * 20 / 100;
cellFromEEPROM(i + 1, 0, saved);
int d2 = pulses[i + 1][0] - saved;
int err2 = pulses[i + 1][0] * 20 / 100;
if (abs(d1) > err1 || abs(d2) > err2)
return i + 1;
}
return 0;
}
void pulseLed(uint16_t tme) {
digitalWrite(STATUS_LED, HIGH);
delay(tme);
digitalWrite(STATUS_LED, LOW);
}
void loop(void) {
readPulses();
}
void readPulses(void) {
uint16_t highpulse, lowpulse; // temporary storage timing
highpulse = lowpulse = 0; // start out with no pulse length
while (IRpin_PIN & (1 << IRpin)) {
// pin is still HIGH
// count off another few microseconds
highpulse++;
delayMicroseconds(RESOLUTION);
if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
printpulses();
mySerial.print("Pulses# ");
mySerial.println(currentpulse + 1);
save2EEPROM(currentpulse);
currentpulse = 0;
return;
}
}
// we didn't time out so lets stash the reading
pulses[currentpulse][0] = highpulse;
while (! (IRpin_PIN & _BV(IRpin))) {
// pin is still LOW
lowpulse++;
delayMicroseconds(RESOLUTION);
if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
printpulses();
mySerial.print("Pulses# ");
mySerial.println(currentpulse + 1);
save2EEPROM(currentpulse);
currentpulse = 0;
return;
}
}
pulses[currentpulse][1] = lowpulse;
// we read one high-low pulse successfully, continue!
currentpulse++;
if (currentpulse >= ARR_SIZE) {
printpulses();
currentpulse = 0;
}
}
void flipPower() {
mySerial.println("KLIK!!!");
if (power) {
power = false;
digitalWrite(POWER_INFO, LOW);
mySerial.println(F("DELAY..."));
delay(POWER_OFF_DELAY);
mySerial.println(F("After DELAY..."));
digitalWrite(POWER_INFO, HIGH);
digitalWrite(POWER_PIN,LOW);
} else {
power = true;
digitalWrite(POWER_PIN,HIGH);
}
}
void printpulses(void) {
/*mySerial.println("\n\r\n\rReceived: \n\rOFF \tON");
for (uint8_t i = 0; i < currentpulse; i++) {
mySerial.print(pulses[i][0] * RESOLUTION, DEC);
mySerial.print(" usec, ");
mySerial.print(pulses[i][1] * RESOLUTION, DEC);
mySerial.println(" usec");
}
*/
// print it in a 'array' format
/*
mySerial.println(F("int IRsignal[] = {)"));
//mySerial.println("// ON, OFF (in 10's of microseconds)");
uint16_t saved;
for (uint8_t i = 0; i < currentpulse - 1; i++) {
mySerial.print(i + 1, DEC);
mySerial.print(F("\t")); // tab
mySerial.print(pulses[i][1], DEC);
mySerial.print(F(", "));
mySerial.print(pulses[i + 1][0] , DEC);
mySerial.print(F(","));
mySerial.print(F("\t")); // tab
cellFromEEPROM(i, 1, saved);
mySerial.print(saved , DEC);
mySerial.print(F(", "));
cellFromEEPROM(i+1, 0, saved);
mySerial.print(saved , DEC);
mySerial.println(F(","));
}
mySerial.print(F("\t")); // tab
mySerial.print(pulses[currentpulse - 1][1] , DEC);
mySerial.print(F(", 0};"));
*/
mySerial.print(F("DIFF: "));
uint8_t diff = isEqual(currentpulse);
if (diff) {
mySerial.print(F("Diff at pos:"));
mySerial.println(diff, DEC);
} else {
flipPower();
}
// delay(400);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment