-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab2.5.cpp
More file actions
51 lines (39 loc) · 1021 Bytes
/
lab2.5.cpp
File metadata and controls
51 lines (39 loc) · 1021 Bytes
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
#include "mbed.h"
#include "ledutils.h"
DigitalOut led1(LED1);
RawSerial serial(SERIAL_TX, SERIAL_RX, 115200);
RGBPwmOut rgbLed(D9, D11, D12);
DigitalIn btn(D8, PullUp);
CAN can(D10, D2); // RX, TX
int main() {
// LED Blink State
Timer ledTimer; // used to track blink length
ledTimer.start();
// Button edge detection
bool lastButton = true;
// Initialize CAN controller at 1 Mbaud
can.frequency(1000000);
can.reset();
while (true) {
// CAN receive handling
CANMessage msg;
while (can.read(msg)) {
if (msg.id == 0x43) {
uint16_t hue = (msg.data[0] << 8) | (msg.data[1] << 0);
rgbLed.hsv_uint16(hue, 65535, 32767);
} else if (msg.id == 0x42) {
led1 = 1;
ledTimer.reset();
}
}
// System actions
if (ledTimer.read_ms() >= 500) {
led1 = 0;
}
bool thisButton = btn;
if (thisButton != lastButton && btn == 0) {
can.write(CANMessage(0x42, (char*)NULL, 0));
}
lastButton = thisButton;
}
}