int8_t set_handler(uint8_t *buf) { /* number of valid bytes in buf, -1 signifies error */ int8_t ret = 3; uint8_t idx; uint8_t tmp[SIZEOF_IR]; switch ((enum command) buf[2]) { case CMD_EMIT: delay_ms(130); irsnd_send_data((IRMP_DATA *) &buf[3], 1); break; case CMD_ALARM: memcpy(&AlarmValue, &buf[3], sizeof(AlarmValue)); break; case CMD_MACRO: idx = (MACRO_DEPTH + 1) * SIZEOF_IR/2 * buf[3] + SIZEOF_IR/2 * buf[4]; eeprom_store(idx, &buf[5]); /* validate stored value in eeprom */ eeprom_restore(tmp, idx); if (memcmp(&buf[5], tmp, sizeof(tmp))) ret = -1; break; case CMD_WAKE: idx = (MACRO_DEPTH + 1) * SIZEOF_IR/2 * MACRO_SLOTS + SIZEOF_IR/2 * buf[3]; eeprom_store(idx, &buf[4]); /* validate stored value in eeprom */ eeprom_restore(tmp, idx); if (memcmp(&buf[4], tmp, sizeof(tmp))) ret = -1; break; default: ret = -1; } return ret; }
static void save(void) { uint16_t s; s = advance_polarity() << 11; if (clock_state.state & (CLOCK_RUNNING | CLOCK_PENDING)) { s |= clock_state.clock; } else { s |= 0x07ff; } if (clock_state.stored != s) { clock_state.stored = s; eeprom_store(clock_state.stored); } }
void store_wakeup(IRMP_DATA *ir) { uint8_t idx; uint8_t tmp[SIZEOF_IR]; uint8_t zeros[SIZEOF_IR] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; idx = (MACRO_DEPTH + 1) * SIZEOF_IR/2 * MACRO_SLOTS; eeprom_restore(tmp, idx); if (!memcmp(tmp, zeros, SIZEOF_IR)) { /* store received wakeup IRData in first wakeup slot */ eeprom_store(idx, (uint8_t *) ir); fast_toggle(); } }
int8_t reset_handler(uint8_t *buf) { /* number of valid bytes in buf, -1 signifies error */ int8_t ret = 3; uint8_t idx; uint8_t zeros[SIZEOF_IR] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; switch ((enum command) buf[2]) { case CMD_ALARM: AlarmValue = 0xFFFFFFFF; break; case CMD_MACRO: idx = (MACRO_DEPTH + 1) * SIZEOF_IR/2 * buf[3] + SIZEOF_IR/2 * buf[4]; eeprom_store(idx, zeros); break; case CMD_WAKE: idx = (MACRO_DEPTH + 1) * SIZEOF_IR/2 * MACRO_SLOTS + SIZEOF_IR/2 * buf[3]; eeprom_store(idx, zeros); break; default: ret = -1; } return ret; }
void store_new_wakeup(void) { uint8_t idx; IRMP_DATA wakeup_IRData; toggle_LED(); /* 5 seconds to press button on remote */ delay_ms(5000); if (irmp_get_data(&wakeup_IRData)) { wakeup_IRData.flags = 0; idx = (MACRO_DEPTH + 1) * SIZEOF_IR/2 * MACRO_SLOTS; /* store received wakeup IRData in first wakeup slot */ eeprom_store(idx, (uint8_t *) &wakeup_IRData); toggle_LED(); } }
int main(void) { uint8_t buf[HID_OUT_BUFFER_SIZE-1], RepeatCounter = 0; IRMP_DATA myIRData; int8_t ret; /* first wakeup slot empty? */ uint8_t learn_wakeup = eeprom_restore(buf, (MACRO_DEPTH + 1) * SIZEOF_IR/2 * MACRO_SLOTS); USB_HID_Init(); LED_Switch_init(); IRMP_Init(); irsnd_init(); FLASH_Unlock(); EE_Init(); Systick_Init(); while (1) { if (!AlarmValue) Wakeup(); wakeup_reset(); /* test if USB is connected to PC and command is received */ if (USB_HID_GetStatus() == USB_HID_CONNECTED && USB_HID_ReceiveData(buf) == RX_READY && buf[0] == STAT_CMD) { switch ((enum access) buf[1]) { case ACC_GET: ret = get_handler(buf); break; case ACC_SET: ret = set_handler(buf); break; case ACC_RESET: ret = reset_handler(buf); break; default: ret = -1; } if (ret == -1) { buf[0] = STAT_FAILURE; ret = 3; } else { buf[0] = STAT_SUCCESS; } /* send configuration data */ USB_HID_SendData(REPORT_ID_CONFIG, buf, ret); toggle_LED(); } /* poll IR-data */ if (irmp_get_data(&myIRData)) { if (learn_wakeup) { /* store received wakeup IRData in first wakeup slot */ eeprom_store((MACRO_DEPTH + 1) * SIZEOF_IR/2 * MACRO_SLOTS, (uint8_t *) &myIRData); learn_wakeup = 0; } if (!(myIRData.flags)) { RepeatCounter = 0; } else { RepeatCounter++; } if (RepeatCounter == 0 || RepeatCounter >= MIN_REPEATS) { toggle_LED(); /* if macros are sent already, while the trigger IR data are still repeated, * the receiving device may crash */ check_macros(&myIRData); check_wakeups(&myIRData); } /* send IR-data */ memcpy(buf, &myIRData, sizeof(myIRData)); USB_HID_SendData(REPORT_ID_IR, buf, sizeof(myIRData)); } } }