Skip to content

Commit

Permalink
Working bluetooth mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
cvanelteren committed Jan 7, 2022
1 parent 5725de2 commit b9851dc
Show file tree
Hide file tree
Showing 7 changed files with 519 additions and 174 deletions.
17 changes: 3 additions & 14 deletions src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

// --- Configuration ---
// TODO: tmp defs to use the features
// #define USE_SLEEP 1
// #define USE_SLEEP
#define USE_ENCODER
#define USE_LED
#define USE_OLED
Expand Down Expand Up @@ -106,19 +106,8 @@ class Config { // see constructor in cpp file
uint8_t rot_encoder_steps = 4;
uint8_t rot_accel = 250;
// change this for your device
// const char *server_address = "80:7d:3a:d4:2e:46";
// uint8_t serv_add[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// uint8_t client_add[6] = {0x80, 0x7D, 0x3A, 0xD4, 0x2E, 0x44};
// uint8_t serv_add[6] = {0x80, 0x7D, 0x3A, 0xD4, 0x2E, 0x44};
uint8_t serv_add[6] = {0x80, 0x7D, 0x3A, 0xD4, 0x2C, 0x9C};
// uint8_t client_add[6] = {0x80, 0x7D, 0x3A, 0xD4, 0x2C, 0x9C};
// uint8_t client_add[6] = {0x80, 0x7D, 0x3A, 0xD4, 0x2C, 0x9C};
// uint8_t serv_add[6] = {0x80, 0x7D, 0x3A, 0xD4, 0x2C, 0x9C};
// uint8_t serv_add[6] = {0x7C, 0x9E, 0xBD, 0xFB, 0xDA, 0xD4};
uint8_t client_add[6] = {0x7C, 0x9E, 0xBD, 0xFB, 0xD9, 0x78};
// uint8_t client_add[6] = {0x7C, 0x9E, 0xBD, 0xFB, 0xDA, 0xD4};
// uint8_t serv_add[6] = {0x7C, 0x9E, 0xBD, 0xFB, 0xDA, 0xD4};
// uint8_t serv_add[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// uint8_t serv_add[6] = {0x7C, 0x9E, 0xBD, 0xFB, 0xDA, 0xD4};
uint8_t serv_add[6] = {0x80, 0x7D, 0x3A, 0xD4, 0x2C, 0x9E};
// uint8_t serv_add[6] = {0x7C, 0x9E, 0xBD, 0xFB, 0xD9, 0x7A};
};
#endif
141 changes: 85 additions & 56 deletions src/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,14 @@

#include <unordered_map>

Keyboard::~Keyboard() {
delete config;
delete matrix;
delete mesh;
delete display;
delete manager;
};

Keyboard::Keyboard(Config *config) {
this->config = config;
// init the keyboard
matrix = new Matrix(config);
mesh = new Mesh(config);
// setup display
display = new Display(config);
led = new LED(config);
manager = new EventManager();

byte mac_addr[6];
WiFi.macAddress(mac_addr);
this->is_server = true;
for (int idx = 0; idx < 6; idx++) {
if (mac_addr[idx] != config->serv_add[idx]) {
this->is_server = false;
break;
}
}

// default to client
if (this->is_server) {
Serial.println("Setting up bluetooth");
this->bluetooth = BleKeyboard(
config->device_name, config->device_manufacturer, get_battery_level());
}
this->encoder_codes = {
{"LEFT",
{{"UP", &KEY_MEDIA_VOLUME_UP}, {"DOWN", &KEY_MEDIA_VOLUME_DOWN}}},
{"RIGHT",
{{"UP", &KEY_MEDIA_VOLUME_DOWN}, {"DOWN", &KEY_MEDIA_VOLUME_UP}}}};

layer_t qwerty = {{KC_ESC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8,
KC_9, KC_0, KC_BSPC},
Expand Down Expand Up @@ -78,24 +52,44 @@ Keyboard::Keyboard(Config *config) {
KC_NO}};

this->layers = {qwerty, program};
// this->layers.push_back(qwerty);
// this->layers.push_back(qwerty);
this->set_active_layer(0);

// setup rotary encoder
this->rotary_encoder = new RotaryEncoder(config);
this->encoder_codes = {
{"LEFT",
{{"UP", &KEY_MEDIA_VOLUME_UP}, {"DOWN", &KEY_MEDIA_VOLUME_DOWN}}},
{"RIGHT",
{{"UP", &KEY_MEDIA_VOLUME_DOWN}, {"DOWN", &KEY_MEDIA_VOLUME_UP}}}};
// this->ble = BleKeyboard(config->name);

// this->fb = std::vector<uint8_t>(this->display->getDisplayHeight() *
// this->display->getDisplayWidth());

// this->fb = std::vector<uint8_t>(64 / 7 * 128 / 14);
}
layer_t *Keyboard::get_active_layer() { return active_layer; }
uint8_t Keyboard::get_active_layer_num() { return active_layer_num; }

