-
Notifications
You must be signed in to change notification settings - Fork 0
/
Messages.cpp
89 lines (82 loc) · 2.25 KB
/
Messages.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Messages.cpp
*
* Created on: Sep 15, 2016
* Author: bradmiller
*/
#include "Arduino.h"
#include "Messages.h"
#include "BTComms.h"
BTComms comms;
/**
* Constuctor
* Initialize everything here when the class is created
* Note: you cannot call methods that depend on other classes having already been created
*/
Messages::Messages() {
stopped = false;
}
/**
* Setup class code that is called from the Arduino sketch setup() function. This doesn't
* get called until all the other classes have been created.
*/
void Messages::setup() {
comms.setup();
}
/**
* Check if the field is currently in the "stop" state
* @returns bool value that is true if the robot should be stopped
*/
bool Messages::isStopped() {
return stopped;
}
/**
* Send a heartbeat message to the field to let it know that your code is alive
* This should be called by your robot program periodically, say once per second. This
* timing can easily be done in the loop() function of your program.
*/
void Messages::sendHeartbeat() {
comms.writeMessage(kHeartbeat, 0x0a, 0x00);
}
/**
* Print message for debugging
* This method prints the message as a string of hex numbers strictly for debugging
* purposes and is not required to be called for any other purpose.
*/
void Messages::printMessage() {
for (int i = 0; i < comms.getMessageLength(); i++) {
Serial.print(comms.getMessageByte(i), HEX);
Serial.print(" ");
}
Serial.println();
}
/**
* Read messages from the Bluetooth serial connection
* This method should be called from the loop() function in your arduino code. It will check
* to see if the lower level comms object has received a complete message, and run the appropriate
* code to handle the message type. This should just save the state of the message inside this class
* inside member variables. Then add getters/setters to retrieve the status from your program.
*/
bool Messages::read() {
static int msgNumber = 0;
if (comms.read()) {
switch (comms.getMessageByte(0)) {
case kStorageAvailability:
break;
case kSupplyAvailability:
break;
case kRadiationAlert:
break;
case kStopMovement:
break;
case kResumeMovement:
break;
case kRobotStatus:
break;
case kHeartbeat:
break;
}
return true;
}
return false;
}