File:  [gforth] / gforth / engine / io-nxt.c
Revision 1.10: download - view: text, annotated - select for diffs
Sun Jun 17 19:26:43 2007 UTC (16 years, 10 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Some work on Gforth NXT
last potential problem with line editing fixed

    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: int bt_state = 0;
   41: 
   42: void
   43: show_splash(U32 milliseconds)
   44: {
   45:   display_clear(0);
   46:   display_goto_xy(4, 6);
   47:   display_string("Gforth NXT");
   48:   display_update();
   49: 
   50:   systick_wait_ms(milliseconds);
   51: }
   52: 
   53: const static bt_lens[0x3C] = { 10, 3, 10, 3,  10, 30, 10, 3,  4, 4, 26, 4,  3, 0, 0, 0,
   54: 			       0, 0, 0, 0,    0, 0, 0, 0,     0, 0, 0, 0,   4, 4, 0, 0,
   55: 			       0, 19, 0, 4,   0, 3, 0, 3,     0, 3, 3, 3,   0, 0, 0, 3,
   56: 			       0, 0, 0, 3, 5, 0, 3, 4, 0,     3, 0, 3, 0 };
   57: 
   58: void bt_send_cmd(char * cmd)
   59: {
   60:   int len = bt_lens[cmd[1]];
   61:   int i, sum=0;
   62: 
   63:   cmd[0] = len;
   64:   for(i=1; i<len-2; i++)
   65:     sum += cmd[i];
   66:   sum = -sum;
   67:   cmd[i++] = (char)(sum>>8);
   68:   cmd[i++] = (char)(sum & 0xff);
   69: 
   70:   //  systick_wait_ms(500);
   71: 
   72:   bt_send(cmd, len+1);
   73: }
   74: 
   75: int do_bluetooth ()
   76: {
   77:   if(!bt_mode) {
   78:     char cmd[30];
   79:     
   80:     bt_receive(cmd);
   81:     if(cmd[0] | cmd[1]) {
   82:       display_char('0'+cmd[0]);
   83:       display_char('0'+cmd[1]);
   84:     }
   85:     
   86:     switch(cmd[1]) {
   87:     case 0x16: // request connection
   88:       display_char('-');
   89:       cmd[1] = 0x9; // accept connection
   90:       cmd[2] = 1; // yes, we do
   91:       bt_send_cmd(cmd);
   92:       break;
   93:     case 0x0f: // inquiry result
   94:       display_char('+');
   95:       cmd[1] = 0x05;
   96:       bt_send_cmd(cmd); // add devices as known device
   97:       break;
   98:     case 0x13: // connect result
   99:       if(cmd[2]) {
  100: 	int n=0;
  101: 	int handle=cmd[3];
  102: 	display_char('/'); display_update();
  103: 	systick_wait_ms(300);
  104: 	bt_receive(cmd);
  105: 	if(cmd[0]==0) {
  106: 	  cmd[1] = 0xB; // open stream
  107: 	  cmd[2] = handle;
  108: 	  bt_send_cmd(cmd);
  109: 	  systick_wait_ms(100);
  110: 	  bt_set_arm7_cmd();
  111: 	  bt_mode = 1;
  112: 	  display_char(')'); display_update();
  113: 	}
  114: 	//	  bt_state = 1;
  115:       } else {
  116: 	display_char('(');
  117:       }
  118:       break;
  119:     case 0x20: // discoverableack
  120:       if(cmd[2]) {
  121: 	display_char('?');
  122: 	break;
  123:       }
  124:     case 0x10:
  125:     case 0x14:
  126:       display_char('!');
  127:       cmd[1] = 0x1C; cmd[2] = 1; bt_send_cmd(cmd);
  128:       break;
  129:     default:
  130:       break;
  131:     }
  132:     display_update();
  133:   }
  134:   return 0;
  135: }
  136: 
  137: void prep_terminal ()
  138: {
  139:   char cmd[30];
  140: 
  141:   aic_initialise();
  142:   interrupts_enable();
  143:   systick_init();
  144:   sound_init();
  145:   nxt_avr_init();
  146:   display_init();
  147:   nxt_motor_init();
  148:   i2c_init();
  149:   bt_init();
  150:   bt_start_ad_converter();
  151:   do {
  152:     bt_receive(cmd);
  153:   } while((cmd[0] != 3) && (cmd[1] != 0x14));
  154:   //  cmd[1] = 0x36; // break stream mode
  155:   //  cmd[2] = 0;
  156:   //  bt_send_cmd(cmd);
  157:   //  cmd[1] = 0x1C; cmd[2] = 1; bt_send_cmd(cmd); // make visible
  158:   cmd[1] = 0x03; bt_send_cmd(cmd); // open port query
  159:   display_clear(1);
  160:   show_splash(1000);
  161:   display_goto_xy(0,0);
  162: 
  163:   terminal_prepped = 1;
  164: }
  165: 
  166: void deprep_terminal ()
  167: {
  168:   terminal_prepped = 0;
  169: }
  170: 
  171: long key_avail ()
  172: {
  173:   if(!terminal_prepped) prep_terminal();
  174: 
  175:   do_bluetooth();
  176:   if(bt_mode) {
  177:     return bt_avail();
  178:   } else {
  179:     systick_wait_ms(100);
  180:     return 0;
  181:   }
  182: }
  183: 
  184: Cell getkey()
  185: {
  186:   int key;
  187: 
  188:   if(!terminal_prepped) prep_terminal();
  189: 
  190:   if(needs_update) {
  191:     display_update();
  192:     needs_update = 0;
  193:   }
  194: 
  195:   while(!key_avail());
  196:   
  197:   while((key=bt_getkey())==0);
  198:   display_char(key); display_update();
  199: 
  200:   return key;
  201: }
  202: 
  203: void emit_char(char x)
  204: {
  205:   if(!terminal_prepped) prep_terminal();
  206:   /*  display_char(x);
  207:   if(x == '\n') {
  208:     display_update();
  209:     needs_update = 0;
  210:   } else
  211:   needs_update = 1; */
  212:   if(bt_mode) bt_send(&x, 1);
  213: }
  214: 
  215: void type_chars(char *addr, unsigned int l)
  216: {
  217:   if(bt_mode) bt_send(addr, l);
  218:   /*  int i;
  219:   for(i=0; i<l; i++)
  220:   emit_char(addr[i]); */
  221: }

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