File:  [gforth] / gforth / engine / io-nxt.c
Revision 1.9: download - view: text, annotated - select for diffs
Sat Jun 16 20:47:00 2007 UTC (15 years, 5 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixed paste error
Some further tries to get Gforth NXT to talk to me

    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_state) {
   78:     if(bt_get_mode()) {
   79:       if(!bt_mode) {
   80: 	bt_set_arm7_cmd();
   81: 	display_char(')'); display_update();
   82: 	bt_mode = 1;
   83:       }
   84:       return 1;
   85:     } else {
   86:       if(bt_mode) {
   87: 	bt_clear_arm7_cmd();
   88: 	display_char('['); display_update();
   89: 	bt_mode = 0;
   90: 	bt_state = 0;
   91:       } else {
   92: 	display_char('.');
   93: 	display_update();
   94:       }
   95:     }
   96:   } else {
   97:     if(!bt_mode) {
   98:       char cmd[30];
   99:       
  100:       bt_receive(cmd);
  101:       if(cmd[0] | cmd[1]) {
  102: 	display_char('0'+cmd[0]);
  103: 	display_char('0'+cmd[1]);
  104:       }
  105:       
  106:       switch(cmd[1]) {
  107:       case 0x10:
  108:       case 0x16: // request connection
  109: 	display_char('-');
  110: 	cmd[1] = 0x9; // accept connection
  111: 	cmd[2] = 1; // yes, we do
  112: 	bt_send_cmd(cmd);
  113: 	break;
  114:       case 0x0f: // inquiry result
  115: 	cmd[1] = 0x05;
  116: 	bt_send_cmd(cmd); // add devices as known device
  117: 	break;
  118:       case 0x13: // connect result
  119: 	if(cmd[2]) {
  120: 	  display_char('/');
  121: 	  int handle=cmd[3];
  122: 	  cmd[1] = 0xB; // open stream
  123: 	  cmd[2] = handle;
  124: 	  bt_send_cmd(cmd);
  125: 	  bt_state = 1;
  126: 	} else {
  127: 	  display_char('(');
  128: 	}
  129: 	break;
  130:       case 0x20: // discoverableack
  131: 	if(cmd[2]) {
  132: 	  display_char('?');
  133: 	  cmd[1] = 0x03; bt_send_cmd(cmd); // open port query
  134: 	  break;
  135: 	}
  136:       case 0x14:
  137: 	display_char('!');
  138: 	cmd[1] = 0x1C; cmd[2] = 1; bt_send_cmd(cmd);
  139: 	break;
  140:       default:
  141: 	break;
  142:       }
  143:       {
  144: 	extern int display_x, display_y;
  145: 	static int n=0;
  146: 	int x = display_x;
  147: 	int y = display_y;
  148: 	display_goto_xy(0,6);
  149: 	display_char("/|\\-"[(n++)&3]);
  150: 	display_goto_xy(x,y);
  151:       }
  152:       display_update();
  153:     }
  154:   }
  155:   return 0;
  156: }
  157: 
  158: void prep_terminal ()
  159: {
  160:   char cmd[30];
  161: 
  162:   aic_initialise();
  163:   interrupts_enable();
  164:   systick_init();
  165:   sound_init();
  166:   nxt_avr_init();
  167:   display_init();
  168:   nxt_motor_init();
  169:   i2c_init();
  170:   bt_init();
  171:   do {
  172:     bt_receive(cmd);
  173:   } while((cmd[0] != 3) && (cmd[1] != 0x14));
  174:   cmd[1] = 0x36; // break stream mode
  175:   cmd[2] = 0;
  176:   bt_send_cmd(cmd);
  177:   cmd[1] = 0x1C; cmd[2] = 1; bt_send_cmd(cmd); // make visible
  178:   display_clear(1);
  179:   show_splash(1000);
  180:   display_goto_xy(0,0);
  181: 
  182:   terminal_prepped = 1;
  183: }
  184: 
  185: void deprep_terminal ()
  186: {
  187:   terminal_prepped = 0;
  188: }
  189: 
  190: long key_avail ()
  191: {
  192:   if(!terminal_prepped) prep_terminal();
  193: 
  194:   do_bluetooth();
  195:   if(bt_mode) {
  196:     return bt_avail();
  197:   } else {
  198:     systick_wait_ms(100);
  199:     return 0;
  200:   }
  201: }
  202: 
  203: Cell getkey()
  204: {
  205:   int key;
  206: 
  207:   if(!terminal_prepped) prep_terminal();
  208: 
  209:   if(needs_update) {
  210:     display_update();
  211:     needs_update = 0;
  212:   }
  213: 
  214:   while(!key_avail());
  215:   
  216:   while((key=bt_getkey())==0);
  217:   display_char(key); display_update();
  218: 
  219:   return key;
  220: }
  221: 
  222: void emit_char(char x)
  223: {
  224:   if(!terminal_prepped) prep_terminal();
  225:   /*  display_char(x);
  226:   if(x == '\n') {
  227:     display_update();
  228:     needs_update = 0;
  229:   } else
  230:   needs_update = 1; */
  231:   if(bt_mode) bt_send(&x, 1);
  232: }
  233: 
  234: void type_chars(char *addr, unsigned int l)
  235: {
  236:   if(bt_mode) bt_send(addr, l);
  237:   /*  int i;
  238:   for(i=0; i<l; i++)
  239:   emit_char(addr[i]); */
  240: }

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