From 73132a39d4fc134a113eee44dde0680ceced9f27 Mon Sep 17 00:00:00 2001 From: Thammi Date: Sat, 31 Aug 2013 14:17:24 +0200 Subject: [PATCH] Add attempt to implement RC-5 sending --- firmware/apps/rctest.c | 30 +++++++++++++++++++ firmware/include/pentabug/hal.h | 5 ++++ firmware/include/pentabug/rc5.h | 13 +++++++++ firmware/lib/hal.c | 8 ++++++ firmware/lib/rc5.c | 51 +++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 firmware/apps/rctest.c create mode 100644 firmware/include/pentabug/rc5.h create mode 100644 firmware/lib/rc5.c diff --git a/firmware/apps/rctest.c b/firmware/apps/rctest.c new file mode 100644 index 0000000..6fd8b5f --- /dev/null +++ b/firmware/apps/rctest.c @@ -0,0 +1,30 @@ +#include +#include +#include + +static void run(void) { + if(button_clicked(LEFT)) { + led_on(RIGHT); + + struct rc5_data data = { 0, 12 }; + + uint8_t i; + for(i = 0; i < 32; ++i) { + /*led_inv(LEFT);*/ + + data.addr = i; + + uint8_t n; + for(n = 0; n < 128; ++n) { + data.code = n; + rc5_send(&data); + + wait_ms(120); + } + } + + led_off(RIGHT); + } +} + +REG(run); diff --git a/firmware/include/pentabug/hal.h b/firmware/include/pentabug/hal.h index 0f7b46f..87f2db9 100644 --- a/firmware/include/pentabug/hal.h +++ b/firmware/include/pentabug/hal.h @@ -9,6 +9,8 @@ #define OFF 0 #define ON 1 +#define TICKS_PER_MS 38 + // INITIALIZATION // initializes the hardware (timers, ...) @@ -68,4 +70,7 @@ uint8_t ir_recv(void); // WARNING: the time is actually measured in pentamilliseconds which are similar but not identical to regular milliseconds void wait_ms(uint16_t ms); +// wait the given amount of ticks, see TICKS_PER_MS +void wait_ticks(int16_t ticks); + #endif /* HAL_H */ diff --git a/firmware/include/pentabug/rc5.h b/firmware/include/pentabug/rc5.h new file mode 100644 index 0000000..4582f47 --- /dev/null +++ b/firmware/include/pentabug/rc5.h @@ -0,0 +1,13 @@ +#ifndef RC5_H +#define RC5_H + +#include + +struct rc5_data { + uint8_t addr; + uint8_t code; +}; + +void rc5_send(struct rc5_data *data); + +#endif /* RC5_H */ diff --git a/firmware/lib/hal.c b/firmware/lib/hal.c index aadfc6e..f379abb 100644 --- a/firmware/lib/hal.c +++ b/firmware/lib/hal.c @@ -225,6 +225,14 @@ void wait_ms(uint16_t ms) { } } +void wait_ticks(int16_t ticks) { + wait_time = ticks; + + while(wait_time > 0) { + test_stop_app(); + } +} + void ir_on(void) { ir_active = 1; } diff --git a/firmware/lib/rc5.c b/firmware/lib/rc5.c new file mode 100644 index 0000000..6bcdcbd --- /dev/null +++ b/firmware/lib/rc5.c @@ -0,0 +1,51 @@ +#include + +#include + +#define HALF_BIT_TICKS 34 + +static uint8_t toggle = 0; + +static void send_bit(uint8_t bit) { + ir_set(!bit); + wait_ticks(HALF_BIT_TICKS); + + ir_set(bit); + wait_ticks(HALF_BIT_TICKS); +} + +static void send_byte_part(uint8_t byte, uint8_t length) { + int8_t i; + + for(i = length - 1; i >= 0; --i) { + send_bit(byte & (1 << i)); + } +} + +void rc5_send(struct rc5_data *data) { + // start bit + + send_bit(1); + + // field bit + + send_bit(!(data->code & (1 << 6))); + + // control bit + + send_bit(toggle); + toggle = !toggle; + + // address + + send_byte_part(data->addr, 5); + + // command + + send_byte_part(data->code, 6); + + // done + + ir_off(); +} +