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: cmd[i++] = (char)(sum>>8);
66: cmd[i++] = (char)(sum & 0xff);
67:
68: bt_send(cmd, len);
69: }
70:
71: void prep_terminal ()
72: {
73: char cmd[30];
74:
75: aic_initialise();
76: interrupts_enable();
77: systick_init();
78: sound_init();
79: nxt_avr_init();
80: display_init();
81: nxt_motor_init();
82: i2c_init();
83: bt_init();
84: cmd[1] = 3;
85: bt_send_cmd(cmd); // open port query
86:
87: display_goto_xy(0,0);
88: display_clear(1);
89:
90: terminal_prepped = 1;
91: }
92:
93: void deprep_terminal ()
94: {
95: terminal_prepped = 0;
96: }
97:
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:
126: long key_avail ()
127: {
128: if(!terminal_prepped) prep_terminal();
129:
130: if(bt_mode) {
131: return bt_avail();
132: } else {
133: if(bt_avail())
134: do_bluetooth();
135: return 0;
136: }
137: }
138:
139: Cell getkey()
140: {
141: int key;
142:
143: if(!terminal_prepped) prep_terminal();
144:
145: if(needs_update) {
146: display_update();
147: needs_update = 0;
148: }
149:
150: while(!key_avail());
151:
152: return bt_getkey();
153: }
154:
155: void emit_char(char x)
156: {
157: if(!terminal_prepped) prep_terminal();
158: display_char(x);
159: if(x == '\n') {
160: display_update();
161: needs_update = 0;
162: } else
163: needs_update = 1;
164: bt_send(&x, 1);
165: }
166:
167: void type_chars(char *addr, unsigned int l)
168: {
169: int i;
170: for(i=0; i<l; i++)
171: emit_char(addr[i]);
172: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>