Skip to content

Commit

Permalink
Merge pull request #1 from IngeniumTeam/commProtocol
Browse files Browse the repository at this point in the history
Comm protocol
  • Loading branch information
SimonPucheu authored Feb 10, 2024
2 parents 62132cf + f145163 commit af4b404
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions src/Bull/Bull.ino
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
#include <SoftwareSerial.h>
#include <Bluetooth.h>
#include <Joystick.h>
#include <Led.h>
#include <Keypad.h>

#define loopTime 20
#define DEBUG false
#define LOOP_TIME 20

#define SWITCH 0
#define KEYPAD 1
#define ESTIMATION 2
#define JOYSTICK_LEFT_X 3
#define JOYSTICK_LEFT_Y 4
#define JOYSTICK_LEFT_CLCK 5
#define JOYSTICK_RIGHT_X 6
#define JOYSTICK_RIGHT_Y 7
#define JOYSTICK_RIGHT_CLCK 8

#define NUM_VALUES 9

// RX TX
SoftwareSerial Serial1(2, 4);
Bluetooth bluetooth(&Serial1);
Joystick leftJoystick(A1, A0, A2, true, false, 512, 512, 50, 50);
Joystick rightJoystick(A4, A3, A5, true, false, 512, 512, 50, 50);
SoftwareSerial Serial1(2, 3);

int sizes[NUM_VALUES] = { 1, 4, 8, 9, 9, 1, 9, 9, 1 };
Bluetooth bluetooth(&Serial1, sizes, NUM_VALUES, '.');
Joystick leftJoystick(A1, A0, A2, true, false, 512, 512, 50, 50, 0, 511);
Joystick rightJoystick(A4, A3, A5, true, false, 512, 512, 50, 50, 0, 511);

Button switcher(A6);
Led blueLed(12);
Led whiteLed(11);
Expand All @@ -30,8 +46,8 @@ void setup ()
{
// Serial setup //
{
Serial.begin(9600);
Serial1.begin(9600);
Serial.begin(9600);
#if DEBUG
Serial.println("Serial communication's on...");
Serial.println("Bluetooth communication's on...");
Expand All @@ -47,18 +63,17 @@ void loop ()
{
// Fetch data to json and send it via bluetooth //
{
bluetooth.json["switch"] = switcher.getAnalogValue() > 512;
if (bluetooth.json["switch"].as<bool>()) {
bluetooth.message.set(SWITCH, switcher.getAnalogValue() > 512);
if (bluetooth.message.get(SWITCH) == 1) {
whiteLed.on();
}
else {
whiteLed.off();
}
const int keypadValue = keypad.getKey();
bluetooth.json["keypad"] = keypadValue;
if (keypadValue == 12) {
bluetooth.message.set(KEYPAD, keypad.getKey());
if (bluetooth.message.get(KEYPAD) == 12) {
redLed.on();
bluetooth.json["keypad"] = 0;
bluetooth.message.set(KEYPAD, 0);
int key = keypad.getKey();
estimation = 0;
while (key != 12 && ((int)log10(estimation) + 1) < 3) {
Expand All @@ -73,24 +88,21 @@ void loop ()
}
redLed.off();
}
bluetooth.json["estimation"] = estimation;
bluetooth.message.set(ESTIMATION, estimation);
{
{
bluetooth.json["joysticks"]["left"]["x"] = leftJoystick.x.getValue();
bluetooth.json["joysticks"]["left"]["y"] = leftJoystick.y.getValue();
bluetooth.json["joysticks"]["left"]["clck"] = leftJoystick.clck.getValue();
bluetooth.message.set(JOYSTICK_LEFT_X, leftJoystick.x.getValue());
bluetooth.message.set(JOYSTICK_LEFT_Y, leftJoystick.y.getValue());
bluetooth.message.set(JOYSTICK_LEFT_CLCK, leftJoystick.clck.getValue());
}
{
bluetooth.json["joysticks"]["right"]["x"] = rightJoystick.x.getValue();
bluetooth.json["joysticks"]["right"]["y"] = rightJoystick.y.getValue();
bluetooth.json["joysticks"]["right"]["clck"] = rightJoystick.clck.getValue();
bluetooth.message.set(JOYSTICK_RIGHT_X, rightJoystick.x.getValue());
bluetooth.message.set(JOYSTICK_RIGHT_Y, rightJoystick.y.getValue());
bluetooth.message.set(JOYSTICK_RIGHT_CLCK, rightJoystick.clck.getValue());
}
}
bluetooth.send();
#if DEBUG
serializeJsonPretty(bluetooth.json, Serial);
#endif
blueLed.on();
}
delay(loopTime);
delay(LOOP_TIME);
}

0 comments on commit af4b404

Please sign in to comment.