#define F_CPU 1000000 #include #include #include #include #include #include "../helpers.h" #include "../lcd.h" static volatile uint8_t address = 0; static volatile uint16_t cnt = 0; static volatile uint8_t signal[60]; // signal that is recorded the first time static uint8_t signals[] = { 36, 0, 19, 24, 28, 32, 37, 46, 50, 54, 59, 67, 72, 81, 89, 94, 103, 111, 120, 124, 129, 137, 146, 155, 159, 168, 173, 181, 190, 194, 199, 203, 212, 216, 225, 164, 175, // 0 36, 0, 20, 24, 29, 33, 37, 46, 51, 55, 59, 68, 72, 81, 90, 94, 103, 112, 121, 125, 134, 142, 151, 160, 164, 169, 173, 182, 186, 191, 195, 200, 208, 217, 226, 165, 176, // 1 36, 0, 20, 24, 29, 33, 37, 46, 50, 55, 59, 68, 72, 81, 90, 94, 103, 112, 121, 125, 134, 143, 151, 160, 164, 173, 178, 186, 191, 195, 199, 204, 213, 217, 226, 165, 176, // 2 36, 0, 20, 24, 29, 33, 37, 46, 51, 55, 59, 68, 72, 81, 90, 94, 103, 112, 121, 125, 129, 134, 142, 151, 156, 165, 169, 177, 186, 195, 199, 204, 213, 217, 226, 165, 176, // 3 }; // how many keys have you recorded uint8_t numberKeys = 4; void timer_init() { // timer TCCR0 |= (1 << CS00); TIMSK |= (1 << OCIE0); // interrupt for changing port GICR |= (1 << INT1); MCUCR |= (1 << ISC10); MCUCR |= (1 << ISC11); } void var_init() { cnt = 0; // timebase address = 0; // position in array } ISR(TIMER0_COMP_vect) { // count up a counter (timebase) // but only if a signal has appeared on the port one time if (address > 0) { cnt ++; } } ISR(INT1_vect) { // we got a signal change on the port, so save the timebase into array // and increase position in array signal[address] = cnt; address ++; } uint8_t matchSignal() { // check if signal equals one of the signals uint16_t signalSavedAddress = 0; uint16_t signalSavedLength = 0; uint8_t key = 0; // lopp all keys that are included in signals for (key = 0; key < numberKeys; key++) { // we have to increase the addresses by the number of bytes signalSavedAddress += signalSavedLength; signalSavedLength = signals[signalSavedAddress]; signalSavedAddress ++; // save here if we have found something uint8_t matched = 1; for (uint8_t i = 0; i < signalSavedLength; i++) { // compare the two values uint8_t value1 = signal[i]; uint8_t value2 = signals[signalSavedAddress + i]; // ignore a difference of +-1 if (value2 != value1 && (value2 + 1) != value1 && value2 != (value1 + 1)) { // did not match, so try the next one matched = 0; break; } } // we found a match, so return the index + 1 (because 0 is "no match found") if (matched == 1) { return key + 1; } } return 0; } int main() { // deactivate all interrupts cli(); // configure timer and interrupts timer_init(); // configure variables var_init(); // configure LCD lcd_init(); // activate interrupts sei(); while(1) { if (address > 0) { // if address is > 0 then we have received a signal _delay_ms(100); // block any other signals cli(); // filter for valid signals if (address > 5) { uint8_t result = matchSignal(); if (result > 0) { lcd_uint16(result); } } else { // no valid signal lcd_str("----"); } var_init(); // activate interrupts sei(); } } }