Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 729 → Rev 730

/Transportables_Koptertool/trunk/jeti.c
0,0 → 1,260
/*****************************************************************************
* Copyright (C) 2009-2010 Peter "woggle" Mack, mac@denich.net *
* *
* see this fine thread on RCLine: *
* http://www.rclineforum.de/forum/thread.php?threadid=226786 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
*****************************************************************************/
 
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
 
#include "main.h" // LEDs
#include "timer.h" // Keys
#include "lcd.h"
#include "jeti.h"
 
#define font8x8
 
uint8_t JetiBuffer[32]; // 32 characters
volatile uint8_t JetiBufferReady;
 
 
//*****************************************************************************
//
ISR (USART1_RX_vect)
{
uint8_t stat;
uint8_t rh;
uint8_t rl;
static uint8_t jbp;
stat = UCSR1A;
rh = UCSR1B;
rl = UDR1;
if (stat & ((1 << FE1) | (1 << DOR1) | (1 << UPE1)))
{ // discard buffer and start new on any error
JetiBufferReady = 0;
jbp = 0;
//1234567890123456
lcd_printpj_at (0, 3, PSTR(" Communication "), 0);
lcd_printpj_at (0, 4, PSTR(" Error "), 0);
LED6_TOGGLE;
}
else if ((rh & (1 << RXB81)) == 0)
{ // control
if (rl == 0xfe)
{ // start condition
JetiBufferReady = 0;
jbp = 0;
LED1_ON;
}
else if (rl == 0xff)
{ // stop condition
JetiBufferReady = 1;
LED1_OFF;
}
}
else
{ // data
if (jbp < 32)
{
JetiBuffer[jbp++] = rl;
}
}
}
 
//*****************************************************************************
//
void JETI_Init (void)
{
DDRD &= ~(1 << DDD3); // set TXD1 pin as input
PORTD &= ~(1 << PORTD3); // disable pullup on TXD1 pin
// set clock divider
#undef BAUD
#define BAUD 9600
#include <util/setbaud.h>
UBRR1H = UBRRH_VALUE;
UBRR1L = UBRRL_VALUE;
#if USE_2X
UCSR1A |= (1 << U2X1); // enable double speed operation
#else
UCSR1A &= ~(1 << U2X1); // disable double speed operation
#endif
// set 9O1
UCSR1C = (1 << UPM11) | (1 << UPM10) | (1 << UCSZ11) | (1 << UCSZ10);
UCSR1B = (1 << UCSZ12);
// flush receive buffer
while ( UCSR1A & (1 << RXC1) ) UDR1;
}
 
//*****************************************************************************
// disable the txd pin of usart
void JETI_DisableTXD (void)
{
UCSR1B &= ~(1 << TXEN1); // disable TX}
}
 
//*****************************************************************************
// enable the txd pin of usart
void JETI_EnableTXD (void)
{
UCSR1B |= (1 << TXEN1); // enable TX
}
 
 
//*****************************************************************************
//
void JETI_putw (uint16_t c)
{
loop_until_bit_is_set(UCSR1A, UDRE1);
UCSR1B &= ~(1 << TXB81);
if (c & 0x0100)
{
UCSR1B |= (1 << TXB81);
}
UDR1 = c;
}
 
//*****************************************************************************
//
void JETI_putc (uint8_t c)
{
loop_until_bit_is_set(UCSR1A, UDRE1);
// UCSRB &= ~(1 << TXB8);
UCSR1B |= (1 << TXB81);
UDR1 = c;
}
 
//*****************************************************************************
//
void JETI_puts (char *s)
{
while (*s)
{
JETI_putc (*s);
s++;
}
}
 
//*****************************************************************************
//
void JETI_puts_p (const char *s)
{
while (pgm_read_byte(s))
{
JETI_putc (pgm_read_byte(s));
s++;
}
}
 
//*****************************************************************************
//
void JETI_put_start (void)
{
loop_until_bit_is_set(UCSR1A, UDRE1);
UCSR1B &= ~(1 << TXB81);
UDR1 = 0xFE;
}
 
//*****************************************************************************
//
void JETI_put_stop (void)
{
loop_until_bit_is_set(UCSR1A, UDRE1);
UCSR1B &= ~(1 << TXB81);
UDR1 = 0xFF;
}
 
//*****************************************************************************
//
void jeti (void)
{
uint8_t key;
uint8_t i;
 
// enable Rx
UCSR1B |= (1 << RXEN1);
UCSR1B |= (1 << RXCIE1);
lcd_cls ();
lcd_printp_at (0, 0, PSTR("Jeti Box Display"), 0);
lcd_printpns_at (0, 7, PSTR(" \x1a \x1b \x1c \x1d"), 0);
#ifdef font8x8
lcd_line(0, 21, 127, 21, 1);
lcd_line(0, 22, 127, 22, 1);
lcd_line(0, 40, 127, 40, 1);
lcd_line(0, 41, 127, 41, 1);
#else
lcd_rect(10, 22, 98, 18, 1);
#endif
do
{
if (JetiBufferReady)
{
JETI_EnableTXD();
LED2_ON;
for (i = 0; i < 16; i++)
{
#ifdef font8x8
lcd_putc_jeti(i, 3, JetiBuffer[i], 0);
#else
lcd_putc (2 + i, 3, JetiBuffer[i], 0);
#endif
}
for (i = 0; i < 16; i++)
{
#ifdef font8x8
lcd_putc_jeti(i, 4, JetiBuffer[i + 16], 0);
#else
lcd_putc (2 + i, 4, JetiBuffer[i + 16], 0);
#endif
}
JetiBufferReady = 0; // invalidate buffer
LED2_OFF;
_delay_ms (1); //
// Writing to the display takes aprox. 5.8 ms @ 7 MHz and 3.2 ms @ 20 MHz.
// With the additional 4 ms we had a 10 ms delay.
// 10 ms works perfect with the MUI30 and the MT125
// But not with the TU transceiver module.
key = get_key_short ((1 << KEY_MINUS) | (1 << KEY_PLUS) | (1 << KEY_ESC) | (1 << KEY_ENTER));
key = (key << 1) | (key >> 3);
key = (~key) & 0xf0;
JETI_putw((uint16_t) key);
_delay_ms (1);
JETI_DisableTXD();
}
}
while (!get_key_long (1 << KEY_ESC));
get_key_press(KEY_ALL);
 
// disable Rx
UCSR1B &= ~(1 << RXCIE1);
UCSR1B &= ~(1 << RXEN1);
}