// these are redundant :P just in case
// Keyboard &Keyboard::operator=(const Keyboard &other) {
// if (&other == this)
// return *this;

// std::swap(this, other);
// // reset all properties
// delete config;
// delete matrix;
// delete mesh;
// delete display;
// delete manager;
// delete active_layer;

// config = other.config;
// matrix = other.matrix;
// mesh = other.mesh;
// display = other.display;
// manager = other.manager;
// is_server = other.is_server;
// active_layer = other.get_active_layer();
// layers = other.layers;
// active_layer_num = other.get_active_layer_num();
// return *this;
// }

Keyboard::~Keyboard() {
delete config;
delete matrix;
delete mesh;
delete display;
delete manager;
};

auto *font = u8g2_font_7x14B_mr;
// start the keyboard
Expand Down Expand Up @@ -146,14 +140,50 @@ double Keyboard::get_battery_level() {
}

void Keyboard::begin() {
matrix = new Matrix(config);
// setup display
display = new Display(config);
led = new LED(config);
manager = new EventManager();

std::string servAddr = BLEAddress(config->serv_add).toString();
printf(BLEDevice::getAddress().toString().c_str());
printf("\n");
printf(servAddr.c_str());
printf("\n%d\n", BLEDevice::getAddress().toString().compare(servAddr));

// // only hub for non-servers
if (BLEDevice::getAddress().toString().compare(servAddr) == 0) {
is_server = true;
} else {
is_server = false;
}

// default to client
if (this->is_server) {
Serial.println("Setting up bluetooth");
this->bluetooth = BleKeyboard(
config->device_name, config->device_manufacturer, get_battery_level());
}

mesh = new Mesh(config);

// setup rotary encoder
this->rotary_encoder = new RotaryEncoder(config);

Serial.println("Starting keyboard");
Serial.println(printf("Am I a server? %d\n", this->is_server));
Serial.print("MAC address is: ");
for (auto hex_num : WiFi.macAddress()) {
Serial.print(hex_num);
}
Serial.println();

// start bluetooth when server
if (this->is_server) {
printf("Starting bluetooth\n");
bluetooth.begin();
bluetooth.releaseAll();
}
led->begin();
mesh->begin();
rotary_encoder->begin();
Expand All @@ -170,13 +200,6 @@ void Keyboard::begin() {
display->log.begin(*this->display, width, height,
&(this->display->log_buffer)[0]);
}

// start bluetooth when server
if (this->is_server) {
printf("Starting bluetooth\n");
bluetooth.begin();
bluetooth.releaseAll();
}
}

void Keyboard::keep_active(keyswitch_t &keyswitch) {
Expand Down Expand Up @@ -449,11 +472,17 @@ void Keyboard::update() {
// handle server
if (this->is_server) {
this->process_keyswitches();
this->mesh->update();
}

// handle client
else {
if (this->matrix->active_keys.size()) {
this->mesh->send(this->matrix->active_keys);
printf("Mesh sending\n");
for (auto &elem : matrix->active_keys) {
printf("%d %d\n", elem.col, elem.row);
}
mesh->send(matrix->active_keys, mesh->message_characteristic);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/keyboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class Keyboard {

Keyboard(Config *config);
~Keyboard();
Keyboard &operator=(const Keyboard &other);
void begin();
void update();
// communicate with bluetooth
Expand All @@ -201,6 +202,8 @@ class Keyboard {
layers_t layers;
void sleep();
void wakeup();
layer_t *get_active_layer();
uint8_t get_active_layer_num();

private:
layer_t *active_layer;
Expand Down
3 changes: 0 additions & 3 deletions src/keyboard_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ bool set_led_cycle() {
bool increase_led_brightness() {
return keyboard.led->increase_brightness();
// TODO: add mesh communication
// msg_t msg;
// msg.kind = EVENT;
// msg.event = keyboard.mesh->send(msg)
}
bool decrease_led_brightness() {
return keyboard.led->decrease_brightness();
Expand Down
Loading

0 comments on commit b9851dc

Please sign in to comment.