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

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
1.11    ! anton       9:   as published by the Free Software Foundation, either version 3
1.1       pazsan     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
1.11    ! anton      18:   along with this program; if not, see http://www.gnu.org/licenses/.
1.1       pazsan     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"
1.2       pazsan     26: #include "../arch/arm/nxt/bt.h"
1.3       pazsan     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"
1.1       pazsan     35: 
1.3       pazsan     36: int terminal_prepped = 0;
1.4       pazsan     37: int needs_update = 0;
                     38: int bt_mode = 0;
1.9       pazsan     39: int bt_state = 0;
1.3       pazsan     40: 
                     41: void
                     42: show_splash(U32 milliseconds)
1.1       pazsan     43: {
1.3       pazsan     44:   display_clear(0);
1.9       pazsan     45:   display_goto_xy(4, 6);
                     46:   display_string("Gforth NXT");
1.3       pazsan     47:   display_update();
1.1       pazsan     48: 
1.3       pazsan     49:   systick_wait_ms(milliseconds);
1.1       pazsan     50: }
                     51: 
1.4       pazsan     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];
1.5       pazsan     65:   sum = -sum;
1.4       pazsan     66:   cmd[i++] = (char)(sum>>8);
                     67:   cmd[i++] = (char)(sum & 0xff);
                     68: 
1.9       pazsan     69:   //  systick_wait_ms(500);
                     70: 
1.5       pazsan     71:   bt_send(cmd, len+1);
1.1       pazsan     72: }
                     73: 
1.9       pazsan     74: int do_bluetooth ()
1.4       pazsan     75: {
1.10      pazsan     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]);
1.9       pazsan     83:     }
1.10      pazsan     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) {
1.9       pazsan    105:          cmd[1] = 0xB; // open stream
                    106:          cmd[2] = handle;
                    107:          bt_send_cmd(cmd);
1.10      pazsan    108:          systick_wait_ms(100);
                    109:          bt_set_arm7_cmd();
                    110:          bt_mode = 1;
                    111:          display_char(')'); display_update();
1.9       pazsan    112:        }
1.10      pazsan    113:        //        bt_state = 1;
                    114:       } else {
                    115:        display_char('(');
                    116:       }
                    117:       break;
                    118:     case 0x20: // discoverableack
                    119:       if(cmd[2]) {
                    120:        display_char('?');
1.9       pazsan    121:        break;
                    122:       }
1.10      pazsan    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;
1.4       pazsan    130:     }
1.10      pazsan    131:     display_update();
1.4       pazsan    132:   }
1.9       pazsan    133:   return 0;
1.4       pazsan    134: }
                    135: 
1.5       pazsan    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();
1.10      pazsan    149:   bt_start_ad_converter();
1.6       pazsan    150:   do {
                    151:     bt_receive(cmd);
                    152:   } while((cmd[0] != 3) && (cmd[1] != 0x14));
1.10      pazsan    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
1.6       pazsan    158:   display_clear(1);
                    159:   show_splash(1000);
1.5       pazsan    160:   display_goto_xy(0,0);
                    161: 
                    162:   terminal_prepped = 1;
                    163: }
                    164: 
                    165: void deprep_terminal ()
                    166: {
                    167:   terminal_prepped = 0;
                    168: }
                    169: 
1.3       pazsan    170: long key_avail ()
                    171: {
                    172:   if(!terminal_prepped) prep_terminal();
1.4       pazsan    173: 
1.9       pazsan    174:   do_bluetooth();
1.4       pazsan    175:   if(bt_mode) {
                    176:     return bt_avail();
                    177:   } else {
1.9       pazsan    178:     systick_wait_ms(100);
1.4       pazsan    179:     return 0;
                    180:   }
1.3       pazsan    181: }
                    182: 
                    183: Cell getkey()
                    184: {
1.4       pazsan    185:   int key;
                    186: 
1.3       pazsan    187:   if(!terminal_prepped) prep_terminal();
1.4       pazsan    188: 
                    189:   if(needs_update) {
                    190:     display_update();
                    191:     needs_update = 0;
                    192:   }
                    193: 
                    194:   while(!key_avail());
1.7       pazsan    195:   
1.6       pazsan    196:   while((key=bt_getkey())==0);
                    197:   display_char(key); display_update();
                    198: 
                    199:   return key;
1.3       pazsan    200: }
                    201: 
1.1       pazsan    202: void emit_char(char x)
                    203: {
1.3       pazsan    204:   if(!terminal_prepped) prep_terminal();
1.6       pazsan    205:   /*  display_char(x);
1.4       pazsan    206:   if(x == '\n') {
                    207:     display_update();
                    208:     needs_update = 0;
                    209:   } else
1.6       pazsan    210:   needs_update = 1; */
1.7       pazsan    211:   if(bt_mode) bt_send(&x, 1);
1.1       pazsan    212: }
                    213: 
                    214: void type_chars(char *addr, unsigned int l)
                    215: {
1.7       pazsan    216:   if(bt_mode) bt_send(addr, l);
1.6       pazsan    217:   /*  int i;
1.2       pazsan    218:   for(i=0; i<l; i++)
1.6       pazsan    219:   emit_char(addr[i]); */
1.1       pazsan    220: }

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