]> git.tdb.fi Git - model-railway-devices.git/blobdiff - firmware/s88w-r.c
Rename the project to R²C²
[model-railway-devices.git] / firmware / s88w-r.c
index f64445d81c5a33f12b8f82ef1cae2ea21c7fba5d..b537584c19ddb698fe9e44ecc4904de47a44568f 100644 (file)
@@ -1,6 +1,6 @@
 /* $Id$
 
-This file is part of the MSP Märklin suite
+This file is part of R²C²
 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 
@@ -27,12 +27,15 @@ D5 - S88 RESET
 #include <avr/interrupt.h>
 #include "lcd.h"
 #include "serial.h"
+#include "delay.h"
 
 #define DATA_OUT PORTD2
 #define CLOCK    PIND3
 #define LOAD     PIND4
 #define RESET    PIND5
 
+#define LCD_DISABLE PINB4
+
 #define BIT(n)   (1<<(n))
 
 void receive(uint8_t);
@@ -43,7 +46,9 @@ volatile uint8_t rx_buf[7];
 volatile uint8_t rx_fill = 0xFF;
 volatile uint8_t input[128] = { 0 };
 volatile uint8_t latch[128] = { 0 };
-uint8_t log_pos = 0;
+volatile uint8_t lcd_enabled = 0;
+uint8_t log_row = 0;
+uint8_t log_col = 0;
 
 int main()
 {
@@ -51,24 +56,23 @@ int main()
        uint8_t bits = 0;
        uint8_t n_bits = 8;
        uint8_t offset = 0;
+       uint8_t i;
 
-       DDRD = 0x06;  // 00000110
-       PIND = 0xC0;  // 11000000
-       DDRB = 0x20;  // 00100000
-       PINB = 0x1F;  // 00011111
+       DDRD = 0x06;   // 00000110
+       PORTD = 0xC0;  // 11000000
+       DDRB = 0x20;   // 00100000
+       PORTB = 0x1F;  // 00011111
 
        serial_init(9600);
-       serial_set_callback(receive);
        lcd_init();
-       lcd_on();
-       lcd_clear();
 
        sei();
 
        while(1)
        {
-               uint8_t d_pins;
+               uint8_t b_pins, d_pins;
 
+               b_pins = PINB;
                d_pins = PIND;
 
                if(d_pins&BIT(CLOCK))
@@ -109,6 +113,28 @@ int main()
                        for(i=0; i<128; ++i)
                                latch[i] = input[i];
                }
+
+               if(b_pins&BIT(LCD_DISABLE))
+               {
+                       if(lcd_enabled)
+                       {
+                               lcd_enabled = 0;
+
+                               lcd_clear();
+                       }
+               }
+               else if(!lcd_enabled)
+               {
+                       lcd_enabled = 1;
+                       log_row = 0;
+                       log_col = 0;
+
+                       lcd_clear();
+                       for(i=0; i<20; ++i)
+                               lcd_write(hexdigit(input[9-i/2]>>(4-i%2*4)));
+                       lcd_gotoxy(0, 1);
+                       lcd_write(255);
+               }
        }
 }
 
@@ -140,13 +166,13 @@ void receive(uint8_t c)
                                        input[j/2] = (input[j/2]&~(0xF<<shift)) | (bits<<shift);
                                        latch[j/2] = input[j/2];
                                }
-                       }
 
-                       lcd_gotoxy(0, 0);
-                       for(i=10; i--;)
-                       {
-                               lcd_write(hexdigit(input[i]>>4));
-                               lcd_write(hexdigit(input[i]));
+                               if(lcd_enabled)
+                               {
+                                       lcd_gotoxy(19-offset-nibbles, 0);
+                                       for(i=0; i<=nibbles; ++i)
+                                               lcd_write(rx_buf[3+i]);
+                               }
                        }
                }
                rx_fill = 0xFF;
@@ -159,15 +185,25 @@ void receive(uint8_t c)
                        rx_fill = 0xFF;
        }
 
-       lcd_gotoxy(log_pos%20, 1+log_pos/20);
-       lcd_write(c);
-       ++log_pos;
-       if(log_pos>=60)
-               log_pos = 0;
-       lcd_gotoxy(log_pos%20, 1+log_pos/20);
-       lcd_write(255);
+       if(lcd_enabled)
+       {
+               lcd_gotoxy(log_col, 1+log_row);
+               lcd_write(c);
+               ++log_col;
+               if(log_col>=20)
+               {
+                       log_col = 0;
+                       ++log_row;
+                       if(log_row>=3)
+                               log_row = 0;
+                       lcd_gotoxy(log_col, 1+log_row);
+               }
+               lcd_write(255);
+       }
 }
 
+SERIAL_SET_CALLBACK(receive)
+
 uint8_t hexdigit(uint8_t n)
 {
        n &= 0xF;