-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshocker.ino
161 lines (127 loc) · 4.06 KB
/
shocker.ino
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <cstdint>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CMD_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define RF_TX 12
#define LIGHT 2
#define MSG_REPEAT_COUNT 6
#define RMT_BIT_COUNT 44
#define CMD_SHOCK 0x01
#define CMD_VIBRATE 0x02
#define CMD_SOUND 0x03
// Measured values:
// Preamble: 1400us HIGH, 800us LOW
// One : 700us HIGH, 300us LOW
// Zero : 200us HIGH, 800us LOW
const rmt_data_t kRmtPreamble = {1400, 1, 800, 0}; // FC = 11111000
const rmt_data_t kRmtOne = {700, 1, 300, 0}; // E = 1110
const rmt_data_t kRmtZero = {200, 1, 800, 0}; // 8 = 1000
// RF PROTOCOL CONSTANTS
const int txId = 0xEE;
const int channelId = 1;
BLEServer *pServer;
BLEService *pService;
BLECharacteristic *pCharacteristic;
bool cmdReceived;
rmt_data_t rmt_cmd[RMT_BIT_COUNT];
class ServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
};
void onDisconnect(BLEServer* pServer) {
delay(100);
pServer->startAdvertising();
}
};
constexpr std::uint8_t checksum(const std::uint32_t payload) {
std::uint8_t* data = (std::uint8_t*)&payload;
std::uint8_t checksum = 0;
checksum += data[0];
checksum += data[1];
checksum += data[2];
checksum += data[3];
return checksum;
}
void encodeCommand(std::uint64_t data) {
rmt_cmd[0] = kRmtPreamble;
for (std::int64_t b = 0; b < 40; b++) {
int bit_pos = 39-b;
rmt_cmd[b+1] = (data >> bit_pos) & 1 ? kRmtOne : kRmtZero;
}
rmt_cmd[41] = kRmtZero;
rmt_cmd[42] = kRmtZero;
rmt_cmd[43] = kRmtZero;
}
void makeTxSequence(int command, int strength) {
switch (command) {
case CMD_SHOCK:
strength = std::min(strength, 10);
break;
case CMD_VIBRATE:
strength = std::min(strength, 99);
break;
case CMD_SOUND:
strength = 0;
break;
default:
return;
}
// Payload layout: [transmitterId:16][channelId:4][type:4][intensity:8]
std::uint64_t payload = (static_cast<std::uint32_t>(txId & 0xFFFF) << 16) |
(static_cast<std::uint32_t>(channelId & 0xF) << 12) |
(static_cast<std::uint32_t>(command & 0xF) << 8) |
(static_cast<std::uint32_t>(strength & 0xFF) << 0);
// Calculate the checksum of the payload
std::uint8_t cs = checksum(payload);
payload = (static_cast<std::uint64_t>(payload) << 8) | static_cast<std::uint64_t>(cs);
encodeCommand(payload);
}
class CmdCallback: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
String value = pCharacteristic->getValue();
if (value.length() == 2) {
makeTxSequence((int)value[0], (int)value[1]);
cmdReceived = true;
}
}
};
void setup() {
Serial.begin(115200);
Serial.println("Starting");
BLEDevice::init("ShockerBoard");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new ServerCallbacks());
pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pChar;
pChar = pService->createCharacteristic(CMD_UUID, BLECharacteristic::PROPERTY_WRITE);
pChar->setCallbacks(new CmdCallback());
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
pinMode(RF_TX, OUTPUT);
pinMode(LIGHT, OUTPUT);
digitalWrite(RF_TX, LOW);
digitalWrite(LIGHT, LOW);
if (!rmtInit(RF_TX, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 1000000)) { // 1us tick
Serial.println("Initialisation Failed");
return;
}
Serial.println("Initialisation Complete");
}
void loop() {
if (cmdReceived) {
for (int c=0; c<MSG_REPEAT_COUNT; c++) {
rmtWrite(RF_TX, rmt_cmd, RMT_BIT_COUNT, 1000000);
}
cmdReceived = false;
delay(1000);
digitalWrite(LIGHT, HIGH);
delay(200);
digitalWrite(LIGHT, LOW);
}
}