File:  [gforth] / gforth / engine / io-nxt.c
Revision 1.11: download - view: text, annotated - select for diffs
Mon Dec 31 18:40:25 2007 UTC (14 years, 11 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright notices for GPL v3

    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 3
   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, see http://www.gnu.org/licenses/.
   19: 
   20:   The following is stolen from the readline library for bash
   21: */
   22: 
   23: #include "config.h"
   24: #include "forth.h"
   25: #include "../arch/arm/nxt/AT91SAM7.h"
   26: #include "../arch/arm/nxt/bt.h"
   27: #include "../arch/arm/nxt/display.h"
   28: #include "../arch/arm/nxt/aic.h"
   29: #include "../arch/arm/nxt/systick.h"
   30: #include "../arch/arm/nxt/sound.h"
   31: #include "../arch/arm/nxt/interrupts.h"
   32: #include "../arch/arm/nxt/nxt_avr.h"
   33: #include "../arch/arm/nxt/nxt_motors.h"
   34: #include "../arch/arm/nxt/i2c.h"
   35: 
   36: int terminal_prepped = 0;
   37: int needs_update = 0;
   38: int bt_mode = 0;
   39: int bt_state = 0;
   40: 
   41: void
   42: show_splash(U32 milliseconds)
   43: {
   44:   display_clear(0);
   45:   display_goto_xy(4, 6);
   46:   display_string("Gforth NXT");
   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:   //  systick_wait_ms(500);
   70: 
   71:   bt_send(cmd, len+1);
   72: }
   73: 
   74: int do_bluetooth ()
   75: {
   76:   if(!bt_mode) {
   77:     char cmd[30];
   78:     
   79:     bt_receive(cmd);
   80:     if(cmd[0] | cmd[1]) {
   81:       display_char('0'+cmd[0]);
   82:       display_char('0'+cmd[1]);
   83:     }
   84:     
   85:     switch(cmd[1]) {
   86:     case 0x16: // request connection
   87:       display_char('-');
   88:       cmd[1] = 0x9; // accept connection
   89:       cmd[2] = 1; // yes, we do
   90:       bt_send_cmd(cmd);
   91:       break;
   92:     case 0x0f: // inquiry result
   93:       display_char('+');
   94:       cmd[1] = 0x05;
   95:       bt_send_cmd(cmd); // add devices as known device
   96:       break;
   97:     case 0x13: // connect result
   98:       if(cmd[2]) {
   99: 	int n=0;
  100: 	int handle=cmd[3];
  101: 	display_char('/'); display_update();
  102: 	systick_wait_ms(300);
  103: 	bt_receive(cmd);
  104: 	if(cmd[0]==0) {
  105: 	  cmd[1] = 0xB; // open stream
  106: 	  cmd[2] = handle;
  107: 	  bt_send_cmd(cmd);
  108: 	  systick_wait_ms(100);
  109: 	  bt_set_arm7_cmd();
  110: 	  bt_mode = 1;
  111: 	  display_char(')'); display_update();
  112: 	}
  113: 	//	  bt_state = 1;
  114:       } else {
  115: 	display_char('(');
  116:       }
  117:       break;
  118:     case 0x20: // discoverableack
  119:       if(cmd[2]) {
  120: 	display_char('?');
  121: 	break;
  122:       }
  123:     case 0x10:
  124:     case 0x14:
  125:       display_char('!');
  126:       cmd[1] = 0x1C; cmd[2] = 1; bt_send_cmd(cmd);
  127:       break;
  128:     default:
  129:       break;
  130:     }
  131:     display_update();
  132:   }
  133:   return 0;
  134: }
  135: 
  136: void prep_terminal ()
  137: {
  138:   char cmd[30];
  139: 
  140:   aic_initialise();
  141:   interrupts_enable();
  142:   systick_init();
  143:   sound_init();
  144:   nxt_avr_init();
  145:   display_init();
  146:   nxt_motor_init();
  147:   i2c_init();
  148:   bt_init();
  149:   bt_start_ad_converter();
  150:   do {
  151:     bt_receive(cmd);
  152:   } while((cmd[0] != 3) && (cmd[1] != 0x14));
  153:   //  cmd[1] = 0x36; // break stream mode
  154:   //  cmd[2] = 0;
  155:   //  bt_send_cmd(cmd);
  156:   //  cmd[1] = 0x1C; cmd[2] = 1; bt_send_cmd(cmd); // make visible
  157:   cmd[1] = 0x03; bt_send_cmd(cmd); // open port query
  158:   display_clear(1);
  159:   show_splash(1000);
  160:   display_goto_xy(0,0);
  161: 
  162:   terminal_prepped = 1;
  163: }
  164: 
  165: void deprep_terminal ()
  166: {
  167:   terminal_prepped = 0;
  168: }
  169: 
  170: long key_avail ()
  171: {
  172:   if(!terminal_prepped) prep_terminal();
  173: 
  174:   do_bluetooth();
  175:   if(bt_mode) {
  176:     return bt_avail();
  177:   } else {
  178:     systick_wait_ms(100);
  179:     return 0;
  180:   }
  181: }
  182: 
  183: Cell getkey()
  184: {
  185:   int key;
  186: 
  187:   if(!terminal_prepped) prep_terminal();
  188: 
  189:   if(needs_update) {
  190:     display_update();
  191:     needs_update = 0;
  192:   }
  193: 
  194:   while(!key_avail());
  195:   
  196:   while((key=bt_getkey())==0);
  197:   display_char(key); display_update();
  198: 
  199:   return key;
  200: }
  201: 
  202: void emit_char(char x)
  203: {
  204:   if(!terminal_prepped) prep_terminal();
  205:   /*  display_char(x);
  206:   if(x == '\n') {
  207:     display_update();
  208:     needs_update = 0;
  209:   } else
  210:   needs_update = 1; */
  211:   if(bt_mode) bt_send(&x, 1);
  212: }
  213: 
  214: void type_chars(char *addr, unsigned int l)
  215: {
  216:   if(bt_mode) bt_send(addr, l);
  217:   /*  int i;
  218:   for(i=0; i<l; i++)
  219:   emit_char(addr[i]); */
  220: }

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