braille_xr/firmware/braille_xr/braille_xr.ino
2024-04-02 14:54:31 -04:00

252 lines
No EOL
3.6 KiB
C++

// esp32 specific
#include <unordered_map>
#include "WiFi.h"
#include "AsyncUDP.h"
#include "wifi_config.h"
AsyncUDP udp;
char in_char = ' ';
#include "ESP32Servo.h"
struct ServoPin {
Servo servo;
int pin;
};
ServoPin servos[6] = {
{Servo(), 14}, {Servo(), 5},
{Servo(), 12}, {Servo(), 4},
{Servo(), 13}, {Servo(), 2} // 19 and 21 are untested
};
// braille alphabet
// example:
// *1 = 180 degrees
// a (0 index)
// 10
// 00
// 00
// braille alphabet array
struct BrailleChar {
int servo_states[6];
};
std::unordered_map<char, BrailleChar> braille_map = {
{' ', {{
0, 0,
0, 0,
0, 0
}}},
{'a', {{
1, 0,
0, 0,
0, 0
}}},
{'b', {{
1, 0,
1, 0,
0, 0
}}},
{'c', {{
1, 1,
0, 0,
0, 0
}}},
{'d', {{
1, 1,
0, 1,
0, 0
}}},
{'e', {{
1, 0,
0, 1,
0, 0
}}},
{'f', {{
1, 1,
1, 0,
0, 0
}}},
{'g', {{
1, 1,
1, 1,
0, 0
}}},
{'h', {{
1, 0,
1, 1,
0, 0
}}},
{'i', {{
0, 1,
1, 0,
0, 0
}}},
{'j', {{
0, 1,
1, 1,
0, 0
}}},
{'k', {{
1, 0,
0, 0,
1, 0
}}},
{'l', {{
1, 0,
1, 0,
1, 0
}}},
{'m', {{
1, 1,
0, 0,
1, 0
}}},
{'n', {{
1, 1,
0, 1,
1, 0
}}},
{'o', {{
1, 0,
0, 1,
1, 0
}}},
{'p', {{
1, 1,
1, 0,
1, 0
}}},
{'q', {{
1, 1,
1, 1,
1, 0
}}},
{'r', {{
1, 0,
1, 1,
1, 0
}}},
{'s', {{
0, 1,
1, 0,
1, 0
}}},
{'t', {{
0, 1,
1, 1,
1, 0
}}},
{'u', {{
1, 0,
0, 0,
1, 1
}}},
{'v', {{
1, 0,
1, 0,
1, 1
}}},
{'w', {{
0, 1,
1, 1,
0, 1
}}},
{'x', {{
1, 1,
0, 0,
1, 1
}}},
{'y', {{
1, 1,
0, 1,
1, 1
}}},
{'z', {{
1, 0,
0, 1,
1, 1
}}},
{',', {{
0, 0,
1, 0,
0, 0
}}},
{'.', {{
0, 0,
1, 1,
0, 1
}}},
{'/', {{
0, 1,
0, 0,
1, 0
}}},
{';', {{
0, 0,
1, 0,
1, 0
}}}
};
void setup() {
Serial.begin(115200);
WiFi.disconnect(true);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
// and print status
Serial.println(WiFi.status());
}
if (udp.listen(1234)) {
Serial.print("UDP Listening on IP: ");
Serial.println(WiFi.localIP());
udp.onPacket([](AsyncUDPPacket packet) {
Serial.print(", Length: ");
Serial.print(packet.length());
Serial.print(", Data: ");
// Serial.write(packet.data(), packet.length());
// that is printing a character instead of a number
// so do this instead
// Serial.print(packet.data()[0]);
// and set the bindex to that value
// in_char = packet.data()[0];
in_char = (char)packet.data()[0];
Serial.print(in_char);
Serial.println();
uint32_t receivedData;
memcpy(&receivedData, packet.data(), sizeof(receivedData));
packet.printf("Got %u bytes of data", packet.length());
});
}
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
for (int i = 0; i < 6; i++) {
servos[i].servo.setPeriodHertz(50);
servos[i].servo.attach(servos[i].pin);
}
}
char old_char = ' ';
void loop() {
// delay(1000);
// udp.broadcast("Anyone here?");
if (in_char != old_char) {
BrailleChar bc = braille_map[in_char];
for (int i = 0; i < 6; i++) {
int state = bc.servo_states[i];
int s = i % 2 == 0 ? state : (1 - state);
servos[i].servo.write(s * 180);
}
old_char = in_char;
}
}