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