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

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.3       pazsan     40: 
                     41: void
                     42: show_splash(U32 milliseconds)
1.1       pazsan     43: {
1.3       pazsan     44:   display_clear(0);
                     45:   display_goto_xy(6, 6);
                     46:   display_string("Gforth");
                     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];
        !            65:   cmd[i++] = (char)(sum>>8);
        !            66:   cmd[i++] = (char)(sum & 0xff);
        !            67: 
        !            68:   bt_send(cmd, len);
        !            69: }
        !            70: 
1.1       pazsan     71: void prep_terminal ()
                     72: {
1.4     ! pazsan     73:   char cmd[30];
        !            74: 
1.2       pazsan     75:   aic_initialise();
                     76:   interrupts_enable();
                     77:   systick_init();
1.3       pazsan     78:   sound_init();
                     79:   nxt_avr_init();
1.2       pazsan     80:   display_init();
1.3       pazsan     81:   nxt_motor_init();
                     82:   i2c_init();
1.2       pazsan     83:   bt_init();
1.4     ! pazsan     84:   cmd[1] = 3;
        !            85:   bt_send_cmd(cmd); // open port query
        !            86: 
1.3       pazsan     87:   display_goto_xy(0,0);
                     88:   display_clear(1);
1.2       pazsan     89: 
1.1       pazsan     90:   terminal_prepped = 1;
                     91: }
                     92: 
                     93: void deprep_terminal ()
                     94: {
                     95:   terminal_prepped = 0;
                     96: }
                     97: 
1.4     ! pazsan     98: void do_bluetooth ()
        !            99: {
        !           100:   if(!bt_mode) {
        !           101:     char cmd[30];
        !           102: 
        !           103:     bt_receive(cmd);
        !           104:     
        !           105:     switch(cmd[1]) {
        !           106:     case 0x16: // request connection
        !           107:       cmd[1] = 9; // accept connection
        !           108:       cmd[2] = 1; // yes, we do
        !           109:       bt_send_cmd(cmd);
        !           110:       break;
        !           111:     case 0x13: // connect result
        !           112:       if(cmd[2]) {
        !           113:        int handle=cmd[3];
        !           114:        cmd[1] = 0xB; // open stream
        !           115:        cmd[2] = handle;
        !           116:        bt_send_cmd(cmd);
        !           117:        bt_mode = 1;
        !           118:       }
        !           119:       break;
        !           120:   default:
        !           121:     break;
        !           122:     }
        !           123:   }
        !           124: }
        !           125: 
1.3       pazsan    126: long key_avail ()
                    127: {
                    128:   if(!terminal_prepped) prep_terminal();
1.4     ! pazsan    129: 
        !           130:   if(bt_mode) {
        !           131:     return bt_avail();
        !           132:   } else {
        !           133:     if(bt_avail())
        !           134:       do_bluetooth();
        !           135:     return 0;
        !           136:   }
1.3       pazsan    137: }
                    138: 
                    139: Cell getkey()
                    140: {
1.4     ! pazsan    141:   int key;
        !           142: 
1.3       pazsan    143:   if(!terminal_prepped) prep_terminal();
1.4     ! pazsan    144: 
        !           145:   if(needs_update) {
        !           146:     display_update();
        !           147:     needs_update = 0;
        !           148:   }
        !           149: 
        !           150:   while(!key_avail());
        !           151: 
1.3       pazsan    152:   return bt_getkey();
                    153: }
                    154: 
1.1       pazsan    155: void emit_char(char x)
                    156: {
1.3       pazsan    157:   if(!terminal_prepped) prep_terminal();
                    158:   display_char(x);
1.4     ! pazsan    159:   if(x == '\n') {
        !           160:     display_update();
        !           161:     needs_update = 0;
        !           162:   } else
        !           163:     needs_update = 1;
1.2       pazsan    164:   bt_send(&x, 1);
1.1       pazsan    165: }
                    166: 
                    167: void type_chars(char *addr, unsigned int l)
                    168: {
1.2       pazsan    169:   int i;
                    170:   for(i=0; i<l; i++)
1.3       pazsan    171:     emit_char(addr[i]);
1.1       pazsan    172: }

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