Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1053 - 1
/*****************************************************************************
2
 *   Copyright (C) 2009-2010 Peter "woggle" Mack, mac@denich.net             *
3
 *                                                                           *
4
 *   see this fine thread on RCLine:                                         *
5
 *   http://www.rclineforum.de/forum/thread.php?threadid=226786              *
6
 *                                                                           *
7
 *   This program is free software; you can redistribute it and/or modify    *
8
 *   it under the terms of the GNU General Public License as published by    *
9
 *   the Free Software Foundation; either version 2 of the License.          *
10
 *                                                                           *
11
 *   This program is distributed in the hope that it will be useful,         *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
14
 *   GNU General Public License for more details.                            *
15
 *                                                                           *
16
 *   You should have received a copy of the GNU General Public License       *
17
 *   along with this program; if not, write to the                           *
18
 *   Free Software Foundation, Inc.,                                         *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.               *
20
 *                                                                           *
21
 *****************************************************************************/
22
 
23
#include <avr/io.h>
24
#include <avr/interrupt.h>
25
#include <avr/pgmspace.h>
26
#include <util/delay.h>
27
 
28
#include "main.h"               // LEDs
29
#include "timer.h"      // Keys
30
#include "lcd.h"
31
#include "jeti.h"
32
#include "usart.h"
33
#define font8x8
34
 
35
uint8_t JetiBuffer[32]; // 32 characters
36
volatile uint8_t JetiBufferReady;
37
 
38
 
39
//*****************************************************************************
40
// 
41
#if 0
42
ISR (USART_RXC_vect)
43
{
44
        uint8_t stat;
45
        uint8_t rh;
46
        uint8_t rl;
47
        static uint8_t jbp;
48
 
49
        stat = UCSRA;
50
        rh = UCSRB;
51
        rl = UDR;
52
 
53
        if (stat & ((1 << FE) | (1 << DOR0) | (1 << UPE)))
54
        {       // discard buffer and start new on any error
55
                JetiBufferReady = 0;
56
                jbp = 0;
57
                                                                                                                  //1234567890123456
58
                lcd_printpj_at (0, 3, PSTR(" Communication  "), 0);
59
                lcd_printpj_at (0, 4, PSTR("    Error       "), 0);
60
//              LED6_TOGGLE;
61
        }
62
        else if ((rh & (1 << RXB8)) == 0)
63
        {       // control
64
                if (rl == 0xfe)
65
                {       // start condition
66
                        JetiBufferReady = 0;
67
                        jbp = 0;
68
//                      LED1_ON;
69
                }
70
                else if (rl == 0xff)
71
                {       // stop condition
72
                        JetiBufferReady = 1;
73
//                      LED1_OFF;
74
                }
75
        }
76
        else
77
        {       // data
78
                if (jbp < 32)
79
                {
80
                        JetiBuffer[jbp++] = rl;
81
                }
82
        }
83
}
84
#endif
85
//*****************************************************************************
86
// 
87
void JETI_Init (void)
88
{
89
        DDRD  &= ~(1 << DDD3);          // set TXD1 pin as input
90
        PORTD &= ~(1 << PORTD3);        // disable pullup on TXD1 pin
91
 
92
        // set clock divider
93
#undef BAUD
94
#define BAUD 9600
95
#include <util/setbaud.h>
96
        UBRRH = UBRRH_VALUE;
97
        UBRRL = UBRRL_VALUE;
98
 
99
#if USE_2X
100
        UCSR1A |= (1 << U2X1);  // enable double speed operation
101
#else
102
        UCSRA &= ~(1 << U2X);   // disable double speed operation
103
#endif
104
 
105
        // set 9O1
106
        UCSRC = (1 << UPM1) | (1 << UPM1) | (1 << UCSZ1) | (1 << UCSZ0);
107
        UCSRB = (1 << UCSZ2);
108
 
109
        // flush receive buffer
110
        while ( UCSRA & (1 << RXC) ) UDR;
111
}
112
 
113
//*****************************************************************************
114
// disable the txd pin of usart
115
void JETI_DisableTXD (void)
116
{
117
        UCSRB &= ~(1 << TXEN);  // disable TX}
118
}
119
 
120
//*****************************************************************************
121
// enable the txd pin of usart
122
void JETI_EnableTXD (void)
123
{
124
        UCSRB |=  (1 << TXEN);  // enable TX
125
}
126
 
127
 
