<commit_before>/*
   FX-SaberOS V1.0

   released on: 7 October 2017
   author: 		<NAME> (<EMAIL>) and <NAME> (<EMAIL>)
   Source : 	https://github.com/Protonerd/FX-SaberOS
   Description:	Operating System for Arduino based LightSaber

   This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
   To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
*/
/***************************************************************************************************/

#include <Arduino.h>
#include <I2Cdev.h>
#include <MPU6050_6Axis_MotionApps20.h>
#include <EEPROMex.h>
#include <OneButton.h>
#include <LinkedList.h>
#include <avr/wdt.h>

#include "Buttons.h"
#include "Config_HW.h"
#include "Config_SW.h"
#include "ConfigMenu.h"
#include "Light.h"
#include "Soundfont.h"
/*
* DFPLAYER variables
*/
#ifdef OLD_DPFPLAYER_LIB
  #include "DFPlayer_Mini_Mp3.h"
  //SoftwareSerial mp3player(DFPLAYER_TX, DFPLAYER_RX); // TX, RX
  SoftwareSerial mp3player(7, 8); // TX, RX
#else
  #include <DFPlayerSerial.h>
  #include <DFPlayer.h>
  DFPlayer dfplayer;
#endif

SoundFont soundFont;
unsigned long sndSuppress = millis();
unsigned long sndSuppress2 = millis();
unsigned long clashSndSuppress = millis();
#ifdef DEEP_SLEEP
  unsigned long sleepTimer = millis();
#endif
bool hum_playing = false; // variable to store whether hum is being played
#ifdef JUKEBOX
bool jukebox_play = false; // indicate whether a song is being played in JukeBox mode
uint8_t jb_track;  // sound file track number in the directory designated for music playback
#endif
#ifdef CROSSGUARDSABER
bool mainignition_done=false;
#endif

/***************************************************************************************************
 * Saber Finite State Machine Custom Type and State Variable
 */
// global Saber state and Sub State variables
extern SaberStateEnum SaberState;
extern SaberStateEnum PrevSaberState;
extern ActionModeSubStatesEnum ActionModeSubStates;
extern ConfigModeSubStatesEnum ConfigModeSubStates;
extern ActionModeSubStatesEnum PrevActionModeSubStates;
extern ConfigModeSubStatesEnum PrevConfigModeSubStates;
//extern SubStateEnum SubState;
/***************************************************************************************************
 * Motion detection Variables
*/
MPU6050 mpu;
#ifdef CLASH_DET_MPU_INT
  I2Cdev i2ccomm;
#endif
// MPU control/status vars
volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
bool dmpReady = false;  // set true if DMP init was successful
uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
uint8_t fifoBuffer[64]; // FIFO storage buffer
uint16_t mpuFifoCount;     // count of all bytes currently in FIFO
// orientation/motion vars
Quaternion curRotation;           // [w, x, y, z]         quaternion container
Quaternion prevRotation;           // [w, x, y, z]         quaternion container
static Quaternion prevOrientation;  // [w, x, y, z]         quaternion container
static Quaternion curOrientation; // [w, x, y, z]         quaternion container
VectorInt16 curAccel;
VectorInt16 prevAccel;
VectorInt16 curDeltAccel;
VectorInt16 prevDeltAccel;

/***************************************************************************************************
 * LED String variables
 */
  cRGB currentColor;
#if defined LEDSTRINGS
  #ifdef DIYINO_PRIME
    uint8_t ledPins[] = {LS1, LS2, LS3, LS4, LS5, LS6};
  #else if defined DIYINO_STARDUST_V2 or defined DIYINO_STARDUST_V3
    uint8_t ledPins[] = {LS1, LS2, LS3};
  #endif
  uint8_t blasterPin;
#endif
#if defined STAR_LED
  uint8_t ledPins[] = {LED_RED, LED_GREEN, LED_BLUE};
#endif
extern bool fireblade;
#if defined PIXELBLADE or defined ADF_PIXIE_BLADE
  #ifdef DIYINO_PRIME
    uint8_t ledPins[] = {LS1, LS2, LS3, LS4, LS5, LS6};
  #else if defined DIYINO_STARDUST_V2 or defined DIYINO_STARDUST_V3
    uint8_t ledPins[] = {LS1, LS2, LS3};
  #endif
  WS2812 pixels(NUMPIXELS);

  cRGB color;
  uint8_t blasterPixel;
#endif
#ifdef PIXEL_ACCENT
  WS2812 accentPixels(NUM_ACCENT_PIXELS);
#endif
uint8_t clash = 0;
bool lockuponclash = false;
uint8_t randomBlink = 0;
/***************************************************************************************************
 * Buttons variables
 */
