-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab2.6.cpp
More file actions
83 lines (67 loc) · 1.78 KB
/
lab2.6.cpp
File metadata and controls
83 lines (67 loc) · 1.78 KB
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
#include "mbed.h"
#include "rtos.h"
#include "ledutils.h"
RGBPwmOut rgbLed(P0_5, P0_6, P0_7);
DigitalOut led1(P0_3);
DigitalOut led2(P0_9);
DigitalIn btn(P0_4);
RawSerial serial(P0_8, NC, 115200);
CAN can(P0_28, P0_29); // RX, TX
Mail<CANMessage, 16> canTransmitQueue;
Thread ledThread;
Mail<uint16_t, 1> ledQueue;
void led_thread() {
while (true) {
osEvent evt = ledQueue.get();
if (evt.status == osEventMail) {
uint16_t waitTime = *(uint16_t*)evt.value.p;
ledQueue.free((uint16_t*)evt.value.p);
led2 = 1;
Thread::wait(waitTime);
led2 = 0;
}
}
}
Thread buttonThread;
void button_thread() {
bool lastButton = true;
while (true) {
bool thisButton = btn;
if (thisButton != lastButton && btn == 0) {
CANMessage* msg = canTransmitQueue.alloc(osWaitForever);
*msg = CANMessage(0x42, NULL, 0);
canTransmitQueue.put(msg);
}
lastButton = thisButton;
Thread::wait(5);
}
}
int main() {
// Initialize CAN controller at 1 Mbaud
can.frequency(1000000);
ledThread.start(led_thread);
buttonThread.start(button_thread);
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) {
uint16_t* waitTime = ledQueue.alloc(osWaitForever);
*waitTime = 500;
ledQueue.put(waitTime);
}
}
// CAN transmit handling
osEvent evt = canTransmitQueue.get(0);
while (evt.status == osEventMail) {
CANMessage msg = *(CANMessage*)evt.value.p;
canTransmitQueue.free((CANMessage*)evt.value.p);
can.write(msg);
evt = canTransmitQueue.get(0);
}
Thread::yield();
}
}