Annotation of gforth/engine/io-nxt.c, revision 1.6
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];
1.5 pazsan 65: sum = -sum;
1.4 pazsan 66: cmd[i++] = (char)(sum>>8);
67: cmd[i++] = (char)(sum & 0xff);
68:
1.5 pazsan 69: bt_send(cmd, len+1);
1.1 pazsan 70: }
71:
1.4 pazsan 72: void do_bluetooth ()
73: {
74: if(!bt_mode) {
75: char cmd[30];
76:
77: bt_receive(cmd);
1.6 ! pazsan 78: if(cmd[0] | cmd[1]) {
! 79: display_char('0'+cmd[0]);
! 80: display_char('0'+cmd[1]);
! 81: display_update();
! 82: }
1.4 pazsan 83:
84: switch(cmd[1]) {
1.6 ! pazsan 85: case 0x10:
1.4 pazsan 86: case 0x16: // request connection
1.6 ! pazsan 87: cmd[1] = 0x9; // accept connection
1.4 pazsan 88: cmd[2] = 1; // yes, we do
89: bt_send_cmd(cmd);
90: break;
91: case 0x13: // connect result
92: if(cmd[2]) {
93: int handle=cmd[3];
94: cmd[1] = 0xB; // open stream
95: cmd[2] = handle;
96: bt_send_cmd(cmd);
1.6 ! pazsan 97: display_char('#'); display_update();
! 98: while (*AT91C_US1_TNCR != 0);
! 99: // while(!(*AT91C_PIOA_PDSR & BT_BC4_CMD_PIN));
! 100: bt_set_arm7_cmd();
! 101: display_char('!'); display_update();
1.4 pazsan 102: bt_mode = 1;
1.6 ! pazsan 103: } else {
! 104: display_char('('); display_update();
1.4 pazsan 105: }
106: break;
107: default:
108: break;
109: }
110: }
111: }
112:
1.5 pazsan 113: void prep_terminal ()
114: {
115: char cmd[30];
116:
117: aic_initialise();
118: interrupts_enable();
119: systick_init();
120: sound_init();
121: nxt_avr_init();
122: display_init();
123: nxt_motor_init();
124: i2c_init();
125: bt_init();
1.6 ! pazsan 126: do {
! 127: bt_receive(cmd);
! 128: } while((cmd[0] != 3) && (cmd[1] != 0x14));
1.5 pazsan 129: cmd[1] = 0x21; strcpy(cmd+2, "Gforth NXT"); bt_send_cmd(cmd); do_bluetooth();
130: cmd[1] = 0x1C; cmd[2] = 1; bt_send_cmd(cmd); do_bluetooth(); // make visible
1.6 ! pazsan 131: cmd[1] = 0x36; cmd[2] = 1; bt_send_cmd(cmd); do_bluetooth(); // don't break stream mode
1.5 pazsan 132: cmd[1] = 0x03; bt_send_cmd(cmd); // open port query
133:
1.6 ! pazsan 134: display_clear(1);
! 135: show_splash(1000);
1.5 pazsan 136: display_goto_xy(0,0);
137:
138: terminal_prepped = 1;
139: }
140:
141: void deprep_terminal ()
142: {
143: terminal_prepped = 0;
144: }
145:
1.3 pazsan 146: long key_avail ()
147: {
148: if(!terminal_prepped) prep_terminal();
1.4 pazsan 149:
150: if(bt_mode) {
151: return bt_avail();
152: } else {
153: if(bt_avail())
154: do_bluetooth();
155: return 0;
156: }
1.3 pazsan 157: }
158:
159: Cell getkey()
160: {
1.4 pazsan 161: int key;
162:
1.3 pazsan 163: if(!terminal_prepped) prep_terminal();
1.4 pazsan 164:
165: if(needs_update) {
166: display_update();
167: needs_update = 0;
168: }
169:
170: while(!key_avail());
171:
1.6 ! pazsan 172: while((key=bt_getkey())==0);
! 173: display_char(key); display_update();
! 174:
! 175: return key;
1.3 pazsan 176: }
177:
1.1 pazsan 178: void emit_char(char x)
179: {
1.3 pazsan 180: if(!terminal_prepped) prep_terminal();
1.6 ! pazsan 181: /* display_char(x);
1.4 pazsan 182: if(x == '\n') {
183: display_update();
184: needs_update = 0;
185: } else
1.6 ! pazsan 186: needs_update = 1; */
1.5 pazsan 187: if(bt_mode)
188: bt_send(&x, 1);
1.1 pazsan 189: }
190:
191: void type_chars(char *addr, unsigned int l)
192: {
1.6 ! pazsan 193: if(bt_mode)
! 194: bt_send(addr, l);
! 195: /* int i;
1.2 pazsan 196: for(i=0; i<l; i++)
1.6 ! pazsan 197: emit_char(addr[i]); */
1.1 pazsan 198: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>