update info

This commit is contained in:
ghassan committed 2020-04-03 14:28:36 +03:00
1 parent 8fb3b3e0d2
commit 29fe2f705c
109 files changed
+1

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
//====================================================================
// GENERAL FUNCTION SECTION
//====================================================================
// I2C DECIVES SCANNER
//--------------------------------------------------------------------
void scanI2C()
{
byte error, address;
int nDevices;
Serial.println(" -> Scanning I2C Devices ...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print(" --> Device Found At Address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print(" --> Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println(" --> No I2C devices found\n");
else
Serial.println(" --> done");
}
//--------------------------------------------------------------------
// Float Number 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;
}
+41
View File
@@ -0,0 +1,41 @@
//====================================================================
// COUNTER GAME PROGRAM PAGE
//====================================================================
// GAME COUNTER - MENU
//--------------------------------------------------------------------
void game_counter_menu()
{
menu_display("COUNTER");
}
//--------------------------------------------------------------------
// GAME COUNTER - SETTINGS DISPLAY
//--------------------------------------------------------------------
void game_counter_settings()
{
}
//--------------------------------------------------------------------
// GAME COUNTER - GAME PLAY
//--------------------------------------------------------------------
void game_counter_play()
{
oled_clear();
drawTextCenter(128/2, 0, 2, "KICKS");
//drawTextCenter(128/2, 20, 3, String(count));
drawTextCenter(128/2, 50, 1, "<< EXIT >>");
oled_show();
}
//--------------------------------------------------------------------
// GAME COUNTER - GAME FINAL RESULT
//--------------------------------------------------------------------
void game_counter_results()
{
}
+41
View File
@@ -0,0 +1,41 @@
//====================================================================
// DECISION GAME PROGRAM PAGE
//====================================================================
// GAME DECISION - MENU
//--------------------------------------------------------------------
void game_decision_menu()
{
menu_display("DECISION");
}
//--------------------------------------------------------------------
// GAME DECISION - SETTINGS DISPLAY
//--------------------------------------------------------------------
void game_decision_settings()
{
}
//--------------------------------------------------------------------
// GAME DECISION - GAME PLAY
//--------------------------------------------------------------------
void game_decision_play()
{
oled_clear();
drawTextCenter(128/2, 0, 2, "KICKS");
//drawTextCenter(128/2, 20, 3, String(count));
drawTextCenter(128/2, 50, 1, "<< EXIT >>");
oled_show();
}
//--------------------------------------------------------------------
// GAME DECISION - GAME FINAL RESULT
//--------------------------------------------------------------------
void game_decision_results()
{
}
+42
View File
@@ -0,0 +1,42 @@
//====================================================================
// FOUCSE GAME PROGRAM PAGE
//====================================================================
// GAME FOUCSE - MENU
//--------------------------------------------------------------------
void game_foucse_menu()
{
menu_display("FOCUSE");
}
//--------------------------------------------------------------------
// GAME FOUCSE - SETTINGS DISPLAY
//--------------------------------------------------------------------
void game_foucse_settings()
{
}
//--------------------------------------------------------------------
// GAME FOUCSE - GAME PLAY
//--------------------------------------------------------------------
void game_foucse_play()
{
oled_clear();
drawTextCenter(128/2, 0, 2, "KICKS");
//drawTextCenter(128/2, 20, 3, String(count));
drawTextCenter(128/2, 50, 1, "<< EXIT >>");
oled_show();
}
//--------------------------------------------------------------------
// GAME FOUCSE - GAME FINAL RESULT
//--------------------------------------------------------------------
void game_foucse_results()
{
}
+42
View File
@@ -0,0 +1,42 @@
//====================================================================
// REACTION GAME PROGRAM PAGE
//====================================================================
// GAME REACTION - MENU
//--------------------------------------------------------------------
void game_reaction_menu()
{
menu_display("REACTION");
}
//--------------------------------------------------------------------
// GAME REACTION - SETTINGS DISPLAY
//--------------------------------------------------------------------
void game_reaction_settings()
{
}
//--------------------------------------------------------------------
// GAME REACTION - GAME PLAY
//--------------------------------------------------------------------
void game_reaction_play()
{
oled_clear();
drawTextCenter(128/2, 0, 2, "KICKS");
//drawTextCenter(128/2, 20, 3, String(count));
drawTextCenter(128/2, 50, 1, "<< EXIT >>");
oled_show();
}
//--------------------------------------------------------------------
// GAME REACTION - GAME FINAL RESULT
//--------------------------------------------------------------------
void game_reaction_results()
{
}
+41
View File
@@ -0,0 +1,41 @@
//====================================================================
// STAMINA GAME PROGRAM PAGE
//====================================================================
// GAME STAMINA - MENU
//--------------------------------------------------------------------
void game_stamina_menu()
{
menu_display("STAMINA");
}
//--------------------------------------------------------------------
// GAME STAMINA - SETTINGS DISPLAY
//--------------------------------------------------------------------
void game_stamina_settings()
{
}
//--------------------------------------------------------------------
// GAME STAMINA - GAME PLAY
//--------------------------------------------------------------------
void game_stamina_play()
{
oled_clear();
drawTextCenter(128/2, 0, 2, "KICKS");
//drawTextCenter(128/2, 20, 3, String(count));
drawTextCenter(128/2, 50, 1, "<< EXIT >>");
oled_show();
}
//--------------------------------------------------------------------
// GAME STAMINA - GAME FINAL RESULT
//--------------------------------------------------------------------
void game_stamina_results()
{
}
+41
View File
@@ -0,0 +1,41 @@
//====================================================================
// TIMEATTACK GAME PROGRAM PAGE
//====================================================================
// GAME TIMEATTACK - MENU
//--------------------------------------------------------------------
void game_timeattack_menu()
{
menu_display("TIMEATTACK");
}
//--------------------------------------------------------------------
// GAME TIMEATTACK - SETTINGS DISPLAY
//--------------------------------------------------------------------
void game_timeattack_settings()
{
}
//--------------------------------------------------------------------
// GAME TIMEATTACK - GAME PLAY
//--------------------------------------------------------------------
void game_timeattack_play()
{
oled_clear();
drawTextCenter(128/2, 0, 2, "KICKS");
//drawTextCenter(128/2, 20, 3, String(count));
drawTextCenter(128/2, 50, 1, "<< EXIT >>");
oled_show();
}
//--------------------------------------------------------------------
// GAME TIMEATTACK - GAME FINAL RESULT
//--------------------------------------------------------------------
void game_timeattack_results()
{
}
+58
View File
@@ -0,0 +1,58 @@
//====================================================================
// COUNTER GAME PROGRAM PAGE
//====================================================================
// DISPLAY MENU TITLE FUNCTION
//--------------------------------------------------------------------
void menu_display(String Title)
{
// Starting The First Code
oled_clear();
drawTextCenter(128/2, 5, 1, "MODE " + String(menu_count + 1));
drawTextCenter(128/2, 22, 2, " - " + Title + " - ");
drawTextCenter(128/2, 43, 1, "<< SELECT >>");
oled_show();
display_changed = true;
}
//--------------------------------------------------------------------
// SWITCH GAME MENUS
//--------------------------------------------------------------------
void switch_menu()
{
if(menu_count == 0 && display_changed == false) {
game_counter_menu();
} else if(menu_count == 1 && display_changed == false) {
game_timeattack_menu();
} else if(menu_count == 2 && display_changed == false) {
game_reaction_menu();
} else if(menu_count == 3 && display_changed == false) {
game_decision_menu();
} else if(menu_count == 4 && display_changed == false) {
game_stamina_menu();
} else if(menu_count == 5 && display_changed == false) {
game_foucse_menu();
}
}
//--------------------------------------------------------------------
// SELECT A GAME
//--------------------------------------------------------------------
void select_game()
{
if(menu_count == 0 && CB.pressed()) {
} else if(menu_count == 1 && CB.pressed()) {
} else if(menu_count == 2 && CB.pressed()) {
} else if(menu_count == 3 && CB.pressed()) {
} else if(menu_count == 4 && CB.pressed()) {
} else if(menu_count == 5 && CB.pressed()) {
}
}
+61
View File
@@ -0,0 +1,61 @@
//====================================================================
// ADAFRUITE NEO PIXEL
//====================================================================
// INCLUDE LIBRARY
//--------------------------------------------------------------------
// Neo Pixel Library
#include <Adafruit_NeoPixel.h>
// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals. Note that for older NeoPixel
Adafruit_NeoPixel pixels(NUMPIXELS, NEOPIXEL, NEO_GRB + NEO_KHZ800);
//--------------------------------------------------------------------
// INITIALIZE LED STRIP OBJECT (REQUIRED)
//--------------------------------------------------------------------
void neo_init() {
BOOST_ON;
delay(5);
pixels.begin();
pixels.clear();
pixels.show();
delay(5);
BOOST_OFF;
}
//--------------------------------------------------------------------
// TURNON LED STRIP - FOR WELCOME
//--------------------------------------------------------------------
void neo_welcome() {
}
//--------------------------------------------------------------------
// TURNON LED STRIP WITH COLOR
//--------------------------------------------------------------------
void neo_color(uint8_t R, uint8_t G, uint8_t B) {
BOOST_ON;
delay(1);
pixels.clear();
for(int i=0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(R, G, B));
}
pixels.setBrightness(50);
delay(2);
pixels.show();
}
//--------------------------------------------------------------------
// TURNOFF LED STRIP
//--------------------------------------------------------------------
void neo_turnoff() {
pixels.clear();
pixels.show();
BOOST_OFF;
}
+175
View File
@@ -0,0 +1,175 @@
//====================================================================
// OLED DISPLAY
//====================================================================
// LIBRARY INCLUDE
//--------------------------------------------------------------------
// Include Libraries
#include <Wire.h>
#include <SSD1306Wire.h>
// Display Instance
SSD1306Wire display(0x3c, 21, 22);
//--------------------------------------------------------------------
// OLED Initialization
//--------------------------------------------------------------------
void oled_init() {
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
}
//--------------------------------------------------------------------
// OLED Display Text
//--------------------------------------------------------------------
void drawTextFlowDemo(String Text) {
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawStringMaxWidth(0, 0, 128, Text);
}
//--------------------------------------------------------------------
// OLED LEFT TEXT Display
//--------------------------------------------------------------------
void drawTextLeft() {
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(0, 10, "Left aligned (0,10)");
}
//--------------------------------------------------------------------
// OLED CENTER TEXT Display
//--------------------------------------------------------------------
void drawTextCenter(int X, int Y, int Size, String Text) {
//display.setFont(ArialMT_Plain_10);
if(Size == 1) {
display.setFont(ArialMT_Plain_10);
} else if(Size == 2) {
display.setFont(ArialMT_Plain_16);
} else if(Size == 3) {
display.setFont(ArialMT_Plain_24);
} else {
display.setFont(ArialMT_Plain_10);
}
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(X, Y, Text);
}
//--------------------------------------------------------------------
// OLED RIGHT TEXT DISPLAY
//--------------------------------------------------------------------
// OLED Display
void drawTextRight() {
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString(128, 33, "Right aligned (128,33)");
}
//--------------------------------------------------------------------
// OLED DRAW RECTANGLE
//--------------------------------------------------------------------
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);
}
//--------------------------------------------------------------------
// OLED DRAW CIRCLE
//--------------------------------------------------------------------
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);
}
}
//--------------------------------------------------------------------
// OLED DRAW PROGRESS BAR
//--------------------------------------------------------------------
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) + "%");
}
//--------------------------------------------------------------------
// OLED CLEAR DISPLAY
//--------------------------------------------------------------------
void oled_clear() {
display.clear();
}
//--------------------------------------------------------------------
// OLED SHOW NEW DATA
//--------------------------------------------------------------------
void oled_show() {
display.display();
}
//--------------------------------------------------------------------
// OLED DISPLAY KICKER WELCOME MESSAGE
//--------------------------------------------------------------------
void kicker_logo() {
// Starting The First Code
oled_clear();
drawTextCenter(128/2, 5, 3, "TAKEONE");
drawTextCenter(128/2, 30, 1, " - T E C H N O L O G Y - ");
drawTextCenter(128/2, 43, 2, "<< KICKER >>");
oled_show();
}
//--------------------------------------------------------------------
// OLED DISPLAY BATTERY LEVEL
//--------------------------------------------------------------------
void battery_level_display() {
// Counting Time
timetick = millis();
// Taking Action Based On Time Count
if(timetick - prevtick >= 5000)
{
prevtick = timetick;
String BV = String(BATTERY,2);
String BC = String(CHARGE, 0);
oled_clear();
drawTextFlowDemo("Battery Level : " + BV + " V Charge Level : " + BC + "%");
drawProgressBarDemo(BC.toInt());
oled_show();
}
}
+240
View File
@@ -0,0 +1,240 @@
//====================================================================
// SDCARD READER FUNCTIONS
//====================================================================
// LIST DIRECTORIES
//--------------------------------------------------------------------
void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if(!root){
Serial.println("Failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
//--------------------------------------------------------------------
// CREATE DIRECTORY
//--------------------------------------------------------------------
void createDir(fs::FS &fs, const char * path){
Serial.printf("Creating Dir: %s\n", path);
if(fs.mkdir(path)){
Serial.println("Dir created");
} else {
Serial.println("mkdir failed");
}
}
//--------------------------------------------------------------------
// REMOVE DIRECTORIES
//--------------------------------------------------------------------
void removeDir(fs::FS &fs, const char * path){
Serial.printf("Removing Dir: %s\n", path);
if(fs.rmdir(path)){
Serial.println("Dir removed");
} else {
Serial.println("rmdir failed");
}
}
//--------------------------------------------------------------------
// READ FILE
//--------------------------------------------------------------------
void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if(!file){
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while(file.available()){
Serial.write(file.read());
}
file.close();
}
//--------------------------------------------------------------------
// WRITE TO A FILE / CREATE A NEW FILE
//--------------------------------------------------------------------
void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("Failed to open file for writing");
return;
}
if(file.print(message)){
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}
//--------------------------------------------------------------------
// WRITE TO THE BOTTOM OF A FILE / CONTINUE WRITING
//--------------------------------------------------------------------
void appendFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)){
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
//--------------------------------------------------------------------
// RENAME A FILE / CHNAGE THE NAME OF A FILE
//--------------------------------------------------------------------
void renameFile(fs::FS &fs, const char * path1, const char * path2){
Serial.printf("Renaming file %s to %s\n", path1, path2);
if (fs.rename(path1, path2)) {
Serial.println("File renamed");
} else {
Serial.println("Rename failed");
}
}
//--------------------------------------------------------------------
// DELETE A FILE
//--------------------------------------------------------------------
void deleteFile(fs::FS &fs, const char * path){
Serial.printf("Deleting file: %s\n", path);
if(fs.remove(path)){
Serial.println("File deleted");
} else {
Serial.println("Delete failed");
}
}
//--------------------------------------------------------------------
// SDCARD TEST ROUTINE
//--------------------------------------------------------------------
void testFileIO(fs::FS &fs, const char * path){
File file = fs.open(path);
static uint8_t buf[512];
size_t len = 0;
uint32_t start = millis();
uint32_t end = start;
if(file){
len = file.size();
size_t flen = len;
start = millis();
while(len){
size_t toRead = len;
if(toRead > 512){
toRead = 512;
}
file.read(buf, toRead);
len -= toRead;
}
end = millis() - start;
Serial.printf("%u bytes read for %u ms\n", flen, end);
file.close();
} else {
Serial.println("Failed to open file for reading");
}
file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("Failed to open file for writing");
return;
}
size_t i;
start = millis();
for(i=0; i<2048; i++){
file.write(buf, 512);
}
end = millis() - start;
Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
file.close();
}
//--------------------------------------------------------------------
// STARTING SDCARD READER
//--------------------------------------------------------------------
void sdcard_init(){
if(!SD.begin()){
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if(cardType == CARD_MMC){
Serial.println("MMC");
} else if(cardType == CARD_SD){
Serial.println("SDSC");
} else if(cardType == CARD_SDHC){
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
//listDir(SD, "/", 0);
//createDir(SD, "/mydir");
//listDir(SD, "/", 0);
//removeDir(SD, "/mydir");
//listDir(SD, "/", 2);
writeFile(SD, "/battery.txt", "Battery Monitoring");
//appendFile(SD, "/hello.txt", "World!\n");
//readFile(SD, "/hello.txt");
//deleteFile(SD, "/foo.txt");
//renameFile(SD, "/hello.txt", "/foo.txt");
//readFile(SD, "/foo.txt");
//testFileIO(SD, "/test.txt");
//Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
//Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
}
+380
View File
@@ -0,0 +1,380 @@
/* ===============================================
* - THIS IS TAKEONE BOARD -
* ===============================================
* MPU9250 Address : 0X69 (ADDRESS)
* OLED 128x64 Address : 0X3C (ADDRESS)
* -----------------------------------------------
* Description : PIN GPIO#
* -----------------------------------------------
* NEOPIXEL LED STRIP : A0/GPIO 26
* PIEZO SENSOR : A2/GPIO 34 - ANALOG ONLY INPUT
* RANDOM SEED : A3/GPIO 39 - ANALOG ONLY INPUT
* https://www.aliexpress.com/item/32813561581.html?spm=a2g0s.9042311.0.0.48644c4dfH5L8o
* BUTTONS : TLB (35), BLB (15), CB (33), BRB (27), TRB(12)
* I2C PIN : SDA 21, SCL 22 (DIGITAL)
* SPI PIN : CS 5, SCK 18, MISO 19, MOSI 23 (DIGITAL)
* BOOST CONTROL PIN : 14 (DIGITAL) OUTPUT ON PADS (TP1 +5V, TP2 GND)
* LED PIN : 13 (DIGITAL)
* BATTERY MONITOR PIN : 35 (ANALOG)
* -----------------------------------------------
* BUTTONS PINOUTS : PIN GPIO
* -----------------------------------------------
* TOP LEFT BUTTON : 15 - SWITCH PREVIOUS MENU / CHANGE PARAMETER
* TOP RIGHT BUTTON : 12 - SWITCH NEXT MENU / CHANGE PARAMETER
* BOTTOM LEFT BUTTON : 32 - POWER INC & BACK & INC PARAMETER
* BOTTOM RIGHT BUTTON : 27 - POWER DEC & SAVE & DEC PARAMETER
* CENTER BUTTON : 33 - POWER ON & SELECT
*/
// Include Library
#include <FS.h>
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <Button.h>
#include <ArduinoJson.h>
#include <BluetoothSerial.h>
#include <Adafruit_NeoPixel.h>
//----------------------------------------------------------------
// Communication Models
//----------------------------------------------------------------
// Serial Port As Terminal
#define terminal Serial
// Clearing Terminal Remainning
#define flush_terminal while(terminal.available()) { terminal.read(); }
// Bluetooth Object
BluetoothSerial bluetooth;
// Bluetooth Connection Status
bool bluetooth_conn = false;
// Clearing Bluetooth Remainning
#define flush_bluetooth while(bluetooth.available()) { bluetooth.read(); }
// Fixed JSON Strings
#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 250
//----------------------------------------------------------------
// TAKEONE BOARD HARDWARE DEFINITIONS
//----------------------------------------------------------------
// On Board MPU9250 I2C Address
#define MPU_ADD 0x69
// Connected I2C OLED Address
#define OLED_ADD 0x3C
// Connected I2C LCD Address
#define LCD_ADD 0x2C
// Define Impact Sensor
#define PIEZO analogRead(34)
#define VOLTS mapf(PIEZO, 0, 4095, 0, 3.3)
// Panel Push Buttons Pin Assignment Sections
Button TLB(32); // Top Left Button
Button BLB(15); // Bottom Left Button
Button CB(33); // Center Button
Button BRB(27); // Bottom Right Button
Button TRB(12); // Top Right Button 33
// Panel Push Buttons Command Assignment
#define PrvMenu TLB.pressed()
#define NxtMenu TRB.pressed()
#define SelMenu CB.pressed()
#define IncBtn BLB.pressed()
#define DecBtn BRB.pressed()
// NEO Pixel Strip Section
#define NEOPIXEL 26 // Define Pin Number
#define NUMPIXELS 22 // Define Number Of LED's
// Battery Level Monitoring
#define BATTERY (float)(((analogRead(35) * (3.3 / 4096)) * 2) + 0.31)
#define CHARGE mapf(BATTERY, 3.27, 4.31, 0, 100)
// Power Latch Holder Pin
#define POWER_PIN 14
#define POWER_SETUP pinMode(POWER_PIN, OUTPUT)
#define POWER_ON digitalWrite(POWER_PIN, HIGH)
#define POWER_OFF digitalWrite(POWER_PIN, LOW)
// 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)
// On Board LED Setup & Control
#define LED_PIN 13
#define LED_SETUP pinMode(LED_PIN, OUTPUT)
#define LED_ON digitalWrite(LED_PIN, HIGH)
#define LED_OFF digitalWrite(LED_PIN, LOW)
// For Timming
unsigned long timetick = 0;
unsigned long prevtick = 0;
/* Display Change Flag Explaination
* This flag is used to prevent continous writing to the oled display
* once a change has happened we drive the display_changed to false this will tell
* the micro controller that a need to write new data is needed, after writing the new data to display
* then we drive the display_changed to true that means the nessary changes has been written
*/
bool display_changed = false;
uint8_t menu_count = 0;
//--------------------------------------------------------------------
// Bluetooth Connection Status Call Back
//--------------------------------------------------------------------
void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
{
if(event == ESP_SPP_SRV_OPEN_EVT){
//Serial.println("Client Connected");
bluetooth_conn = true;
}
if(event == ESP_SPP_CLOSE_EVT ){
//Serial.println("Client disconnected");
bluetooth_conn = false;
}
}
//--------------------------------------------------------------------
// CHIP MAIN SETUP
//--------------------------------------------------------------------
void setup()
{
// Starting Serial Communications
terminal.begin(115200); // Starting Main USB UART Communication Interface
terminal.println(); // New Line Seperator 1
terminal.println(); // New Line Seperator 2
// Starting The Bluetooth Module
if(!bluetooth.begin("KICKER")) {
terminal.println("An error occurred initializing Bluetooth");
} else {
terminal.println("Bluetooth initialized");
}
// Checking The Connection Status
bluetooth.register_callback(callback);
// Pin Assignments
BOOST_SETUP; // Boost Converter Pin Setup
LED_SETUP; // OnBoard LED Pin Setup
// Turn Off The Pins
BOOST_OFF; // Turn Off The Booster
LED_OFF; // Turn Off The LED
// The Five Panel Buttons
TLB.begin(); // Setup The Top Left Panel Button
BLB.begin(); // Setup The Bottom Left Panel Button
BRB.begin(); // Setup The Bottom Right Panel Button
TRB.begin(); // Setup The Top Right Panel Button
CB.begin(); // Setup The Center Panel Button
// I2C Section
Wire.begin(); // Starting I2C Communication Interface
scanI2C(); // Starting I2C Communicatable Devices Detection
oled_init(); // Starting OLED Display I2C Communications
kicker_logo(); // Display Kicker Main Logo Display
// Starting SDCARD Interface
sdcard_init(); // Starting SDCARD Reader Communication Protocol
// Neo Pixel Section
neo_init(); // Starting Neo Pixel
neo_color(0, 0, 0); // Clear All LED's
delay(10); // Delay 10 Milli Seconds
neo_color(255, 0, 0); // Display RED LED Strip
delay(1000); // Delay 1 Second
neo_color(0, 255, 0); // Display GREEN LED Strip
delay(1000); // Delay 1 Second
neo_color(0, 0, 255); // Display BLUE LED Strip
delay(1000); // Delay 1 Second
neo_turnoff(); // Power Off Neo Pixels & Their Power
// THE PROGRAM STATE = MENU
display_changed = false; // Allow Display Change
}
//--------------------------------------------------------------------
// CHIP MAIN PROGRAM
//--------------------------------------------------------------------
void loop()
{
// Checking The Bluetooth Connection
bluetooth.register_callback(callback);
// Condition The Preiority Is For The Bluetooth
if(bluetooth_conn == true)
{
// Wait For Game JSON String
read_game_json();
}
// Condition To Stand Alone Mode - Manual Mode
else
{
// Previous Menu ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(TLB.pressed()) {
if(menu_count == 0) {
menu_count = 4;
} else {
menu_count--;
}
display_changed = false;
}
// Next Menu ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(TRB.pressed()) {
if(menu_count == 4) {
menu_count = 0;
} else {
menu_count++;
}
display_changed = false;
}
// Select A Game ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(CB.pressed()) {
select_game();
}
// Increase Power ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(BLB.pressed()) {
}
// Decrease Power ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(BRB.pressed()) {
}
// Display Target Menu ~~~~~~~~~~~~~~~~~~~~~~~~~~
switch_menu();
}
}
//======================================================
// Read Game JSON String Command From Mobile
//======================================================
void read_game_json()
{
// Reading From Bluetooth
if(bluetooth.available())
{
// Reading Message From Bluetooth
String msg = bluetooth.readStringUntil('\n');
// Clearing The Receive Buffer
flush_bluetooth;
// Clean The Message
msg.trim();
// Display On Sertial
terminal.println(msg);
// Parsing Objects Message Objects
DynamicJsonDocument root(capacity);
// Deserialize the JSON document
DeserializationError error = deserializeJson(root, msg);
// If The System Understood The Message Clearly
if(!error)
{
// Sending Status Message
terminal.println(UNDERSTOOD);
bluetooth.println(UNDERSTOOD);
// Reading Game Name & Settings
String game = root["gm"];
String settings = root["set"];
// Clearing JSON Document Buffer
root.clear();
// Entering To Game Selector
game_selector(game, settings);
}
else
{
// Display On Serial
terminal.println(JSON_ERROR);
}
// Bluetooth Serial RXD Flush
flush_bluetooth;
}
}
//======================================================
// Game Selector Function
//======================================================
void game_selector(String game, String settings)
{
// Kick Counter Game
if(game == "ct") {
terminal.println(GM_COUNTER);
//game_counter(settings);
return;
}
// Time Attack Game
else if(game == "ta") {
terminal.println(GM_TATTACK);
//game_timeAttack(settings);
return;
}
// Time Attack Game
else if(game == "ra") {
terminal.println(GM_REACTION);
//game_reaction(settings);
return;
}
// Unknown Game
else {
terminal.println(UNKNOWN_GAME);
bluetooth.println(UNKNOWN_GAME);
return;
}
}