-/* $Id$
-
-This file is part of the MSP Märklin suite
-Copyright © 2010 Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-
+/*
Firmware for wireless S88 transmitter module
ATMega pinout:
D7 - input 6
B0 - input 7
B1 - input 8
+B2 - input 9
+B3 - input 10
+B4 - input 11
+B5 - input 12
Inputs are pulled high by internal pull-up resistors. Connect to GND to
activate.
+
+The module can be configured by sending a string of form ":Shhh." over the
+serial port, where hhh is a hex number. Lower two bits indicate the number of
+inputs (00=4, 01=8, 10=12, 11=16) and upper ten bits indicate offset of lowest
+input in multiples of 4 bits. At the moment there are no provisions for
+having 16 physical inputs.
+
+Example: ":S016." would configure the module to have 12 inputs at offset 20.
*/
#include <avr/io.h>
volatile uint8_t nibbles = 2;
volatile uint8_t offset = 0;
volatile uint16_t state = 0;
+volatile uint8_t time_since_send = 0;
int main()
{
DDRD = 0x02; // 00000010
PORTD = 0xFC; // 11111100
- DDRB = 0x20; // 00000000
+ DDRB = 0x00; // 00000000
PORTB = 0x3F; // 00111111
serial_init(9600);
- serial_set_callback(receive);
- timer_init(1, 2);
- timer_set_callback(send_state);
+ timer_start_hz(1, 100, 1);
sei();
while(1)
{
- uint8_t i;
+ uint8_t i, j;
uint16_t input = 0;
uint16_t valid = 0xFFF;
for(i=0; i<100; ++i)
{
- uint16_t pins;
+ uint16_t pins = 0;
+ for(j=0; j<100; ++j)
+ pins |= ~((PIND>>2) | ((PINB&0x3F)<<6));
- pins = ~((PIND>>2) | ((PINB&0x3F)<<6));
if(i==0)
input = pins;
+
valid &= ~(pins^input);
}
input |= state&~valid;
input &= (1<<(nibbles*4))-1;
- if(input!=state)
+ if(input!=state && time_since_send>5)
{
state = input;
send_state();
}
}
+SERIAL_SET_CALLBACK(receive)
+
void send_state(void)
{
uint8_t i;
+
serial_write(':');
serial_write(hexdigit(offset>>8));
serial_write(hexdigit(offset>>4));
for(i=nibbles; i--;)
serial_write(hexdigit(state>>(i*4)));
serial_write('.');
+
+ time_since_send = 0;
+}
+
+void timer(void)
+{
+ ++time_since_send;
+ if(time_since_send>200)
+ send_state();
}
+TIMER_SET_CALLBACK(1, timer)
+
uint8_t hexdigit(uint8_t n)
{
n &= 0xF;