File:  [gforth] / gforth / engine / io-nxt.c
Revision 1.13: download - view: text, annotated - select for diffs
Tue Jul 15 16:11:49 2008 UTC (12 years, 11 months ago) by anton
Branches: MAIN
CVS tags: v0-7-0, HEAD
updated copyright years
updated copyright-blacklist (added libltdl)
updated distributed files (don't distribute files without distribution terms)
added copyright to preforth.in and build-ec.in

    1: /* direct key io driver for NXT brick
    2: 
    3:   Copyright (C) 2007,2008 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: static char bt_buf[0x100];
   41: static char udp_buf[0x100];
   42: int bt_index;
   43: int udp_index;
   44: int udp_size;
   45: 
   46: void
   47: show_splash(U32 milliseconds)
   48: {
   49:   display_clear(0);
   50:   display_goto_xy(4, 6);
   51:   display_string("Gforth NXT");
   52:   display_update();
   53: 
   54:   systick_wait_ms(milliseconds);
   55: }
   56: 
   57: const static bt_lens[0x3C] = { 10, 3, 10, 3,  10, 30, 10, 3,  4, 4, 26, 4,  3, 0, 0, 0,
   58: 			       0, 0, 0, 0,    0, 0, 0, 0,     0, 0, 0, 0,   4, 4, 0, 0,
   59: 			       0, 19, 0, 4,   0, 3, 0, 3,     0, 3, 3, 3,   0, 0, 0, 3,
   60: 			       0, 0, 0, 3, 5, 0, 3, 4, 0,     3, 0, 3, 0 };
   61: 
   62: void bt_send_cmd(char * cmd)
   63: {
   64:   int len = bt_lens[cmd[1]];
   65:   int i, sum=0;
   66: 
   67:   cmd[0] = len;
   68:   for(i=1; i<len-2; i++)
   69:     sum += cmd[i];
   70:   sum = -sum;
   71:   cmd[i++] = (char)(sum>>8);
   72:   cmd[i++] = (char)(sum & 0xff);
   73: 
   74:   //  systick_wait_ms(500);
   75: 
   76:   bt_send(cmd, len+1);
   77: }
   78: 
   79: int do_bluetooth ()
   80: {
   81:   if(!bt_mode) {
   82:     char cmd[30];
   83:     
   84:     bt_receive(cmd);
   85:     if(cmd[0]) {
   86:       display_goto_xy(0,1);
   87:       display_hex(cmd[0],2);
   88:       display_goto_xy(3,1);
   89:       display_hex(cmd[1],2);
   90:     }
   91:     
   92:     switch(cmd[1]) {
   93:     case 0x16: // request connection
   94:       display_char('-');
   95:       cmd[1] = 0x9; // accept connection
   96:       cmd[2] = 1; // yes, we do
   97:       bt_send_cmd(cmd);
   98:       break;
   99:     case 0x0f: // inquiry result
  100:       display_char('+');
  101:       cmd[1] = 0x05;
  102:       bt_send_cmd(cmd); // add devices as known device
  103:       break;
  104:     case 0x13: // connect result
  105:       if(cmd[2]) {
  106: 	int n=0;
  107: 	int handle=cmd[3];
  108: 	display_char('/'); display_update();
  109: 	systick_wait_ms(300);
  110: 	bt_receive(cmd);
  111: 	if(cmd[0]==0) {
  112: 	  cmd[1] = 0xB; // open stream
  113: 	  cmd[2] = handle;
  114: 	  bt_send_cmd(cmd);
  115: 	  systick_wait_ms(100);
  116: 	  bt_set_arm7_cmd();
  117: 	  bt_mode = 1;
  118: 	  display_char(')'); display_update();
  119: 	  type_chars("Gforth NXT\n", 11);
  120: 	}
  121: 	//	  bt_state = 1;
  122:       } else {
  123: 	display_char('(');
  124:       }
  125:       break;
  126:     case 0x20: // discoverableack
  127:       if(cmd[2]) {
  128: 	display_char('?');
  129: 	cmd[1] = 0x03; bt_send_cmd(cmd); // open port query
  130: 	break;
  131:       }
  132:     case 0x10:
  133:     case 0x14:
  134:       display_char('!');
  135:       cmd[1] = 0x1C; cmd[2] = 1; bt_send_cmd(cmd);
  136:       break;
  137:     default:
  138:       break;
  139:     }
  140:     display_update();
  141:   }
  142:   return bt_mode;
  143: }
  144: 
  145: void prep_terminal ()
  146: {
  147:   char cmd[30];
  148: 
  149:   aic_initialise();
  150:   interrupts_enable();
  151:   systick_init();
  152:   sound_init();
  153:   nxt_avr_init();
  154:   nxt_motor_init();
  155:   i2c_init();
  156:   bt_init();
  157:   udp_init();
  158:   display_init();
  159:   show_splash(2000);
  160:   display_goto_xy(0,0);
  161:   display_string("BT Reset ");
  162:   display_update();
  163:   //  bt_reset();
  164:   display_string("ok");
  165:   display_update();
  166:   bt_buf[0] = 0;
  167:   bt_buf[1] = 0;
  168:   bt_index = 0;
  169:   //  bt_start_ad_converter();
  170:   //  do {
  171:   //    bt_receive(cmd);
  172:   //  } while((cmd[0] != 3) && (cmd[1] != 0x14));
  173:   //  cmd[1] = 0x36; // break stream mode
  174:   //  cmd[2] = 0;
  175:   //  bt_send_cmd(cmd);
  176:   // cmd[1] = 0x1C; cmd[2] = 1; bt_send_cmd(cmd); // make visible
  177:   display_string(".");
  178:   display_goto_xy(0,1);
  179:   display_update();
  180:   while(!do_bluetooth() && !udp_configured());
  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:   if(do_bluetooth()) {
  195:     return bt_buf[0] - bt_index;
  196:   } else if(udp_configured()) {
  197:     return udp_size - udp_index;
  198:   } else {
  199:     systick_wait_ms(100);
  200:     return 0;
  201:   }
  202: }
  203: 
  204: Cell getkey()
  205: {
  206:   int key;
  207:   
  208:   if(!terminal_prepped) prep_terminal();
  209:   
  210:   if(needs_update) {
  211:     display_update();
  212:     needs_update = 0;
  213:   }
  214:   
  215:   do {
  216:     if(do_bluetooth()) {
  217:       while(bt_index >= bt_buf[0]) {
  218: 	bt_receive(bt_buf);
  219: 	bt_index = 0;
  220:       }
  221:       key = bt_buf[bt_index++];
  222:     } else if(udp_configured()) {
  223:       if(udp_index < udp_size) {
  224: 	key = udp_buf[udp_index++];
  225:       } else {
  226: 	udp_size = udp_read(udp_buf, 0x100);
  227: 	udp_index = 0;
  228: 	key = 0;
  229:       }
  230:     } else {
  231:       key = 0;
  232:     }
  233:   } while(!key);
  234:     
  235:   display_char(key); display_update();
  236:   
  237:   return key;
  238: }
  239: 
  240: void emit_char(char x)
  241: {
  242:   char buf[3];
  243:   if(!terminal_prepped) prep_terminal();
  244:   if(bt_mode) {
  245:     buf[0] = 1;
  246:     buf[1] = 0;
  247:     buf[2] = x;
  248:     bt_send(buf, 3);
  249:   }
  250: }
  251: 
  252: void type_chars(char *addr, unsigned int l)
  253: {
  254:   int i;
  255:   char buf[0x100];
  256:   if(bt_mode) {
  257:     buf[0]=l;
  258:     buf[1]=0;
  259:     for(i=0; i<l; i++) {
  260:       buf[2+i]=addr[i];
  261:     }
  262:     bt_send(buf, l+2);
  263:   }
  264: }
  265: 
  266: volatile unsigned char gMakeRequest;

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