2 Firmware for wireless S88 transmitter module
20 Inputs are pulled high by internal pull-up resistors. Connect to GND to
23 The module can be configured by sending a string of form ":Shhh." over the
24 serial port, where hhh is a hex number. Lower two bits indicate the number of
25 inputs (00=4, 01=8, 10=12, 11=16) and upper ten bits indicate offset of lowest
26 input in multiples of 4 bits. At the moment there are no provisions for
27 having 16 physical inputs.
29 Example: ":S016." would configure the module to have 12 inputs at offset 20.
33 #include <avr/interrupt.h>
38 #define BIT(n) (1<<(n))
40 void receive(uint8_t);
41 void send_state(void);
42 uint8_t hexdigit(uint8_t);
43 uint8_t decode_hex(uint8_t);
46 uint8_t rx_fill = 0xFF;
47 volatile uint8_t nibbles = 2;
48 volatile uint8_t offset = 0;
49 volatile uint16_t state = 0;
50 volatile uint8_t time_since_send = 0;
54 if(eeprom_read(0)==0xA5)
56 offset = eeprom_read(1)<<8;
57 offset |= eeprom_read(2);
58 nibbles = eeprom_read(3);
61 DDRD = 0x02; // 00000010
62 PORTD = 0xFC; // 11111100
63 DDRB = 0x00; // 00000000
64 PORTB = 0x3F; // 00111111
67 timer_start_hz(1, 100, 1);
75 uint16_t valid = 0xFFF;
81 pins |= ~((PIND>>2) | ((PINB&0x3F)<<6));
86 valid &= ~(pins^input);
90 input |= state&~valid;
91 input &= (1<<(nibbles*4))-1;
93 if(input!=state && time_since_send>5)
103 void receive(uint8_t c)
112 if(rx_buf[0]=='S' && rx_fill==4)
114 offset = (decode_hex(rx_buf[1])<<8) | (decode_hex(rx_buf[2])<<4) | decode_hex(rx_buf[3]);
115 nibbles = (offset&3)+1;
118 eeprom_write(0, 0xA5);
119 eeprom_write(1, offset>>8);
120 eeprom_write(2, offset);
121 eeprom_write(3, nibbles);
127 if(rx_fill<sizeof(rx_buf))
128 rx_buf[rx_fill++] = c;
134 SERIAL_SET_CALLBACK(receive)
136 void send_state(void)
141 serial_write(hexdigit(offset>>8));
142 serial_write(hexdigit(offset>>4));
143 serial_write(hexdigit(offset|(nibbles-1)));
145 serial_write(hexdigit(state>>(i*4)));
154 if(time_since_send>200)
158 TIMER_SET_CALLBACK(1, timer)
160 uint8_t hexdigit(uint8_t n)
169 uint8_t decode_hex(uint8_t c)
173 else if(c>='A' && c<='F')