Annotation of gforth/engine/io-nxt.c, revision 1.13
1.1 pazsan 1: /* direct key io driver for NXT brick
2:
1.13 ! anton 3: Copyright (C) 2007,2008 Free Software Foundation, Inc.
1.1 pazsan 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.12 pazsan 40: static char bt_buf[0x100];
41: static char udp_buf[0x100];
42: int bt_index;
43: int udp_index;
44: int udp_size;
1.3 pazsan 45:
46: void
47: show_splash(U32 milliseconds)
1.1 pazsan 48: {
1.3 pazsan 49: display_clear(0);
1.9 pazsan 50: display_goto_xy(4, 6);
51: display_string("Gforth NXT");
1.3 pazsan 52: display_update();
1.1 pazsan 53:
1.3 pazsan 54: systick_wait_ms(milliseconds);
1.1 pazsan 55: }
56:
1.4 pazsan 57: const static bt_lens[0x3C] = { 10, 3, 10, 3, 10, 30, 10, 3, 4, 4, 26, 4, 3, 0, 0, 0,
58: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0,
59: 0, 19, 0, 4, 0, 3, 0, 3, 0, 3, 3, 3, 0, 0, 0, 3,
60: 0, 0, 0, 3, 5, 0, 3, 4, 0, 3, 0, 3, 0 };
61:
62: void bt_send_cmd(char * cmd)
63: {
64: int len = bt_lens[cmd[1]];
65: int i, sum=0;
66:
67: cmd[0] = len;
68: for(i=1; i<len-2; i++)
69: sum += cmd[i];
1.5 pazsan 70: sum = -sum;
1.4 pazsan 71: cmd[i++] = (char)(sum>>8);
72: cmd[i++] = (char)(sum & 0xff);
73:
1.9 pazsan 74: // systick_wait_ms(500);
75:
1.5 pazsan 76: bt_send(cmd, len+1);
1.1 pazsan 77: }
78:
1.9 pazsan 79: int do_bluetooth ()
1.4 pazsan 80: {
1.10 pazsan 81: if(!bt_mode) {
82: char cmd[30];
83:
84: bt_receive(cmd);
1.12 pazsan 85: if(cmd[0]) {
86: display_goto_xy(0,1);
87: display_hex(cmd[0],2);
88: display_goto_xy(3,1);
89: display_hex(cmd[1],2);
1.9 pazsan 90: }
1.10 pazsan 91:
92: switch(cmd[1]) {
93: case 0x16: // request connection
94: display_char('-');
95: cmd[1] = 0x9; // accept connection
96: cmd[2] = 1; // yes, we do
97: bt_send_cmd(cmd);
98: break;
99: case 0x0f: // inquiry result
100: display_char('+');
101: cmd[1] = 0x05;
102: bt_send_cmd(cmd); // add devices as known device
103: break;
104: case 0x13: // connect result
105: if(cmd[2]) {
106: int n=0;
107: int handle=cmd[3];
108: display_char('/'); display_update();
109: systick_wait_ms(300);
110: bt_receive(cmd);
111: if(cmd[0]==0) {
1.9 pazsan 112: cmd[1] = 0xB; // open stream
113: cmd[2] = handle;
114: bt_send_cmd(cmd);
1.10 pazsan 115: systick_wait_ms(100);
116: bt_set_arm7_cmd();
117: bt_mode = 1;
118: display_char(')'); display_update();
1.12 pazsan 119: type_chars("Gforth NXT\n", 11);
1.9 pazsan 120: }
1.10 pazsan 121: // bt_state = 1;
122: } else {
123: display_char('(');
124: }
125: break;
126: case 0x20: // discoverableack
127: if(cmd[2]) {
128: display_char('?');
1.12 pazsan 129: cmd[1] = 0x03; bt_send_cmd(cmd); // open port query
1.9 pazsan 130: break;
131: }
1.10 pazsan 132: case 0x10:
133: case 0x14:
134: display_char('!');
135: cmd[1] = 0x1C; cmd[2] = 1; bt_send_cmd(cmd);
136: break;
137: default:
138: break;
1.4 pazsan 139: }
1.10 pazsan 140: display_update();
1.4 pazsan 141: }
1.12 pazsan 142: return bt_mode;
1.4 pazsan 143: }
144:
1.5 pazsan 145: void prep_terminal ()
146: {
147: char cmd[30];
148:
149: aic_initialise();
150: interrupts_enable();
151: systick_init();
152: sound_init();
153: nxt_avr_init();
154: nxt_motor_init();
155: i2c_init();
156: bt_init();
1.12 pazsan 157: udp_init();
158: display_init();
159: show_splash(2000);
160: display_goto_xy(0,0);
161: display_string("BT Reset ");
162: display_update();
163: // bt_reset();
164: display_string("ok");
165: display_update();
166: bt_buf[0] = 0;
167: bt_buf[1] = 0;
168: bt_index = 0;
169: // bt_start_ad_converter();
170: // do {
171: // bt_receive(cmd);
172: // } while((cmd[0] != 3) && (cmd[1] != 0x14));
1.10 pazsan 173: // cmd[1] = 0x36; // break stream mode
174: // cmd[2] = 0;
175: // bt_send_cmd(cmd);
1.12 pazsan 176: // cmd[1] = 0x1C; cmd[2] = 1; bt_send_cmd(cmd); // make visible
177: display_string(".");
178: display_goto_xy(0,1);
179: display_update();
180: while(!do_bluetooth() && !udp_configured());
1.5 pazsan 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.12 pazsan 194: if(do_bluetooth()) {
195: return bt_buf[0] - bt_index;
196: } else if(udp_configured()) {
197: return udp_size - udp_index;
1.4 pazsan 198: } else {
1.9 pazsan 199: systick_wait_ms(100);
1.4 pazsan 200: return 0;
201: }
1.3 pazsan 202: }
203:
204: Cell getkey()
205: {
1.4 pazsan 206: int key;
1.12 pazsan 207:
1.3 pazsan 208: if(!terminal_prepped) prep_terminal();
1.12 pazsan 209:
1.4 pazsan 210: if(needs_update) {
211: display_update();
212: needs_update = 0;
213: }
1.7 pazsan 214:
1.12 pazsan 215: do {
216: if(do_bluetooth()) {
217: while(bt_index >= bt_buf[0]) {
218: bt_receive(bt_buf);
219: bt_index = 0;
220: }
221: key = bt_buf[bt_index++];
222: } else if(udp_configured()) {
223: if(udp_index < udp_size) {
224: key = udp_buf[udp_index++];
225: } else {
226: udp_size = udp_read(udp_buf, 0x100);
227: udp_index = 0;
228: key = 0;
229: }
230: } else {
231: key = 0;
232: }
233: } while(!key);
234:
1.6 pazsan 235: display_char(key); display_update();
1.12 pazsan 236:
1.6 pazsan 237: return key;
1.3 pazsan 238: }
239:
1.1 pazsan 240: void emit_char(char x)
241: {
1.12 pazsan 242: char buf[3];
1.3 pazsan 243: if(!terminal_prepped) prep_terminal();
1.12 pazsan 244: if(bt_mode) {
245: buf[0] = 1;
246: buf[1] = 0;
247: buf[2] = x;
248: bt_send(buf, 3);
249: }
1.1 pazsan 250: }
251:
252: void type_chars(char *addr, unsigned int l)
253: {
1.12 pazsan 254: int i;
255: char buf[0x100];
256: if(bt_mode) {
257: buf[0]=l;
258: buf[1]=0;
259: for(i=0; i<l; i++) {
260: buf[2+i]=addr[i];
261: }
262: bt_send(buf, l+2);
263: }
1.1 pazsan 264: }
1.12 pazsan 265:
266: volatile unsigned char gMakeRequest;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>