Loaded Every Thing From My Work

This commit is contained in:
ghassan committed 2020-01-28 08:42:25 +03:00
1 parent 4d81b2687b
commit 3229659f4e
176 files changed
+9623

No files matched your search

+258
View File
@@ -0,0 +1,258 @@
//======================================================
// LIBRARIES
//======================================================
#include <Button.h>
#include <ArduinoJson.h>
#include <TimeTrigger.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// MODE SELECTION VARIABLES
int mode;
bool flagChange = false;
// 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 JSON_CAPACITY
#define JSON_CAPACITY 500
//======================================================
// HARDWARE SELECTION
//======================================================
#define TERMINAL Serial // SERIAL PORT USED AS TERMINAL WINDO FOR DEBUG
#define VTH 65 // THRESHOLD VOLTAGE FOR ZERO POSITIONNING KICKING POWER SENSOR
#define PWR 65 // MIN THRESHOLD VOLTAGE FROM ZERO POINT TO BE ACCOUNTED AS KICK
#define NUM_PIXELS 22 // NUMBER OF LED'S IN NEO PIXEL STRIP
#define MPU_ADD 0x69 // On Board MPU9250 I2C Address
#define OLED_ADD 0x3C // Connected I2C OLED Address
// Incase You Are Using Arduino MEGA
#ifdef ARDUINO_AVR_MEGA2560
#define BLUETOOTH Serial1
Button button(52); // BUTTON ON PIN 2
#define LED_STRIP 53 // LED STRIP PIN NUMBER
#define SENSOR_PIN A0 // IMPACT SENSOR
#define RANDOM_SOURCE A2 // 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); // RX, TX
Button button(2); // BUTTON ON PIN 2
#define LED_STRIP 8 // LED STRIP PIN NUMBER
#define SENSOR_PIN A0 // IMPACT SENSOR
#define RANDOM_SOURCE A3 // 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
//----------------------------------------------------------
// BLUETOOTH DEFINITION
BluetoothSerial BLUETOOTH;
// BUTTONS
Button button(2); // BUTTON ON PIN 2
// PIEZO SENSOR PIN
#define SENSOR_PIN 39 // IMPACT SENSOR
// LED STRIP PIN SET
#define LED_STRIP 26 // LED STRIP PIN NUMBER
#define RANDOM_SOURCE 34 // RANDOM NUMBER GENERATOR SOURCE
// Battery To 5V Level Booster Setup & Control
#define BOOST_PIN 14
#define BOOST_SETUP pinMode(BOOST_PIN, OUTPUT)
#define BOOST_ON digitalWrite(BOOST_PIN, HIGH)
#define BOOST_OFF digitalWrite(BOOST_PIN, LOW)
// Battery Level Monitoring
#define BATTERY (float)(((analogRead(35) * (3.3 / 4096)) * 2) + 0.28)
#define CHARGE mapf(BATTERY, 3.27, 4.31, 0, 100)
// End Of Arduino Selection ARDUINO_MEGA
#endif
// Float Mapping Function
double mapf(double val, double in_min, double in_max, double out_min, double out_max) {
return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
// 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 = abs(constrain(analogRead(SENSOR_PIN), VTH, 1023));
value = map(value, VTH, 1023, 0, 1023);
return value;
}
//======================================================
// MAIN HARDWARE SETUP
//======================================================
void setup()
{
// Communications
TERMINAL.begin(9600);
// Oled Display
oled_init();
// Make Sure Of The Core
#ifdef ESP32
BOOST_SETUP;
BOOST_OFF;
BLUETOOTH.begin("KICKER"); //Bluetooth device name
#else
BLUETOOTH.begin(9600);
#endif
// I/O
button.begin(); // Button
pixels.begin(); // Neo Pixel
LED_CLEAR(); // Turn Off All LED
// The Random Number Source
randomSeed(analogRead(RANDOM_SOURCE));
// Battery Managment
if(BATTERY <= 3.7) {
// Display Battery Low
dualcomm("Battery Low");
// Display Battery Voltage
while(BATTERY <= 3.7) { Serial.println(BATTERY); drawProgressBarDemo(CHARGE); oled_show(); delay(5000); }
// Clearing The Screen
oled_clear();
oled_show();
}
// POWER UP MESSGAE
dualcomm(POWERUP);
// TURN BOOSTER ON
BOOST_ON;
// Blue, Red And Green
colorWipe(pixels.Color(0, 0, 255), 15); // Blue
delay(100);
colorWipe(pixels.Color(255, 0, 0), 15); // Red
delay(100);
colorWipe(pixels.Color(0, 255, 0), 15); // Green
// Delay 1 Sec
delay(500);
// Red FadeOut
LED_GREEN_FADEOUT();
// Display Rainbow
LED_CLEAR();
// TURN BOOSTER ON
BOOST_OFF;
}
//======================================================
// MAIN PROGRAM LOOP
//======================================================
void loop()
{
// Reading From Bluetooth
if(BLUETOOTH.available())
{
// Reading Message From Bluetooth
String msg = BLUETOOTH.readString();
msg.trim();
// Display On Sertial
terminal(msg);
// Parsing Objects Message Objects
DynamicJsonDocument root(JSON_CAPACITY);
// Deserialize the JSON document
DeserializationError error = deserializeJson(root, msg);
// If The System Understood The Message Clearly
if(!error)
{
// Sending Status Message
dualcomm(UNDERSTOOD);
// Reading Game Name & Settings
String game = root["gm"];
String settings = root["set"];
// Clearing JSON Buffer
root.clear();
// Entering To Game Selector
game_selector(game, settings);
}
else
{
// Display The Error Message
terminal(JSON_ERROR);
}
// Clearing Buffer
BLUETOOTH.flush();
}
}
//======================================================
// END OF PROGRAM
//======================================================
+29
View File
@@ -0,0 +1,29 @@
//======================================================
// TERMINAL WINDOW - NATIVE USB
//======================================================
void terminal(String msg)
{
msg += "\n";
TERMINAL.print(msg);
}
//======================================================
// BLUETOOTH WINDOW - NATIVE/VERTUAL USB
//======================================================
void bluetooth(String msg)
{
msg += "\n";
BLUETOOTH.print(msg);
}
//======================================================
// BLUETOOTH WINDOW - NATIVE/VERTUAL USB
//======================================================
void dualcomm(String msg)
{
terminal(msg);
bluetooth(msg);
}
+206
View File
@@ -0,0 +1,206 @@
//======================================================
// MAIN COUNTER GAME LOOP
//======================================================
/* COUNTER GAME SETTINGS PARAMETERS NOTE:
* --------------------------------------
*
* FORMAT :
* --------
* {"le": false, "lm": 10}
*
* MEANNING :
* ----------
* ct : count
* le : stands for limit enable, if is TRUE the game end if target value is meet
* lm : targeted amount of kicks
*
* THINGS THAT ENDS THE GAME :
* ---------------------------
* 1. BY A COMMAND "FROM APP VIA BLUETOOTH"
* 2. BY LIMITS MEET "WHEN NUMBER OF YOUR KICKS REACH TO lm VALUE"
*
* WHAT DATA TO BE COLLECTED :
* ---------------------------
* 1. FOR EVERY KICK YOU RECEIVE THE COUNTER DISPLAY AND TIMESTAMP
* WE NEED TO COLLECT TIMESTAMPS AS AN ARRAY IN JSON FORMAT
*
* CALCULATING RESULTS :
* ---------------------
* No calculation required, Its directly the count
*/
void game_counter(String settings)
{
//------------------------------------------------------
// START OF READING THE SETTINGS
//------------------------------------------------------
// CONVERTING GAME TEXT TO JSON
DynamicJsonDocument play(JSON_CAPACITY); // building JSON Buffer
// Deserialize the JSON document
DeserializationError error = deserializeJson(play, settings); // the main JSON body container
// JSON ERROR
if(error) {
dualcomm(JSON_ERROR);
return;
}
// PUBLIC VARIABLES
uint16_t limit = play["lm"]; // Kick Target Limit
bool limitEnable = play["le"]; // Enable Kick Target Limit
// Clearing Buffer
play.clear();
// Kick Counter
uint16_t counter = 0;
//------------------------------------------------------
// PREPARE FOR GAME START
//------------------------------------------------------
// Display Welcome Message
// TERMINAL("");
// TERMINAL("==================================================");
// TERMINAL(" -->> Counter Game Settings");
// TERMINAL("--------------------------------------------------");
// TERMINAL("Strike Limit : " + String(limit) + " Strikes");
// TERMINAL("Limits Enable : " + String(limitEnable));
// TERMINAL("--------------------------------------------------");
// TERMINAL("2 Seconds To Start Get Ready & Good Luck !!");
// TERMINAL("==================================================");
// TERMINAL("");
// Game Start Lights
Counter_Start();
// staring Of time stamp
unsigned long startStamp = millis();
while(1)
{
// Time Stamp
unsigned long timeStamp = millis() - startStamp;
// If Stop Command Came From Smart Phone
if(isBluetooth)
{
String msg = BLUETOOTH.readString();
msg.trim();
if(msg == CM_STOP)
{
// TERMINAL("");
// TERMINAL("---------------------------------------------------");
dualcomm(GAME_OVER);
// TERMINAL("===================================================");
// TERMINAL("");
Counter_End();
// Grean Light In
//LED_GREEN_FADEOUT();
// RESET
return;
}
else if(msg == CM_RESET)
{
Counter_Reset();
counter = 0;
startStamp = millis();
timeStamp = startStamp;
}
}
// If Limits Are Enabled And Meet
if(limitEnable == true && counter == limit)
{
// END GAME
// TERMINAL("");
// TERMINAL("---------------------------------------------------");
dualcomm(GAME_OVER);
// TERMINAL("===================================================");
// TERMINAL("");
// Celebration
Counter_Celebration();
// RESET
return;
}
// Read Impact
if(readImpact() > PWR)
{
counter++;
dualcomm(counter_data_json(counter, timeStamp));
delay(100);
}
}
}
//======================================================
// COUNTER DATA JSON -> To Send Back To Server
//======================================================
String counter_data_json(unsigned int ct, unsigned int st)
{
// Building The Buffer
DynamicJsonDocument data(JSON_CAPACITY);
// Convert Int To Float
float stamp = st;
stamp /= 1000;
// Assignning Dataset
data["ct"] = ct;
data["st"] = String(stamp, 3).toFloat();
String output;
serializeJson(data, output);
data.clear();
return output;
}
//======================================================
// REACTION GAME START LED SIGNAL
//======================================================
void Counter_Start()
{
// LightUp
LED_WHITE_CROSSFADE(2);
// Grean Light In
LED_GREEN();
}
//======================================================
// REACTION GAME END SINGNAL LED SIGNAL
//======================================================
void Counter_End()
{
LED_RED_BLINK(2);
LED_RED_FADEOUT();
}
//======================================================
// REACTION GAME START LED CELEBRATION
//======================================================
void Counter_Celebration()
{
// LightUp
rainbowCycle(2);
// Grean Light OUT
LED_GREEN_FADEOUT();
}
//======================================================
// REACTION GAME RESET LED SIGNAL
//======================================================
void Counter_Reset()
{
//
LED_BLUE();
delay(250);
LED_BLUE_FADEOUT();
}
+237
View File
@@ -0,0 +1,237 @@
//======================================================
// REACTION GAME MAIN ROUTINE
//======================================================
/* REACTION GAME SETTINGS PARAMETERS NOTE:
* ------------------------------------------
*
* FORMAT :
* --------
* {"tr": 10, "mn": 1, "mx": 2}
*
* MEANNING :
* ----------
* tr : trials pre counter
* mn : minimum delay, between trials
* mx : maximum delay, between trials
*
* THINGS THAT ENDS THE GAME :
* ---------------------------
* 1. BY COUNT DOWN "tr" REACH TO ZERO
* 2. BY A COMMAND "FROM APP VIA BLUETOOTH"
*
* CALCULATING RESULTS :
* ---------------------
* Rapid Kick Speed = Number Of Kickes Made / Elasped Time In Sec
*/
//======================================================
void game_reaction(String settings)
{
//------------------------------------------------------
// START OF READING THE SETTINGS
//------------------------------------------------------
// CONVERTING GAME TEXT TO JSON
DynamicJsonDocument play(JSON_CAPACITY);
// Deserialize the JSON document
DeserializationError error = deserializeJson(play, settings);
// JSON ERROR
if(error) {
dualcomm(JSON_ERROR);
return;
}
// PUBLIC VARIABLES
uint8_t minDelay = play["mn"];
uint8_t maxDelay = play["mx"];
uint16_t trials = play["lm"];
uint16_t temp = trials;
// Clearing Buffer
play.clear();
//------------------------------------------------------
// PREPARE FOR GAME START
//------------------------------------------------------
// Display Welcome Message
// TERMINAL("");
// TERMINAL("==================================================");
// TERMINAL(" -->> Reaction Game Settings");
// TERMINAL("--------------------------------------------------");
// TERMINAL("trials : " + String(trials) + " Strikes");
// TERMINAL("try delay : " + String(minDelay) + " to " + String(maxDelay) + " Seconds");
// TERMINAL("--------------------------------------------------");
// TERMINAL("2 Seconds To Start Get Ready & Good Luck !!");
// TERMINAL("==================================================");
// TERMINAL("");
// Ready Signal
Reaction_Start();
// Main Variables
uint16_t sdelay = 0;
uint16_t power = 0;
uint16_t response = 0;
unsigned long startTime = 0;
unsigned long stopTime = 0;
// Processing Results
for(int i=0; i<trials; i++)
{
// Reading Kick Data
terminal(" ------ Trial Number (" + String(i + 1) + ") ------");
terminal(" -> Get Ready !!");
// Set Random Delay Time
sdelay = random(minDelay, (maxDelay + 1));
sdelay *= 1000;
delay(sdelay);
// Light Up Green
rgbLED(0, 150, 0);
// Start Timer
startTime = millis();
// Sensing Strike Power
while(power < PWR)
{
// Reading The Strike
power = readImpact();
// If Stop Command Came From Smart Phone
if(isBluetooth)
{
String msg = BLUETOOTH.readString();
msg.trim();
if(msg == CM_STOP) {
dualcomm(GAME_OVER);
Reaction_End();
// LightUp
//LED_RED();
//delay(2000);
// Red FadeOut
//LED_RED_FADEOUT();
// RESET
return;
}
else if(msg == CM_RESET) {
trials = temp;
break;
}
}
}
// Stop Timer
stopTime = millis();
// Stop Light
rgbLED(150, 0, 0);
// Calculating Response Time
response = stopTime - startTime - 1;
// Create Json Data
String msg = reaction_data_json(i+1, power, response, sdelay);
// Print To TERMINAL
dualcomm(msg);
// Clear All
sdelay = 0;
power = 0;
response = 0;
}
// Red Light Delay
delay(500);
// END GAME
// TERMINAL("");
// TERMINAL("---------------------------------------------------");
dualcomm(GAME_OVER);
// TERMINAL("===================================================");
// TERMINAL("");
// Celebration
Reaction_Celebration();
// Blink Red
//Reaction_End();
// Return To Game Selector
return;
}
//======================================================
// REACTION DATA JSON -> To Send Back To Server
//======================================================
String reaction_data_json(unsigned int count, unsigned int power, unsigned int response, unsigned int startDelay)
{
// Building The Buffer
DynamicJsonDocument data(JSON_CAPACITY);
// Convert To Seconds Unit
float re = response;
re /= 1000;
// Asignning Values
data["ct"] = count; // Strike Number
data["pw"] = power; // Strike Power In Analog Read
data["sd"] = startDelay/1000; // LED Strip Start Delay in Seconds
data["re"] = String(re, 3).toFloat(); // Strike Reaction Time In Milli Sec
String output;
serializeJson(data, output);
data.clear();
return output;
}
//======================================================
// REACTION GAME START LED SIGNAL
//======================================================
void Reaction_Start()
{
LED_WHITE_CROSSFADE(3);
LED_RED_FADEIN();
}
//======================================================
// REACTION GAME END SINGNAL LED SIGNAL
//======================================================
void Reaction_End()
{
LED_RED_BLINK(2);
LED_RED_FADEOUT();
}
//======================================================
// REACTION GAME START LED CELEBRATION
//======================================================
void Reaction_Celebration()
{
// LightUp
rainbowCycle(2);
// Grean Light OUT
LED_GREEN_FADEOUT();
}
//======================================================
// REACTION GAME RESET LED SIGNAL
//======================================================
void Reaction_Reset()
{
LED_BLUE();
delay(250);
LED_BLUE_FADEOUT();
}
+37
View File
@@ -0,0 +1,37 @@
//======================================================
// MAIN PROGRAM LOOP
//======================================================
/* game selector is the main function that tell the game groutine
* which game to be played and at what parameters
*/
void game_selector(String game, String settings)
{
// Kick Counter Game
if(game == "ct") {
terminal(GM_COUNTER);
game_counter(settings);
return;
}
// Time Attack Game
else if(game == "ta") {
terminal(GM_TATTACK);
game_timeAttack(settings);
return;
}
// Time Attack Game
else if(game == "ra") {
terminal(GM_REACTION);
game_reaction(settings);
return;
}
// Unknown Game
else {
dualcomm(UNKNOWN_GAME);
return;
}
}
@@ -0,0 +1,261 @@
//======================================================
// TIME ATTACK GAME MAIN ROUTINE
//======================================================
/* TIME ATTACK GAME SETTINGS PARAMETERS NOTE:
* ------------------------------------------
*
* FORMAT :
* --------
* {"tm": 10, "le": false, "lm": 10}
*
* tm : count down timer starting value
* le : stands for limit enable, if is TRUE the game end if target value is meet
* lm : targeted amount of kicks
*
* THINGS THAT ENDS THE GAME :
* ---------------------------
* 1. BY COUNT DOWN "tm" REACH TO ZERO
* 2. BY A COMMAND "FROM APP VIA BLUETOOTH"
* 3. BY LIMITS MEET "WHEN NUMBER OF YOUR KICKS REACH TO lm VALUE"
*
* CALCULATING RESULTS :
* ---------------------
* Rapid Kick Speed = Number Of Kickes Made / Elasped Time In Sec
* Delay Between Kicks = Time Of Current Kick - Time Of Last Kick
*
* RETURNNING DATA FORMAT :
* ------------------------
* {"tm" : 82, "ct" : 10, "dl" : 1234, "st" : 585}
*
* tm : timer count in Sec
* ct : strike counter (number of kicks)
* dl : delay between strikes
* st : time stampe of the strike in milli Seconds
*/
void game_timeAttack(String settings)
{
//------------------------------------------------------
// START OF READING THE SETTINGS
//------------------------------------------------------
// CONVERTING GAME TEXT TO JSON
DynamicJsonDocument play(JSON_CAPACITY); // building JSON Buffer
// Deserialize the JSON document
DeserializationError error = deserializeJson(play, settings); // the main JSON body container
// JSON ERROR
if(error) {
dualcomm(JSON_ERROR);
return;
}
// PUBLIC VARIABLES
uint16_t time = play["tm"]; // Time Window To Play
uint16_t timer = time;
uint16_t limit = play["lm"]; // Kick Target Limit
bool limitEnable = play["le"]; // Enable Kick Target Limit
// Clearing Buffer
play.clear();
//------------------------------------------------------
// PREPARE FOR GAME START
//------------------------------------------------------
// Display Welcome Message
// TERMINAL("");
// TERMINAL("==================================================");
// TERMINAL(" -->> Time Attack Game Settings");
// TERMINAL("--------------------------------------------------");
// TERMINAL("Timer : " + String(time) + " Seconds");
// TERMINAL("Strike Limit : " + String(limit));
// TERMINAL("Limit Enabled : " + String(limitEnable));
// TERMINAL("--------------------------------------------------");
// TERMINAL("2 Seconds To Start Get Ready & Good Luck !!");
// TERMINAL("==================================================");
// TERMINAL("");
// Send Initial Display
dualcomm(timeAttack_data_json(timer, 0, 0, 0));
// Start LED Signal
TimeAttack_Start();
// Building Timers 1000mSec -> 1Sec
TimeTrigger sec_tick(1000);
// Kick Counter
uint16_t counter = 0;
// staring Of time stamp
unsigned long startStamp = millis();
// store the time of the previous kick
unsigned long lastStamp = 0;
// reseting time ticker
sec_tick.Reset();
// While The Game Is Still On
while(timer != 0)
{
// Time Stamp
unsigned long timeStamp = millis() - startStamp;
// If Stop Command Came From Smart Phone
if(isBluetooth)
{
String msg = BLUETOOTH.readString();
msg.trim();
if(msg == CM_STOP)
{
dualcomm(GAME_OVER);
// Grean Light In
LED_GREEN_FADEOUT();
// Return
return;
}
else if(msg == CM_RESET)
{
timer = time;
counter = 0;
startStamp = millis();
timeStamp = startStamp;
lastStamp = timeStamp;
sec_tick.Reset();
dualcomm(timeAttack_data_json(timer, counter, lastStamp, timeStamp));
}
}
// Every Kick ---------------------------->>>
if(readImpact() > 100)
{
counter++;
dualcomm(timeAttack_data_json(timer, counter, lastStamp, timeStamp));
lastStamp = timeStamp;
delay(100);
}
// Every Second ---------------------------->>>
if(sec_tick.Trigger())
{
timer--;
dualcomm(timeAttack_data_json(timer, counter, 0, 0));
}
// If Limits Are Enabled And Meet
if(limitEnable == true && counter == limit)
{
// END GAME
// TERMINAL("");
// TERMINAL("---------------------------------------------------");
dualcomm(GAME_OVER);
// TERMINAL("===================================================");
// TERMINAL("");
// LightUp
rainbowCycle(2);
// Grean Light In
LED_GREEN_FADEOUT();
// Return
return;
}
}
// Send End Game Signal To Smart Phone
// TERMINAL("");
// TERMINAL("---------------------------------------------------");
dualcomm(GAME_OVER);
// TERMINAL("===================================================");
// TERMINAL("");
// LightUp
LED_RED_FADEIN(); // RED Color
delay(2000); // Delay 2000
LED_CLEAR(); // CLEAR OFF
// Return
return;
}
//======================================================
// TIME ATTACK KICK DATA JSON -> To Send Back To Server
//======================================================
String timeAttack_data_json(unsigned int RunningTime, unsigned int KickCount, unsigned int LastStamp, unsigned int TimeStamp)
{
// Building The Buffer
DynamicJsonDocument data(JSON_CAPACITY);
data["tm"] = RunningTime;
data["ct"] = KickCount;
// Calculations
if(TimeStamp > 0 )
{
// Calculate Time Stamp In Seconds
float st = TimeStamp;
st /= 1000;
data["st"] = String(st, 3).toFloat();
// Calculate Strike Delay In Seconds
float dl = TimeStamp - LastStamp;
dl /= 1000;
data["dl"] = String(dl, 3).toFloat();
}
String output;
serializeJson(data, output);
data.clear();
return output;
}
//======================================================
// REACTION GAME START LED SIGNAL
//======================================================
void TimeAttack_Start()
{
// LightUp
LED_WHITE_CROSSFADE(3);
// Grean Light In
LED_GREEN();
}
//======================================================
// REACTION GAME END SINGNAL LED SIGNAL
//======================================================
void TimeAttack_End()
{
// Grean Light In
LED_GREEN_FADEOUT();
// Red Light In
LED_RED_FADEIN();
// Wait For 2 Sec
delay(2000);
// Red Light Out
LED_RED_FADEOUT();
}
//======================================================
// REACTION GAME START LED CELEBRATION
//======================================================
void TimeAttack_Celebration()
{
// LightUp
rainbowCycle(2);
// Grean Light OUT
LED_GREEN_FADEOUT();
}
//======================================================
// REACTION GAME RESET LED SIGNAL
//======================================================
void TimeAttack_Reset()
{
LED_BLUE();
delay(250);
LED_BLUE_FADEOUT();
}
+450
View File
@@ -0,0 +1,450 @@
#define BETWEEN_FADE 15
#define FADE_RATE 15
#define BLINK_RATE 250
//======================================================
// SET STRIP COLOR
//======================================================
void rgbLED(int red, int green, int blue)
{
#ifdef ESP32
BOOST_ON;
#endif
for(int i=0; i<NUM_PIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(red,green,blue));
}
pixels.show();
}
//======================================================
// CLEAR SIGNAL LED COLOR : NO COLOR
//======================================================
void LED_CLEAR()
{
rgbLED(0, 0, 0);
}
//======================================================
// RED
//======================================================
void LED_RED()
{
#ifdef ESP32
BOOST_ON;
#endif
rgbLED(255, 0, 0);
}
//------------------------------------------------------
// BLINK
//------------------------------------------------------
void LED_RED_BLINK(int times)
{
#ifdef ESP32
BOOST_ON;
#endif
for(int i=0; i<times; i++)
{
LED_RED();
delay(BLINK_RATE);
LED_CLEAR();
delay(BLINK_RATE);
}
#ifdef ESP32
BOOST_OFF;
#endif
}
//------------------------------------------------------
// FADEIN
//------------------------------------------------------
void LED_RED_FADEIN()
{
#ifdef ESP32
BOOST_ON;
#endif
// Fade In Red Indicate That You Enter Battle
for(int i=0; i<=255; i=i+5)
{
rgbLED(i, 0, 0);
delay(FADE_RATE);
}
}
//------------------------------------------------------
// FADEOUT
//------------------------------------------------------
void LED_RED_FADEOUT()
{
// Fade In Red Indicate That You Enter Battle
for(int i=255; i>=0; i=i-5)
{
rgbLED(i, 0, 0);
delay(FADE_RATE);
}
#ifdef ESP32
BOOST_OFF;
#endif
}
//------------------------------------------------------
// CROSS FADE
//------------------------------------------------------
void LED_RED_CROSSFADE(int times)
{
#ifdef ESP32
BOOST_ON;
#endif
for(int i=0; i<times; i++)
{
LED_RED_FADEIN();
delay(BETWEEN_FADE);
LED_RED_FADEOUT();
delay(BETWEEN_FADE);
}
#ifdef ESP32
BOOST_OFF;
#endif
}
//======================================================
// GREEN
//======================================================
void LED_GREEN()
{
#ifdef ESP32
BOOST_ON;
#endif
rgbLED(0, 255, 0);
}
//------------------------------------------------------
// BLINK
//------------------------------------------------------
void LED_GREEN_BLINK(int times)
{
#ifdef ESP32
BOOST_ON;
#endif
for(int i=0; i<times; i++)
{
LED_GREEN();
delay(BLINK_RATE);
LED_CLEAR();
delay(BLINK_RATE);
}
#ifdef ESP32
BOOST_OFF;
#endif
}
//------------------------------------------------------
// FADEIN
//------------------------------------------------------
void LED_GREEN_FADEIN()
{
#ifdef ESP32
BOOST_ON;
#endif
// Fade In Red Indicate That You Enter Battle
for(int i=0; i<=255; i=i+5)
{
rgbLED(0, i, 0);
delay(FADE_RATE);
}
}
//------------------------------------------------------
// FADEOUT
//------------------------------------------------------
void LED_GREEN_FADEOUT()
{
// Fade In Red Indicate That You Enter Battle
for(int i=255; i>=0; i=i-5)
{
rgbLED(0, i, 0);
delay(FADE_RATE);
}
#ifdef ESP32
BOOST_OFF;
#endif
}
//------------------------------------------------------
// CROSS FADE
//------------------------------------------------------
void LED_GREEN_CROSSFADE(int times)
{
#ifdef ESP32
BOOST_ON;
#endif
for(int i=0; i<times; i++)
{
LED_GREEN_FADEIN();
delay(BETWEEN_FADE);
LED_GREEN_FADEOUT();
delay(BETWEEN_FADE);
}
#ifdef ESP32
BOOST_OFF;
#endif
}
//======================================================
// BLUE
//======================================================
void LED_BLUE()
{
#ifdef ESP32
BOOST_ON;
#endif
rgbLED(0, 0, 255);
}
//------------------------------------------------------
// BLINK
//------------------------------------------------------
void LED_BLUE_BLINK(int times)
{
#ifdef ESP32
BOOST_ON;
#endif
for(int i=0; i<times; i++)
{
LED_BLUE();
delay(BLINK_RATE);
LED_CLEAR();
delay(BLINK_RATE);
}
#ifdef ESP32
BOOST_OFF;
#endif
}
//------------------------------------------------------
// FADEIN
//------------------------------------------------------
void LED_BLUE_FADEIN()
{
#ifdef ESP32
BOOST_ON;
#endif
// Fade In Red Indicate That You Enter Battle
for(int i=0; i<=255; i=i+5)
{
rgbLED(0, 0, i);
delay(FADE_RATE);
}
}
//------------------------------------------------------
// FADEOUT
//------------------------------------------------------
void LED_BLUE_FADEOUT()
{
#ifdef ESP32
BOOST_ON;
#endif
// Fade In Red Indicate That You Enter Battle
for(int i=255; i>=0; i=i-5)
{
rgbLED(0, 0, i);
delay(FADE_RATE);
}
#ifdef ESP32
BOOST_OFF;
#endif
}
//------------------------------------------------------
// CROSS FADE
//------------------------------------------------------
void LED_BLUE_CROSSFADE(int times)
{
#ifdef ESP32
BOOST_ON;
#endif
for(int i=0; i<times; i++)
{
LED_BLUE_FADEIN();
delay(BETWEEN_FADE);
LED_BLUE_FADEOUT();
delay(BETWEEN_FADE);
}
#ifdef ESP32
BOOST_OFF;
#endif
}
//======================================================
// WHITE
//======================================================
void LED_WHITE()
{
#ifdef ESP32
BOOST_ON;
#endif
rgbLED(255, 255, 255);
}
//------------------------------------------------------
// BLINK
//------------------------------------------------------
void LED_WHITE_BLINK(int times)
{
#ifdef ESP32
BOOST_ON;
#endif
for(int i=0; i<times; i++)
{
LED_WHITE();
delay(BLINK_RATE);
LED_CLEAR();
delay(BLINK_RATE);
}
#ifdef ESP32
BOOST_OFF;
#endif
}
//------------------------------------------------------
// FADEIN
//------------------------------------------------------
void LED_WHITE_FADEIN()
{
#ifdef ESP32
BOOST_ON;
#endif
// Fade In Red Indicate That You Enter Battle
for(int i=0; i<=255; i=i+5)
{
rgbLED(i, i, i);
delay(FADE_RATE);
}
}
//------------------------------------------------------
// FADEOUT
//------------------------------------------------------
void LED_WHITE_FADEOUT()
{
// Fade In Red Indicate That You Enter Battle
for(int i=255; i>=0; i=i-5)
{
rgbLED(i, i, i);
delay(FADE_RATE);
}
}
//------------------------------------------------------
// CROSS FADE
//------------------------------------------------------
void LED_WHITE_CROSSFADE(int times)
{
for(int i=0; i<times; i++)
{
LED_WHITE_FADEIN();
delay(BETWEEN_FADE);
LED_WHITE_FADEOUT();
delay(BETWEEN_FADE);
}
}
//======================================================
// COLOR WHIPE
//======================================================
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait)
{
for(uint16_t i=0; i<NUM_PIXELS; i++)
{
pixels.setPixelColor(i, c);
pixels.show();
delay(wait);
}
}
//======================================================
// END GAME LIGHTING SIGNAL
//======================================================
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait)
{
uint16_t i, j;
for(j=0; j<256*5; j++)
{ // 5 cycles of all colors on wheel
for(i=0; i< pixels.numPixels(); i++)
{
pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
}
pixels.show();
delay(wait);
}
}
//======================================================
// END GAME LIGHTING SIGNAL
//======================================================
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos)
{
WheelPos = 255 - WheelPos;
if(WheelPos < 85)
{
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170)
{
WheelPos -= 85;
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
+84
View File
@@ -0,0 +1,84 @@
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
#include <SSD1306Wire.h> // legacy include: `#include "SSD1306.h"
// Display Instance
SSD1306Wire display(0x3c, 21, 22);
void oled_init() {
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.clear();
display.display();
}
void drawTextFlowDemo(String Text) {
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawStringMaxWidth(0, 0, 128, Text);
}
void drawTextAlignmentDemo() {
// Text alignment demo
display.setFont(ArialMT_Plain_10);
// The coordinates define the left starting point of the text
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(0, 10, "Left aligned (0,10)");
// The coordinates define the center of the text
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 22, "Center aligned (64,22)");
// The coordinates define the right end of the text
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString(128, 33, "Right aligned (128,33)");
}
void drawRectDemo() {
// Draw a pixel at given position
for (int i = 0; i < 10; i++) {
display.setPixel(i, i);
display.setPixel(10 - i, i);
}
display.drawRect(12, 12, 20, 20);
// Fill the rectangle
display.fillRect(14, 14, 17, 17);
// Draw a line horizontally
display.drawHorizontalLine(0, 40, 20);
// Draw a line horizontally
display.drawVerticalLine(40, 0, 20);
}
void drawCircleDemo() {
for (int i=1; i < 8; i++) {
display.setColor(WHITE);
display.drawCircle(32, 32, i*3);
if (i % 2 == 0) {
display.setColor(BLACK);
}
display.fillCircle(96, 32, 32 - i* 3);
}
}
void drawProgressBarDemo(int Value) {
int progress = Value;
// draw the progress bar
display.drawProgressBar(0, 50, 120, 10, progress);
// draw the percentage as String
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 35, String(progress) + "%");
}
void oled_clear() {
display.clear();
}
void oled_show() {
display.display();
}