198 lines
5.9 KiB
C++

//======================================================
// LIBRARIES
//======================================================
#include <ArduinoJson.h>
#include <TimeTrigger.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
//======================================================
// JSON STRINGS AND DOCUMENT
//======================================================
// FIXED MESSAGES
#define POWERUP "{\"status\":\"POWER UP\"}"
#define UNDERSTOOD "{\"status\":\"OK\"}"
#define JSON_ERROR "{\"status\":\"ERROR JSON\"}"
#define TARGET_MET "{\"status\":\"TARGET MEET\"}"
#define GAME_OVER "{\"status\":\"GAME OVER\"}"
#define TIME_OVER "{\"status\":\"TIME OVER\"}"
#define UNKNOWN_GAME "{\"status\":\"UNKNOWN GAME\"}"
// GAME NAMES
#define GM_COUNTER "{\"gm\":\"COUNTER\"}"
#define GM_TATTACK "{\"gm\":\"TIME ATTACK\"}"
#define GM_REACTION "{\"gm\":\"REACTION\"}"
#define GM_DECISION "{\"gm\":\"DECISION\"}"
#define GM_STAMINA "{\"gm\":\"STAMINA\"}"
// COMMAND LIST
#define CM_START "{\"cm\":\"START\"}"
#define CM_RESET "{\"cm\":\"RESET\"}"
#define CM_STOP "{\"cm\":\"STOP\"}"
#define CM_EXIT "{\"cm\":\"EXIT\"}"
// JSON CAPACITY
#define CAPACITY 100
// Public JSON Document
DynamicJsonDocument JSON(CAPACITY); // Building JSON Buffer
//======================================================
// HARDWARE SETUP & SELECTION
//======================================================
// Arduino Hardware Serial Port
#define Terminal Serial // SERIAL PORT USED AS TERMINAL WINDO FOR DEBUG
// The Integer Value Of The Strike Is Exceeded Accept As A Point
#define VTH 14 // THRESHOLD VOLTAGE FOR ZERO POSITIONNING KICKING POWER SENSOR
// Number Of LED's In The Neo Pixel Strip
#define NUM_PIXELS 14 // NUMBER OF LED'S IN NEO PIXEL STRIP
// Incase You Are Using Arduino MEGA
#ifdef ARDUINO_AVR_MEGA2560
#define Bluetooth Serial1 // We will Connect The HC-05 To Hardware Serial1 In Arduino Mega
#define LED_STRIP 53 // LED STRIP PIN NUMBER
#define SENSOR_PIN A0 // IMPACT SENSOR
#define RANDOM_SOURCE A7 // RANDOM NUMBER GENERATOR SOURCE
// Incase You Are Using Arduino Nano
#elif ARDUINO_AVR_UNO || ARDUINO_AVR_NANO
//----------------------------------------------------------
/* ARDUINO NANO DOESNOT HAVE A SECOND NATIVE
SERIAL PORT SO WE USE SOFTWARE SERIAL LIBRARY */
#include <SoftwareSerial.h>
//----------------------------------------------------------
SoftwareSerial Bluetooth(10, 11); // Pin 10 RX, Pin 11 TX
#define LED_STRIP 2 // LED STRIP PIN NUMBER
#define SENSOR_PIN A0 // IMPACT SENSOR
#define RANDOM_SOURCE A4 // RANDOM NUMBER GENERATOR SOURCE
// if This Was The ESP32 Core
#elif ESP32
//----------------------------------------------------------
#include <BluetoothSerial.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
//----------------------------------------------------------
BluetoothSerial Bluetooth; // Here We Use the Native Bluetooth In The ESP32
#define LED_STRIP 33 // LED STRIP PIN NUMBER
#define SENSOR_PIN 39 // IMPACT SENSOR
#define RANDOM_SOURCE 34 // RANDOM NUMBER GENERATOR SOURCE
// End Of Arduino Selection ARDUINO_MEGA
#endif
// NEO PIXEL STRIP
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_PIXELS, LED_STRIP, NEO_GRB + NEO_KHZ800);
//======================================================
// DEFINITION & DECLERATIONS
//======================================================
// Serial Flags
#define isBluetooth Bluetooth.available()
#define isTerminal Terminal.available()
//======================================================
// READING IMPACT POWER FROM ANALOG PINS
//======================================================
// READING THE IMPACT
int readImpact()
{
int value = analogRead(SENSOR_PIN);
return value;
}
//======================================================
// MAIN HARDWARE SETUP
//======================================================
void setup()
{
// Communications
Terminal.begin(9600);
// Make Sure Of The Core
#ifdef ESP32
Bluetooth.begin("KICKER"); //Bluetooth device name
#else
Bluetooth.begin(9600);
#endif
// I/O
pixels.begin(); // Neo Pixel
LED_CLEAR(); // Turn Off All LED
// The Random Number Source
randomSeed(analogRead(RANDOM_SOURCE));
// POWER UP MESSGAE
dualcomm(POWERUP);
// DISPLAY COMPANY COUNTRY FLAG
LED_BAHRAIN_FLAG(); // Bahrain Flag
LED_COLOMBIA_FLAG(); // Colombia Flag
// DisJSON Rainbow
LED_CLEAR();
}
//======================================================
// MAIN PROGRAM LOOP
//======================================================
void loop()
{
// Reading From Bluetooth
if(Bluetooth.available())
{
// Reading Message From Bluetooth
String msg = Bluetooth.readStringUntil('\n');
msg.trim();
// DisJSON On Sertial
terminal(msg);
// Deserialize the JSON document
DeserializationError error = deserializeJson(JSON, msg);
// If The System Understood The Message Clearly
if(!error)
{
// Sending Status Message
dualcomm(UNDERSTOOD);
// Reading Game Name & Settings
String game = JSON["gm"];
String settings = JSON["set"];
// Clearing JSON Buffer
JSON.clear();
// Entering To Game Selector
game_selector(game, settings);
}
else
{
terminal(JSON_ERROR);
}
Bluetooth.flush();
}
}
//======================================================
// END OF PROGRAM
//======================================================