/
kitt.c
218 lines (182 loc) · 4.69 KB
/
kitt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* LED demo for Teensy USB Development Board
* Portions Copyright (c) 2008, 2010 PJRC.COM, LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdint.h>
#include <util/delay.h>
#include "print.h"
#include "string.h"
#include "usb_serial.h"
#include "kitt.h"
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
/* Enumerate the LEDs by their port numbers (0-7) */
#define FIRST_LED 0
#define LAST_LED 7
#define LED_COUNT (LAST_LED - FIRST_LED + 1)
// #define LED_DELAY 50
int LED_DELAY = 50;
int main(void)
{
configure_teensy();
configure_LEDs();
configure_usb();
int16_t lastcmd = '\0', cmd = '\0';
while (1)
{
wait_for_usb_connection();
while (1)
{
cmd = usb_serial_getchar();
if(is_valid_control_key(cmd))
{
if(cmd == '+') adjust_speed(-10); // Speed up == less time.
else if(cmd == '-') adjust_speed(10);
}
if( is_valid_cmd(cmd) )
{
lastcmd = cmd;
}
if(lastcmd == 'k') knight_rider();
else if(lastcmd == 's') step();
else if(lastcmd == 'f') follow();
else if(lastcmd == '0') nop();
else demo();
}
}
}
void adjust_speed(unsigned delta)
{
unsigned new_speed = LED_DELAY + delta;
if( new_speed > 1 && new_speed < 1000 ) // Arbitrary boundaries for now.
{
LED_DELAY = new_speed;
}
}
void configure_LEDs()
{
for (int i = 0; i < LED_COUNT; i++) led_config(i + FIRST_LED);
}
void wait_for_usb_connection()
{
// wait for the user to run their terminal emulator program
// which sets DTR to indicate it is ready to receive.
while (!(usb_serial_get_control() & USB_SERIAL_DTR)) /* wait */ ;
}
void configure_teensy()
{
// set for 16 MHz clock, and make sure the LED is off
CPU_PRESCALE(0);
// normal mode, 2 MHz, count to 2^16 and roll over
TCCR1A = 0x00;
TCCR1B = 0x02;
TCCR1C = 0x00;
}
void configure_usb()
{
// initialize the USB, and then wait for the host
// to set configuration. If the Teensy is powered
// without a PC connected to the USB port, this
// will wait forever.
usb_init();
while (!usb_configured()) /* wait */ ;
_delay_ms(1000);
}
int is_valid_cmd(int16_t cmd)
{
return (cmd == 'k' || cmd == 's' || cmd == 'f' || cmd == 'd' || cmd == '0');
}
int is_valid_control_key(int16_t cmd)
{
return (cmd == '+' || cmd == '-');
}
void nop()
{
return;
}
void knight_rider()
{
int i;
for (i = 0; i < LED_COUNT; i++) {
led_on(i + FIRST_LED);
_delay_ms(LED_DELAY);
led_off(i + FIRST_LED);
}
for (i = 0; i < LED_COUNT; i++) {
led_on(LAST_LED - i);
_delay_ms(LED_DELAY);
led_off(LAST_LED - i);
}
}
void demo()
{
for (int i = 0; i < (LED_COUNT / 2); i++) {
led_on(FIRST_LED + i);
led_on(LAST_LED - i);
_delay_ms(LED_DELAY);
led_off(FIRST_LED + i);
led_off(LAST_LED - i);
}
}
void step()
{
for (int i = 0; i < LED_COUNT; i++) {
led_on(i + FIRST_LED);
_delay_ms(LED_DELAY);
led_off(i + FIRST_LED);
}
}
int prev(int position)
{
if(position < 1) return 7;
return position - 1;
}
int next(int position)
{
if(position > 6) return 0;
return position + 1;
}
void follow()
{
for (int i = 0; i < LED_COUNT; i++) {
int current = i + FIRST_LED;
led_on(prev(current));
led_on(current);
led_on(next(current));
_delay_ms(LED_DELAY);
led_off(prev(current));
led_off(current);
led_off(next(current));
}
}
void led_config(int port)
{
DDRD |= (1<<port);
led_off(port);
}
void led_on(int port)
{
PORTD |= (1<<port);
}
void led_off(int port)
{
PORTD &= ~(1<<port);
}