Skip to content

Commit

Permalink
Updating bluetooth
Browse files Browse the repository at this point in the history
  • Loading branch information
bungogood committed Sep 9, 2024
1 parent e4a7e6f commit 5fcd710
Show file tree
Hide file tree
Showing 22 changed files with 201 additions and 51 deletions.
Empty file added include/ble.hpp
Empty file.
7 changes: 6 additions & 1 deletion include/bluetooth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

#include <string>

#include "fsm.hpp"

#define SERVICE_UUID "f88918be-312c-4a9b-a7a2-97db83b2e3a9"
#define FEN_CHAR_UUID "82ca99da-f6c3-4eb7-ac2d-ea12cac9af5c"
#define REED_SWITCH_CHAR_UUID "3f7ebdc2-6d63-426f-a2ad-8d10c450743f"
#define STATE_CHAR_UUID "1a100eac-62c1-4c73-955c-55c97f4c57be"
#define MOVE_CHAR_UUID "27dc4447-33d4-4e8a-88fa-c683d4545d47"
#define CMD_CHAR_UUID "b375a4f9-2c61-47ab-a14e-3dbacdb9c25d"

class BluetoothManager {
Expand All @@ -19,13 +22,15 @@ class BluetoothManager {
void begin(const char* deviceName);
void setFEN(const std::string& fen);
void setReedSwitchValue(uint64_t value);
void setFSMState(const std::string& state);
void setFSMState(const FSMState state);
void setMove(const Move move);

private:
BLEServer* pServer = nullptr;
BLEService* pService = nullptr;
BLECharacteristic* fenChar = nullptr;
BLECharacteristic* reedSwitchChar = nullptr;
BLECharacteristic* stateChar = nullptr;
BLECharacteristic* moveChar = nullptr;
// bool deviceConnected = false;
};
1 change: 1 addition & 0 deletions include/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Board {

public:
Board(HBot* hbot, Magnet* magnet);
void init();

void calibrate(int speed = CALIBRATION_SPEED);
void move(std::vector<MoveStep> steps, int speed = DEFAULT_SPEED);
Expand Down
6 changes: 4 additions & 2 deletions include/fsm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "chess.hpp"

enum State {
enum FSMState : int {
Idle,
FriendlyPU,
EnemyPU,
Expand All @@ -15,7 +15,9 @@ enum State {
MoveComplete
};

std::string fsm_state_string(FSMState state);

typedef struct {
int memory[3];
int length;
} State_Memory;
} State_Memory;
1 change: 1 addition & 0 deletions include/hbot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class HBot {

public:
HBot(Motor* leftMotor, Motor* rightMotor, int switchPin);
void init();

bool inBounds(Position position);

Expand Down
1 change: 1 addition & 0 deletions include/indicator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Indicator {
const int blue_pin;

Indicator(int red_pin, int green_pin, int blue_pin);
void init();

void set(Color state);
};
1 change: 1 addition & 0 deletions include/magnet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Magnet {
const Coord offset;

Magnet(int pin, Coord offset);
void init();

void enable();
void disable();
Expand Down
1 change: 1 addition & 0 deletions include/motor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Motor {
const int stepsPerRevolution;

Motor(int enablePin, int dirPin, int stepPin, int stepsPerRevolution);
void init();
void reset();

void enable();
Expand Down
18 changes: 18 additions & 0 deletions include/reed.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <stdint.h>

class Reed {
public:
const int outClk;
const int outData;
const int outLch;
const int inClk;
const int inData;
const int inLch;

Reed(int outClk, int outData, int outLch, int inClk, int inData, int inLch);
void init();

uint64_t read();
};
2 changes: 2 additions & 0 deletions lib/chess/include/chess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ typedef struct {
Move best_move;
} Search_Info;

std::string square_to_string(Square square);

class Chess {
private:
int board_array[129] = { // 0x88 board + centers positional scores
Expand Down
24 changes: 22 additions & 2 deletions lib/chess/src/chess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ std::string Chess::get_fen() {
std::string fen = "";
int empty_count = 0;

for (int i = 0; i < 63; i++) {
for (int i = 0; i < 64; i++) {
char piece = square_char(i);
if (piece == '.') {
empty_count++;
Expand All @@ -564,7 +564,27 @@ std::string Chess::get_fen() {
}
}

// side to move
fen += side == WHITE ? " w" : " b";
// Append castling rights, en passant, etc.

// castling rights
fen += " KQkq";

// en passant
fen += en_passant == 128 ? " -" : " " + square_to_string(en_passant);

// halfmove clock
fen += " 0";

// fullmove number
fen += " 1";

return fen;
}

std::string square_to_string(Square square) {
std::string str = "";
str += 'a' + (square & 7);
str += '1' + 7 - (square >> 4);
return str;
}
5 changes: 5 additions & 0 deletions src/bin/ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
#include <string>

#include "bluetooth.hpp"
#include "indicator.hpp"

// Global variable to store FEN string
std::string startFen =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
std::string otherFen =
"rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2";

Indicator indicator(LED_RED, LED_GREEN, LED_BLUE);

BluetoothManager* ble;
uint64_t reedSwitchValue;

void setup() {
Serial.begin(9600);
ble = new BluetoothManager();
ble->begin("Gambit");
indicator.init();
indicator.set(Color::RED);

reedSwitchValue = 0;
}
Expand Down
16 changes: 6 additions & 10 deletions src/bin/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "bluetooth.hpp"
#include "controller.hpp"
#include "fsm.hpp"

Indicator indicator(LED_RED, LED_GREEN, LED_BLUE);

Expand Down Expand Up @@ -56,14 +57,8 @@ void replay() {
}
}

// BluetoothManager* ble;
uint64_t reedSwitchValue;

std::string currentFEN =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
std::string other =
"rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2";

void setup() {
Serial.begin(9600);
delay(1000); // wait for serial monitor
Expand All @@ -73,22 +68,23 @@ void setup() {

// Serial.println("Starting...");
controller.init();
// controller.calibrate();
controller.calibrate();
ble.setFSMState(FSMState::Idle);
// upMagnet.enable();
// indicator.init();
// Serial.println("Calibrated");
// Move move = {SQD2, SQE3, WPAWN | WHITE, WPAWN, BPAWN | BLACK, SQE3};
// Move move = {SQC3, SQE4, KNIGHT | WHITE, KNIGHT};
// Move move = chess.parse_move("a2a4", WHITE);
// controller.makeMove(move, 500);
// board.gotoSquare(SQB4);
// hbot.gotoPosition({250, 0});
replay();
}

void loop() {
ble.setReedSwitchValue(reedSwitchValue);
reedSwitchValue++;
// ble.setFEN(chess.get_fen());
ble.setFEN(chess.get_fen());
delay(1000);
ble.setFEN(other);
delay(1000);
}
19 changes: 17 additions & 2 deletions src/bluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ void BluetoothManager::begin(const char* deviceName) {
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
stateChar->addDescriptor(new BLE2902());

moveChar = pService->createCharacteristic(
MOVE_CHAR_UUID, BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY);
moveChar->addDescriptor(new BLE2902());

pService->start();

// BLEAdvertising* pAdvertising = BLEDevice::getAdvertising();
Expand All @@ -60,9 +66,18 @@ void BluetoothManager::setReedSwitchValue(uint64_t value) {
}
}

void BluetoothManager::setFSMState(const std::string& state) {
void BluetoothManager::setFSMState(const FSMState state) {
if (stateChar != nullptr) {
stateChar->setValue(state.c_str());
std::string stateStr = fsm_state_string(state);
stateChar->setValue(stateStr.c_str());
stateChar->notify();
}
}

void BluetoothManager::setMove(const Move move) {
if (moveChar != nullptr) {
std::string moveStr = square_to_string(move.source_square) +
square_to_string(move.target_square);
moveChar->notify();
}
}
5 changes: 5 additions & 0 deletions src/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

Board::Board(HBot* hbot, Magnet* magnet) : hbot(hbot), magnet(magnet) {}

void Board::init() {
hbot->init();
magnet->init();
}

void Board::calibrate(int speed) { hbot->calibrate(speed); }

void Board::gotoSquare(Square square, int speed) {
Expand Down
7 changes: 6 additions & 1 deletion src/controller.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include "controller.hpp"

#include <Arduino.h>

Controller::Controller(Board* board, Chess* chess, Indicator* indicator,
BluetoothManager* bluetooth)
: board(board), chess(chess), indicator(indicator), bluetooth(bluetooth) {}

void Controller::init() {
board->init();
indicator->init();
indicator->set(Color::RED);
bluetooth->begin("Gambit");
bluetooth->setFEN(chess->get_fen());
Expand All @@ -21,7 +25,8 @@ void Controller::makeMove(Move move, int speed) {
indicator->set(Color::BLUE);
chess->make_move(move);
bluetooth->setFEN(chess->get_fen());
board->move(steps, speed);
Serial.println(chess->get_fen().c_str());
// board->move(steps, speed);
// should do comparision between chess and matrix
indicator->set(Color::GREEN);
}
Expand Down
Loading

0 comments on commit 5fcd710

Please sign in to comment.