-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
70 lines (63 loc) · 1.86 KB
/
main.cpp
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
/* Basic Raw HID Example
Teensy can send/receive 64 byte packets with a
dedicated program running on a PC or Mac.
You must select Raw HID from the "Tools > USB Type" menu
Optional: LEDs should be connected to pins 0-7,
and analog signals to the analog inputs.
This example code is in the public domain.
*/
void setup() {
Serial.begin(9600);
Serial.println(F("RawHID Example"));
for (int i=0; i<7; i++) {
pinMode(i, OUTPUT);
}
}
// RawHID packets are always 64 bytes
byte buffer[64];
elapsedMillis msUntilNextSend;
unsigned int packetCount = 0;
void loop() {
int n;
n = RawHID.recv(buffer, 0); // 0 timeout = do not wait
if (n > 0) {
// the computer sent a message. Display the bits
// of the first byte on pin 0 to 7. Ignore the
// other 63 bytes!
Serial.print(F("Received packet, first byte: "));
Serial.println((int)buffer[0]);
for (int i=0; i<8; i++) {
int b = buffer[0] & (1 << i);
digitalWrite(i, b);
}
}
// every 2 seconds, send a packet to the computer
if (msUntilNextSend > 2000) {
msUntilNextSend = msUntilNextSend - 2000;
// first 2 bytes are a signature
buffer[0] = 0xAB;
buffer[1] = 0xCD;
// next 24 bytes are analog measurements
for (int i=0; i<12; i++) {
int val = analogRead(i);
buffer[i * 2 + 2] = highByte(val);
buffer[i * 2 + 3] = lowByte(val);
}
// fill the rest with zeros
for (int i=26; i<62; i++) {
buffer[i] = 0;
}
// and put a count of packets sent at the end
buffer[62] = highByte(packetCount);
buffer[63] = lowByte(packetCount);
// actually send the packet
n = RawHID.send(buffer, 100);
if (n > 0) {
Serial.print(F("Transmit packet "));
Serial.println(packetCount);
packetCount = packetCount + 1;
} else {
Serial.println(F("Unable to transmit packet"));
}
}
}