-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctrlbox-firmware.c
87 lines (71 loc) · 1.95 KB
/
ctrlbox-firmware.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
/*
ctrlbox-firmware
Copyright 2009, Jonatan Liljedahl
Firmware for Arduino-based USB controller
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with This program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <WProgram.h>
#define BAUDRATE 500000
//#define BAUDRATE 115200
//#define BAUDRATE 57600
int i,v,x = 0;
int accum[6] = {0,0,0,0,0,0};
int dval[10];
int tmr[10];
void setup() {
for(i = 0; i < 10; ++i)
pinMode(i + 2, INPUT);
pinMode(13, OUTPUT);
beginSerial(BAUDRATE);
}
int smooth(int data, int *accum, long filterVal) {
*accum = ((data * filterVal) / 1000) + ((*accum * (1000 - filterVal) / 1000));
return *accum;
}
void loop() {
for(i = 0; i < 6; ++i) {
int old = accum[i];
v = analogRead(i);
v = smooth(v, &accum[i], 100);
if(v != old) {
serialWrite(0x80 | i);
serialWrite(v >> 7);
serialWrite(v & 0x7F);
x = 5;
}
}
for(i = 0; i < 10; ++i) {
v = digitalRead(i + 2);
if(v != dval[i] && tmr[i] == 0) {
serialWrite(0xC0 | i);
serialWrite(v);
tmr[i] = 10;
x = 5;
dval[i] = v;
}
if(tmr[i] > 0) --tmr[i];
}
if(x>1) {
digitalWrite(13, HIGH);
--x;
} else if(x==1) {
digitalWrite(13, LOW);
x = 0;
}
}
int main(void) {
init();
setup();
for (;;)
loop();
return 0;
}