OneButton mainButton(MAIN_BUTTON, true);
#ifndef SINGLEBUTTON
OneButton lockupButton(AUX_BUTTON, true);
#endif
// replaced by Saber State Machine Variables
//bool actionMode = false; // Play with your saber
//bool configMode = false; // Configure your saber
//static bool ignition = false;
//static bool browsing = false;

/***************************************************************************************************
 * ConfigMode Variables
 */
int8_t modification = 0;
int8_t prev_modification = 0;
int16_t value = 0;
uint8_t menu = 0;
bool enterMenu = false;
bool changeMenu = false;
bool play = false;
unsigned int configAdress = 0;
volatile uint8_t portbhistory = 0xFF;     // default is high because the pull-up

struct StoreStruct {
	// This is for mere detection if they are our settings
	char version[5];
	// The settings
	uint8_t volume;// 0 to 31
	uint8_t soundFont;// as many as Sound font you have defined in Soundfont.h Max:253
  struct Profile {
    cRGB mainColor;
    cRGB clashColor;
    cRGB blasterboltColor;
    uint16_t swingSensitivity;
    uint8_t flickerType;
    uint8_t poweronoffType;
  }sndProfile[SOUNDFONT_QUANTITY];
}storage;


/***************************************************************************************************
 * Function Prototypes
 * The following prototypes are not correctly generated by Arduino IDE 1.6.5-r5 or previous
 */
inline void printQuaternion(Quaternion quaternion, long multiplier);
inline void printAcceleration(VectorInt16 aaWorld);

