File:  [gforth] / gforth / engine / io-nxt.c
Revision 1.8: download - view: text, annotated - select for diffs
Tue Apr 24 21:49:25 2007 UTC (15 years, 7 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixed important bug

    1: /* direct key io driver for NXT brick
    2: 
    3:   Copyright (C) 2007 Free Software Foundation, Inc.
    4: 
    5:   This file is part of Gforth.
    6: 
    7:   Gforth is free software; you can redistribute it and/or
    8:   modify it under the terms of the GNU General Public License
    9:   as published by the Free Software Foundation; either version 2
   10:   of the License, or (at your option) any later version.
   11: 
   12:   This program is distributed in the hope that it will be useful,
   13:   but WITHOUT ANY WARRANTY; without even the implied warranty of
   14:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15:   GNU General Public License for more details.
   16: 
   17:   You should have received a copy of the GNU General Public License
   18:   along with this program; if not, write to the Free Software
   19:   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21:   The following is stolen from the readline library for bash
   22: */
   23: 
   24: #include "config.h"
   25: #include "forth.h"
   26: #include "../arch/arm/nxt/AT91SAM7.h"
   27: #include "../arch/arm/nxt/bt.h"
   28: #include "../arch/arm/nxt/display.h"
   29: #include "../arch/arm/nxt/aic.h"
   30: #include "../arch/arm/nxt/systick.h"
   31: #include "../arch/arm/nxt/sound.h"
   32: #include "../arch/arm/nxt/interrupts.h"
   33: #include "../arch/arm/nxt/nxt_avr.h"
   34: #include "../arch/arm/nxt/nxt_motors.h"
   35: #include "../arch/arm/nxt/i2c.h"
   36: 
   37: int terminal_prepped = 0;
   38: int needs_update = 0;
   39: int bt_mode = 0;
   40: 
   41: void
   42: show_splash(U32 milliseconds)
   43: {
   44:   display_clear(0);
   45:   display_goto_xy(6, 6);
   46:   display_string("Gforth");
   47:   display_update();
   48: 
   49:   systick_wait_ms(milliseconds);
   50: }
   51: 
   52: const static bt_lens[0x3C] = { 10, 3, 10, 3,  10, 30, 10, 3,  4, 4, 26, 4,  3, 0, 0, 0,
   53: 			       0, 0, 0, 0,    0, 0, 0, 0,     0, 0, 0, 0,   4, 4, 0, 0,
   54: 			       0, 19, 0, 4,   0, 3, 0, 3,     0, 3, 3, 3,   0, 0, 0, 3,
   55: 			       0, 0, 0, 3, 5, 0, 3, 4, 0,     3, 0, 3, 0 };
   56: 
   57: void bt_send_cmd(char * cmd)
   58: {
   59:   int len = bt_lens[cmd[1]];
   60:   int i, sum=0;
   61: 
   62:   cmd[0] = len;
   63:   for(i=1; i<len-2; i++)
   64:     sum += cmd[i];
   65:   sum = -sum;
   66:   cmd[i++] = (char)(sum>>8);
   67:   cmd[i++] = (char)(sum & 0xff);
   68: 
   69:   bt_send(cmd, len+1);
   70: }
   71: 
   72: void do_bluetooth ()
   73: {
   74:   if(!bt_mode) {
   75:     char cmd[30];
   76: 
   77:     bt_receive(cmd);
   78:     if(cmd[0] | cmd[1]) {
   79:       display_char('0'+cmd[0]);
   80:       display_char('0'+cmd[1]);
   81:       display_update();
   82:     }
   83:     
   84:     switch(cmd[1]) {
   85:     case 0x10:
   86:     case 0x16: // request connection
   87:       cmd[1] = 0x9; // accept connection
   88:       cmd[2] = 1; // yes, we do
   89:       bt_send_cmd(cmd);
   90:       break;
   91:     case 0x13: // connect result
   92:       if(cmd[2]) {
   93: 	int handle=cmd[3];
   94: 	cmd[1] = 0xB; // open stream
   95: 	cmd[2] = handle;
   96: 	bt_send_cmd(cmd);
   97: 	while (*AT91C_US1_TNCR != 0);
   98: 	// while(!(*AT91C_PIOA_PDSR & BT_BC4_CMD_PIN));
   99: 	bt_set_arm7_cmd();
  100: 	display_char(')'); display_update();
  101: 	bt_mode = 1;
  102: 	bt_send("Hello Bluetooth\n", 16);
  103:       } else {
  104: 	display_char('('); display_update();
  105:       }
  106:       break;
  107:   default:
  108:     break;
  109:     }
  110:   }
  111: }
  112: 
  113: void prep_terminal ()
  114: {
  115:   char cmd[30];
  116: 
  117:   aic_initialise();
  118:   interrupts_enable();
  119:   systick_init();
  120:   sound_init();
  121:   nxt_avr_init();
  122:   display_init();
  123:   nxt_motor_init();
  124:   i2c_init();
  125:   bt_init();
  126:   do {
  127:     bt_receive(cmd);
  128:   } while((cmd[0] != 3) && (cmd[1] != 0x14));
  129:   //  cmd[1] = 0x21; strcpy(cmd+2, "NXT"); bt_send_cmd(cmd); do_bluetooth();
  130:   cmd[1] = 0x1C; cmd[2] = 1; bt_send_cmd(cmd); do_bluetooth(); // make visible
  131:   cmd[1] = 0x36; cmd[2] = 1; bt_send_cmd(cmd); do_bluetooth(); // don't break stream mode
  132:   cmd[1] = 0x03; bt_send_cmd(cmd); // open port query
  133: 
  134:   display_clear(1);
  135:   show_splash(1000);
  136:   display_goto_xy(0,0);
  137: 
  138:   terminal_prepped = 1;
  139: }
  140: 
  141: void deprep_terminal ()
  142: {
  143:   terminal_prepped = 0;
  144: }
  145: 
  146: long key_avail ()
  147: {
  148:   if(!terminal_prepped) prep_terminal();
  149: 
  150:   if(bt_mode) {
  151:     return bt_avail();
  152:   } else {
  153:     if(bt_avail())
  154:       do_bluetooth();
  155:     return 0;
  156:   }
  157: }
  158: 
  159: Cell getkey()
  160: {
  161:   int key;
  162: 
  163:   if(!terminal_prepped) prep_terminal();
  164: 
  165:   if(needs_update) {
  166:     display_update();
  167:     needs_update = 0;
  168:   }
  169: 
  170:   while(!key_avail());
  171:   
  172:   while((key=bt_getkey())==0);
  173:   display_char(key); display_update();
  174: 
  175:   return key;
  176: }
  177: 
  178: void emit_char(char x)
  179: {
  180:   if(!terminal_prepped) prep_terminal();
  181:   /*  display_char(x);
  182:   if(x == '\n') {
  183:     display_update();
  184:     needs_update = 0;
  185:   } else
  186:   needs_update = 1; */
  187:   if(bt_mode) bt_send(&x, 1);
  188: }
  189: 
  190: void type_chars(char *addr, unsigned int l)
  191: {
  192:   if(bt_mode) bt_send(addr, l);
  193:   /*  int i;
  194:   for(i=0; i<l; i++)
  195:   emit_char(addr[i]); */
  196: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>