128
//*****************************************************************************
129
// 
130
void JETI_putw (uint16_t c)
131
{
132
        loop_until_bit_is_set(UCSRA, UDRE);
133
        UCSRB &= ~(1 << TXB8);
134
        if (c & 0x0100)
135
        {
136
                UCSRB |= (1 << TXB8);
137
        }
138
        UDR = c;
139
}
140
 
141
//*****************************************************************************
142
// 
143
void JETI_putc (uint8_t c)
144
{
145
        loop_until_bit_is_set(UCSRA, UDRE);
146
        //      UCSRB &= ~(1 << TXB8);
147
        UCSRB |= (1 << TXB8);
148
        UDR = c;
149
}
150
 
151
//*****************************************************************************
152
// 
153
void JETI_puts (char *s)
154
{
155
        while (*s)
156
        {
157
                JETI_putc (*s);
158
                s++;
159
        }
160
}
161
 
162
//*****************************************************************************
163
// 
164
void JETI_puts_p (const char *s)
165
{
166
        while (pgm_read_byte(s))
167
        {
168
                JETI_putc (pgm_read_byte(s));
169
                s++;
170
        }
171
}
172
 
173
//*****************************************************************************
174
// 
175
void JETI_put_start (void)
176
{
177
        loop_until_bit_is_set(UCSRA, UDRE);
178
        UCSRB &= ~(1 << TXB8);
179
        UDR = 0xFE;
180
}
181
 
182
//*****************************************************************************
183
// 
184
void JETI_put_stop (void)
185
{
186
        loop_until_bit_is_set(UCSRA, UDRE);
187
        UCSRB &= ~(1 << TXB8);
188
        UDR = 0xFF;
189
}
190
 
191
//*****************************************************************************
192
// 
193
void jeti (void)
194
{
195
        uint8_t key;
196
        uint8_t i;
197
 
198
        // enable Rx
199
        UCSRB |= (1 << RXEN);
200
        UCSRB |= (1 << RXCIE);
201
 
202
        lcd_cls ();
203
        lcd_printp_at (0, 0, PSTR("Jeti Box Display"), 0);
204
        lcd_printpns_at (0, 7, PSTR(" \x1a    \x1b      \x1c     \x1d"), 0);
205
 
206
#ifdef font8x8
207
        lcd_line(0, 21, 127, 21, 1);
208
        lcd_line(0, 22, 127, 22, 1);
209
        lcd_line(0, 40, 127, 40, 1);
210
        lcd_line(0, 41, 127, 41, 1);
211
#else
212
        lcd_rect(10, 22, 98, 18, 1);
213
#endif
214
 
215
        do
216
        {
217
                if (JetiBufferReady)
218
                {
219
                        JETI_EnableTXD();
220
//                      LED2_ON;
221
                        for (i = 0; i < 16; i++)
222
                        {
223
#ifdef font8x8
224
                                lcd_putc_jeti(i, 3, JetiBuffer[i], 0);
225
#else
226
                                lcd_putc (2 + i, 3, JetiBuffer[i], 0);
227
#endif
228
                        }
229
                        for (i = 0; i < 16; i++)
230
                        {
231
#ifdef font8x8
232
                                lcd_putc_jeti(i, 4, JetiBuffer[i + 16], 0);
233
#else
234
                                lcd_putc (2 + i, 4, JetiBuffer[i + 16], 0);
235
#endif
236
                        }
237
                        JetiBufferReady = 0;    // invalidate buffer
238
//                      LED2_OFF;
239
 
240
                        _delay_ms (1);  //
241
                                                                                                // Writing to the display takes aprox. 5.8 ms @ 7 MHz and 3.2 ms @ 20 MHz.
242
                                                                                                // With the additional 4 ms we had a 10 ms delay.
243
                                                                                                // 10 ms works perfect with the MUI30 and the MT125
244
                                                                                                // But not with the TU transceiver module.
245
 
246
                        key = get_key_short ((1 << KEY_MINUS) | (1 << KEY_PLUS) | (1 << KEY_ESC) | (1 << KEY_ENTER));
247
                        key = (key << 1) | (key >> 3);
248
                        key = (~key) & 0xf0;
249
 
250
                        JETI_putw((uint16_t) key);
251
                        _delay_ms (1);
252
                        JETI_DisableTXD();
253
                }
254
        }
255
        while (!get_key_long (1 << KEY_ESC));
256
        get_key_press(KEY_ALL);
257
 
258
        // disable Rx
259
        UCSRB &= ~(1 << RXCIE);
260
        UCSRB &= ~(1 << RXEN);
261
}