Annotation of gforth/engine/io-nxt.c, revision 1.9

1.1       pazsan      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"
1.2       pazsan     27: #include "../arch/arm/nxt/bt.h"
1.3       pazsan     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"
1.1       pazsan     36: 
1.3       pazsan     37: int terminal_prepped = 0;
1.4       pazsan     38: int needs_update = 0;
                     39: int bt_mode = 0;
1.9     ! pazsan     40: int bt_state = 0;
1.3       pazsan     41: 
                     42: void
                     43: show_splash(U32 milliseconds)
1.1       pazsan     44: {
1.3       pazsan     45:   display_clear(0);
1.9     ! pazsan     46:   display_goto_xy(4, 6);
        !            47:   display_string("Gforth NXT");
1.3       pazsan     48:   display_update();
1.1       pazsan     49: 
1.3       pazsan     50:   systick_wait_ms(milliseconds);
1.1       pazsan     51: }
                     52: 
1.4       pazsan     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];
1.5       pazsan     66:   sum = -sum;
1.4       pazsan     67:   cmd[i++] = (char)(sum>>8);
                     68:   cmd[i++] = (char)(sum & 0xff);
                     69: 
1.9     ! pazsan     70:   //  systick_wait_ms(500);
        !            71: 
1.5       pazsan     72:   bt_send(cmd, len+1);
1.1       pazsan     73: }
                     74: 
1.9     ! pazsan     75: int do_bluetooth ()
1.4       pazsan     76: {
1.9     ! pazsan     77:   if(bt_state) {
        !            78:     if(bt_get_mode()) {
        !            79:       if(!bt_mode) {
1.6       pazsan     80:        bt_set_arm7_cmd();
1.7       pazsan     81:        display_char(')'); display_update();
1.4       pazsan     82:        bt_mode = 1;
1.9     ! pazsan     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;
1.6       pazsan     91:       } else {
1.9     ! pazsan     92:        display_char('.');
        !            93:        display_update();
1.4       pazsan     94:       }
1.9     ! pazsan     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();
1.4       pazsan    153:     }
                    154:   }
1.9     ! pazsan    155:   return 0;
1.4       pazsan    156: }
                    157: 
1.5       pazsan    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();
1.6       pazsan    171:   do {
                    172:     bt_receive(cmd);
                    173:   } while((cmd[0] != 3) && (cmd[1] != 0x14));
1.9     ! pazsan    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
1.6       pazsan    178:   display_clear(1);
                    179:   show_splash(1000);
1.5       pazsan    180:   display_goto_xy(0,0);
                    181: 
                    182:   terminal_prepped = 1;
                    183: }
                    184: 
                    185: void deprep_terminal ()
                    186: {
                    187:   terminal_prepped = 0;
                    188: }
                    189: 
1.3       pazsan    190: long key_avail ()
                    191: {
                    192:   if(!terminal_prepped) prep_terminal();
1.4       pazsan    193: 
1.9     ! pazsan    194:   do_bluetooth();
1.4       pazsan    195:   if(bt_mode) {
                    196:     return bt_avail();
                    197:   } else {
1.9     ! pazsan    198:     systick_wait_ms(100);
1.4       pazsan    199:     return 0;
                    200:   }
1.3       pazsan    201: }
                    202: 
                    203: Cell getkey()
                    204: {
1.4       pazsan    205:   int key;
                    206: 
1.3       pazsan    207:   if(!terminal_prepped) prep_terminal();
1.4       pazsan    208: 
                    209:   if(needs_update) {
                    210:     display_update();
                    211:     needs_update = 0;
                    212:   }
                    213: 
                    214:   while(!key_avail());
1.7       pazsan    215:   
1.6       pazsan    216:   while((key=bt_getkey())==0);
                    217:   display_char(key); display_update();
                    218: 
                    219:   return key;
1.3       pazsan    220: }
                    221: 
1.1       pazsan    222: void emit_char(char x)
                    223: {
1.3       pazsan    224:   if(!terminal_prepped) prep_terminal();
1.6       pazsan    225:   /*  display_char(x);
1.4       pazsan    226:   if(x == '\n') {
                    227:     display_update();
                    228:     needs_update = 0;
                    229:   } else
1.6       pazsan    230:   needs_update = 1; */
1.7       pazsan    231:   if(bt_mode) bt_send(&x, 1);
1.1       pazsan    232: }
                    233: 
                    234: void type_chars(char *addr, unsigned int l)
                    235: {
1.7       pazsan    236:   if(bt_mode) bt_send(addr, l);
1.6       pazsan    237:   /*  int i;
1.2       pazsan    238:   for(i=0; i<l; i++)
1.6       pazsan    239:   emit_char(addr[i]); */
1.1       pazsan    240: }

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