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

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.10    ! pazsan     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]);
1.9       pazsan     84:     }
1.10    ! pazsan     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) {
1.9       pazsan    106:          cmd[1] = 0xB; // open stream
                    107:          cmd[2] = handle;
                    108:          bt_send_cmd(cmd);
1.10    ! pazsan    109:          systick_wait_ms(100);
        !           110:          bt_set_arm7_cmd();
        !           111:          bt_mode = 1;
        !           112:          display_char(')'); display_update();
1.9       pazsan    113:        }
1.10    ! pazsan    114:        //        bt_state = 1;
        !           115:       } else {
        !           116:        display_char('(');
        !           117:       }
        !           118:       break;
        !           119:     case 0x20: // discoverableack
        !           120:       if(cmd[2]) {
        !           121:        display_char('?');
1.9       pazsan    122:        break;
                    123:       }
1.10    ! pazsan    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;
1.4       pazsan    131:     }
1.10    ! pazsan    132:     display_update();
1.4       pazsan    133:   }
1.9       pazsan    134:   return 0;
1.4       pazsan    135: }
                    136: 
1.5       pazsan    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();
1.10    ! pazsan    150:   bt_start_ad_converter();
1.6       pazsan    151:   do {
                    152:     bt_receive(cmd);
                    153:   } while((cmd[0] != 3) && (cmd[1] != 0x14));
1.10    ! pazsan    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
1.6       pazsan    159:   display_clear(1);
                    160:   show_splash(1000);
1.5       pazsan    161:   display_goto_xy(0,0);
                    162: 
                    163:   terminal_prepped = 1;
                    164: }
                    165: 
                    166: void deprep_terminal ()
                    167: {
                    168:   terminal_prepped = 0;
                    169: }
                    170: 
1.3       pazsan    171: long key_avail ()
                    172: {
                    173:   if(!terminal_prepped) prep_terminal();
1.4       pazsan    174: 
1.9       pazsan    175:   do_bluetooth();
1.4       pazsan    176:   if(bt_mode) {
                    177:     return bt_avail();
                    178:   } else {
1.9       pazsan    179:     systick_wait_ms(100);
1.4       pazsan    180:     return 0;
                    181:   }
1.3       pazsan    182: }
                    183: 
                    184: Cell getkey()
                    185: {
1.4       pazsan    186:   int key;
                    187: 
1.3       pazsan    188:   if(!terminal_prepped) prep_terminal();
1.4       pazsan    189: 
                    190:   if(needs_update) {
                    191:     display_update();
                    192:     needs_update = 0;
                    193:   }
                    194: 
                    195:   while(!key_avail());
1.7       pazsan    196:   
1.6       pazsan    197:   while((key=bt_getkey())==0);
                    198:   display_char(key); display_update();
                    199: 
                    200:   return key;
1.3       pazsan    201: }
                    202: 
1.1       pazsan    203: void emit_char(char x)
                    204: {
1.3       pazsan    205:   if(!terminal_prepped) prep_terminal();
1.6       pazsan    206:   /*  display_char(x);
1.4       pazsan    207:   if(x == '\n') {
                    208:     display_update();
                    209:     needs_update = 0;
                    210:   } else
1.6       pazsan    211:   needs_update = 1; */
1.7       pazsan    212:   if(bt_mode) bt_send(&x, 1);
1.1       pazsan    213: }
                    214: 
                    215: void type_chars(char *addr, unsigned int l)
                    216: {
1.7       pazsan    217:   if(bt_mode) bt_send(addr, l);
1.6       pazsan    218:   /*  int i;
1.2       pazsan    219:   for(i=0; i<l; i++)
1.6       pazsan    220:   emit_char(addr[i]); */
1.1       pazsan    221: }

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