-
Notifications
You must be signed in to change notification settings - Fork 29
/
Receive.cpp
134 lines (117 loc) · 4.67 KB
/
Receive.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
Receive.cpp
Author: Seb Madgwick
*/
//------------------------------------------------------------------------------
// Includes
#include <Arduino.h>
#include "I2CBus.h"
#include "Receive.h"
#include "LEDs.h"
//------------------------------------------------------------------------------
// Definitions
#define LED_PACKET_LENGTH 5
#define VIB_PACKET_LEGNTH 3
#define MISC_PACKET_LEGNTH 10
#define MAX_PACKET_LENGTH 10
//------------------------------------------------------------------------------
// Variables
char Receive::rxBuf[256];
unsigned char Receive::rxBufIndex;
//------------------------------------------------------------------------------
// Methods
void Receive::doTasks() {
while(Serial.available() > 0) {
static bool inSync = false;
static int byteCount = 0;
static int red;
static int blu;
static int grn;
// LEDs::setled(0,255,0);
// Add new byte to buffer
//rxBuf[rxBufIndex++] = Serial.read();
int incomingValues = Serial.read();
//Serial.println(incomingValues);
if(incomingValues >=0 && incomingValues <= 50) analogWrite(10, incomingValues*5);
if(incomingValues >=51 && incomingValues <= 100) red=incomingValues-50;
if(incomingValues >=101 && incomingValues <= 150) grn=incomingValues-100;
if(incomingValues >=151 && incomingValues <= 200) blu=incomingValues-150;
LEDs::setled(red*5,grn*5,blu*5);
}
}
// // Check if out of sync
// if (++byteCount > MAX_PACKET_LENGTH) {
// byteCount = MAX_PACKET_LENGTH; // prevent overflow
// inSync = false;
// }
//
// // Decode LED packet
// if (rxBufIndex >= LED_PACKET_LENGTH) {
//--
// if (calcChecksum(LED_PACKET_LENGTH) == 0) {
// unsigned char red = (unsigned char)rxBuf[rxBufIndex - (LED_PACKET_LENGTH - 1)];
// unsigned char green = (unsigned char)rxBuf[rxBufIndex - (LED_PACKET_LENGTH - 2)];
// unsigned char blue = (unsigned char)rxBuf[rxBufIndex - (LED_PACKET_LENGTH - 3)];
// I2CBus::setRGB(red, green, blue);
// rxBufIndex = 0;
// byteCount = 0;
// inSync = true;
// }
// }
// }
//
// // Decode vibration motor packet
// if (rxBufIndex >= VIB_PACKET_LEGNTH) {
// if ((inSync ? rxBuf[0] : rxBuf[rxBufIndex - VIB_PACKET_LEGNTH]) == 'V') {
// if (calcChecksum(VIB_PACKET_LEGNTH) == 0) {
// char value = (unsigned char)rxBuf[rxBufIndex - (VIB_PACKET_LEGNTH - 1)];
// //Serial.println(value);
// // TODO: process data
// pinMode(8,OUTPUT);
//
// if(value == 0) {
// digitalWrite(8, LOW);
// }
// else {
// digitalWrite(8, HIGH);
// }
// rxBufIndex = 0;
// byteCount = 0;
// inSync = true;
// }
// }
// }
//
//
//
// // Decode misc packet
// if (rxBufIndex >= MISC_PACKET_LEGNTH) {
// if ((inSync ? rxBuf[0] : rxBuf[rxBufIndex - MISC_PACKET_LEGNTH]) == 'M') {
// if (calcChecksum(MISC_PACKET_LEGNTH) == 0) {
// char charArray[8];
// charArray[0] = (unsigned char)rxBuf[rxBufIndex - (MISC_PACKET_LEGNTH - 1)];
// charArray[1] = (unsigned char)rxBuf[rxBufIndex - (MISC_PACKET_LEGNTH - 2)];
// charArray[2] = (unsigned char)rxBuf[rxBufIndex - (MISC_PACKET_LEGNTH - 3)];
// charArray[3] = (unsigned char)rxBuf[rxBufIndex - (MISC_PACKET_LEGNTH - 4)];
// charArray[4] = (unsigned char)rxBuf[rxBufIndex - (MISC_PACKET_LEGNTH - 5)];
// charArray[5] = (unsigned char)rxBuf[rxBufIndex - (MISC_PACKET_LEGNTH - 6)];
// charArray[6] = (unsigned char)rxBuf[rxBufIndex - (MISC_PACKET_LEGNTH - 7)];
// charArray[7] = (unsigned char)rxBuf[rxBufIndex - (MISC_PACKET_LEGNTH - 8)];
// // TODO: process data
// rxBufIndex = 0;
// byteCount = 0;
// inSync = true;
// }
// }
// }
// }
char Receive::calcChecksum(const int packetLength) {
unsigned char tempRxBufIndex = rxBufIndex - packetLength;
unsigned char checksum = 0;
while(tempRxBufIndex != rxBufIndex) {
checksum ^= rxBuf[tempRxBufIndex++];
}
return checksum;
}
//------------------------------------------------------------------------------
// End of file