-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBofu.h
137 lines (111 loc) · 3.02 KB
/
Bofu.h
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
134
135
136
137
#ifndef BOFU_H
#define BOFU_H
#include <Arduino.h>
#include <stdint.h>
/**
* @brief Protocol Definition
* Pulse lengths, and command size
*/
#define AGC1_PULSE 4885 // 216 samples
#define AGC2_PULSE 2450 // 108 samples
#define AGC3_PULSE 1700 // 75 samples
#define RADIO_SILENCE 5057 // 223 samples
#define PULSE 340 // 15 samples
#define MESSAGE_BIT_LENGTH 41
#define MESSAGE_TIMINGS_LENGTH MESSAGE_BIT_LENGTH * 2 + 4
#define BUFFER_SIZE 20
namespace Bofu {
enum class Command: uint16_t {
UP_SINGLE = 0x10C0,
UP_HOLD = 0x00C0,
DOWN_SINGLE = 0x1010,
DOWN_HOLD = 0x0010,
STOP = 0x0150,
PAIR = 0x1140,
LIMIT = 0x0120,
CHANGE_DIR_UP = 0x0190,
CHANGE_DIR_DOWN = 0x01A0
};
enum class Channel: uint8_t {
ONE =0x01,
TWO =0x02,
THREE =0x03,
FOUR =0x04,
FIVE =0x05,
ALL =0x0F
};
String toString(Command cmd);
String toString(Channel ch);
class Message {
public:
Message();
Message(uint32_t message);
Message(uint32_t message, uint8_t checksum);
Message(uint16_t remote_id, Channel channel, Command command);
uint32_t getData();
uint16_t getRemoteID();
void setRemoteID(uint16_t remote_id);
Channel getChannel();
void setChannel(Channel channel);
Command getCommand();
void setCommand(Command command);
uint8_t getChecksum();
void recomputeChecksum();
bool validateChecksum();
static uint8_t calculateChecksum(uint32_t message);
private:
uint32_t data;
uint8_t checksum;
};
class Transmit {
public:
Transmit();
void setPin(int pin);
void setRepeat(int repeat);
void sendMessage(Message message);
private:
int pin;
int repeat;
void send(uint8_t status, int delay);
void sendBit(uint8_t bit);
void sendOnce(Message message);
void agc();
};
class MessageBuffer {
public:
MessageBuffer();
void enqueue(Message message);
Message dequeue();
bool isEmpty();
bool hasOverflowed();
private:
Message data[BUFFER_SIZE];
int front;
int back;
bool overflow;
};
class Receive {
public:
static void setPin(int pin);
static void setTimeout(unsigned int timeout);
// DEBUG
static int getAGCCount();
static unsigned int * getTimings();
static int getTimingsLength();
static void startListening();
static void stopListening();
static bool available();
static Message readMessage();
private:
Receive() = delete;
static int pin;
static unsigned int timeout;
static unsigned int agc_count;
volatile static int change_count;
static unsigned int timings[MESSAGE_TIMINGS_LENGTH];
static MessageBuffer messages;
static Message parseTimings();
static void handleInterrupt();
};
}
#endif