// ====================================================================================
// ===        	       	   			SETUP ROUTINE  	 	                			===
// ====================================================================================
void setup() {
  #ifdef PIXEL_ACCENT
    accentPixels.setOutput(PIXEL_ACCENT_DATA);
  #endif
	// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
	Wire.begin();
	TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Comment this line if having compilation difficulties with TWBR.
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
			Fastwire::setup(400, true);
#endif
	// Serial line for debug
	Serial.begin(115200);

	/***** LOAD CONFIG *****/<commit_msg>add accent RG swap option<commit_after>/*
   FX-SaberOS V1.0

   released on: 7 October 2017
   author: 		<NAME> (<EMAIL>) and <NAME> (<EMAIL>)
   Source : 	https://github.com/Protonerd/FX-SaberOS
   Description:	Operating System for Arduino based LightSaber

   This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
   To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
*/
/***************************************************************************************************/

#include <Arduino.h>
#include <I2Cdev.h>
#include <MPU6050_6Axis_MotionApps20.h>
#include <EEPROMex.h>
#include <OneButton.h>
#include <LinkedList.h>
#include <avr/wdt.h>

#include "Buttons.h"
#include "Config_HW.h"
#include "Config_SW.h"
#include "ConfigMenu.h"
#include "Light.h"
#include "Soundfont.h"
/*
* DFPLAYER variables
*/
#ifdef OLD_DPFPLAYER_LIB
  #include "DFPlayer_Mini_Mp3.h"
  //SoftwareSerial mp3player(DFPLAYER_TX, DFPLAYER_RX); // TX, RX
  SoftwareSerial mp3player(7, 8); // TX, RX
#else
  #include <DFPlayerSerial.h>
  #include <DFPlayer.h>
  DFPlayer dfplayer;
#endif

SoundFont soundFont;
unsigned long sndSuppress = millis();
unsigned long sndSuppress2 = millis();
unsigned long clashSndSuppress = millis();
#ifdef DEEP_SLEEP
  unsigned long sleepTimer = millis();
#endif
bool hum_playing = false; // variable to store whether hum is being played
#ifdef JUKEBOX
bool jukebox_play = false; // indicate whether a song is being played in JukeBox mode
uint8_t jb_track;  // sound file track number in the directory designated for music playback
#endif
#ifdef CROSSGUARDSABER
bool mainignition_done=false;
#endif

/***************************************************************************************************
 * Saber Finite State Machine Custom Type and State Variable
 */
// global Saber state and Sub State variables
extern SaberStateEnum SaberState;
extern SaberStateEnum PrevSaberState;
extern ActionModeSubStatesEnum ActionModeSubStates;
extern ConfigModeSubStatesEnum ConfigModeSubStates;
extern ActionModeSubStatesEnum PrevActionModeSubStates;
extern ConfigModeSubStatesEnum PrevConfigModeSubStates;
//extern SubStateEnum SubState;
/***************************************************************************************************
 * Motion detection Variables
*/
MPU6050 mpu;
#ifdef CLASH_DET_MPU_INT
  I2Cdev i2ccomm;
#endif
// MPU control/status vars
volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
bool dmpReady = false;  // set true if DMP init was successful
uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
uint8_t fifoBuffer[64]; // FIFO storage buffer
uint16_t mpuFifoCount;     // count of all bytes currently in FIFO
// orientation/motion vars
Quaternion curRotation;           // [w, x, y, z]         quaternion container
Quaternion prevRotation;           // [w, x, y, z]         quaternion container
static Quaternion prevOrientation;  // [w, x, y, z]         quaternion container
static Quaternion curOrientation; // [w, x, y, z]         quaternion container
VectorInt16 curAccel;
VectorInt16 prevAccel;
VectorInt16 curDeltAccel;
VectorInt16 prevDeltAccel;

/***************************************************************************************************
 * LED String variables
 */
  cRGB currentColor;
#if defined LEDSTRINGS
  #ifdef DIYINO_PRIME
    uint8_t ledPins[] = {LS1, LS2, LS3, LS4, LS5, LS6};
  #else if defined DIYINO_STARDUST_V2 or defined DIYINO_STARDUST_V3
    uint8_t ledPins[] = {LS1, LS2, LS3};
  #endif
  uint8_t blasterPin;
#endif
#if defined STAR_LED
  uint8_t ledPins[] = {LED_RED, LED_GREEN, LED_BLUE};
#endif
extern bool fireblade;
#if defined PIXELBLADE or defined ADF_PIXIE_BLADE
  #ifdef DIYINO_PRIME
    uint8_t ledPins[] = {LS1, LS2, LS3, LS4, LS5, LS6};
  #else if defined DIYINO_STARDUST_V2 or defined DIYINO_STARDUST_V3
    uint8_t ledPins[] = {LS1, LS2, LS3};
  #endif
  WS2812 pixels(NUMPIXELS);

  cRGB color;
  uint8_t blasterPixel;
#endif
#ifdef PIXEL_ACCENT
  WS2812 accentPixels(NUM_ACCENT_PIXELS);
#endif
uint8_t clash = 0;
bool lockuponclash = false;
uint8_t randomBlink = 0;
/***************************************************************************************************
 * Buttons variables
 */
OneButton mainButton(MAIN_BUTTON, true);
#ifndef SINGLEBUTTON
OneButton lockupButton(AUX_BUTTON, true);
#endif
// replaced by Saber State Machine Variables
//bool actionMode = false; // Play with your saber
//bool configMode = false; // Configure your saber
//static bool ignition = false;
//static bool browsing = false;

/***************************************************************************************************
 * ConfigMode Variables
 */
int8_t modification = 0;
int8_t prev_modification = 0;
int16_t value = 0;
uint8_t menu = 0;
bool enterMenu = false;
bool changeMenu = false;
bool play = false;
unsigned int configAdress = 0;
volatile uint8_t portbhistory = 0xFF;     // default is high because the pull-up

struct StoreStruct {
	// This is for mere detection if they are our settings
	char version[5];
	// The settings
	uint8_t volume;// 0 to 31
	uint8_t soundFont;// as many as Sound font you have defined in Soundfont.h Max:253
  struct Profile {
    cRGB mainColor;
    cRGB clashColor;
    cRGB blasterboltColor;
    uint16_t swingSensitivity;
    uint8_t flickerType;
    uint8_t poweronoffType;
  }sndProfile[SOUNDFONT_QUANTITY];
}storage;


/***************************************************************************************************
 * Function Prototypes
 * The following prototypes are not correctly generated by Arduino IDE 1.6.5-r5 or previous
 */
inline void printQuaternion(Quaternion quaternion, long multiplier);
inline void printAcceleration(VectorInt16 aaWorld);

// ====================================================================================
// ===        	       	   			SETUP ROUTINE  	 	                			===
// ====================================================================================
void setup() {
  #ifdef PIXEL_ACCENT
    accentPixels.setOutput(PIXEL_ACCENT_DATA);
    #ifdef SWAP_PIXEL_RG
      accentPixels.setColorOrderRGB();
    #endif
  #endif
	// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
	Wire.begin();
	TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Comment this line if having compilation difficulties with TWBR.
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
			Fastwire::setup(400, true);
#endif
	// Serial line for debug
	Serial.begin(115200);

	/***** LOAD CONFIG *****/
{'fraction_non_alphanumeric': 0.2051961433723412, 'fraction_numerical': 0.022079929344226097, 'mean_word_length': 4.902693310165074, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7608412', 'n_tokens_mistral': 4459, 'n_tokens_neox': 4068, 'n_words': 1248}