PadController/PadController.ino
2022-08-21 14:00:32 +02:00

211 lines
4.6 KiB
C++

#include "static.h"
#include "tools.h"
#define u8 uint8_t
u8 mode = mode_bluetooth; //0: nrf, 1: ble, 2: debug
#include <BleX.h>
BleX blex("Gamecontrolle","pikiexe");
//loop time
unsigned long lastTime = 0;
unsigned long currentTime = 0;
//light
CRGB leds[NUM_LEDS];
bool firstLaunch = true;
#include <Preferences.h>
Preferences preferences;
#include "input.h"
#include "mode.h"
bool radio_connected = false;
bool bluetooth_connected = false;
void setup() {
pinMode(relay_1, OUTPUT);
digitalWrite(relay_1, HIGH);
delay(100);
pinMode(relay_2, OUTPUT);
Serial.begin(115200);
//init storage and reading settings
preferences.begin("data", false);
firstLaunch = preferences.getUChar("first",1);
preferences.putUChar("first",0);
//mode = preferences.getChar("mode",mode_bluetooth);
delay(100);
iniInput();
iniNRF();
iniBLE();
changeMode(mode_bluetooth);
}
bool power_btn_last_state = true;
bool power_btn_dble_possible = false;
unsigned long power_btn_since = 0;
bool shutdown = false;
unsigned long shutdown_since = 0;
u8 rpiStatus = rpi_off;
bool rpiTarget = false; //true -> running
unsigned long rpiSince = 0;
void loop() {
handelInput();
handelRpi();
if(power_btn_last_state != state[4]){
if(power_btn_since==0){
power_btn_since=millis();
power_btn_last_state = false;
}else if(power_btn_last_state){
/*if(shutdown== true){
digitalWrite(relay_1, LOW);
delay(10000);
}*/
unsigned long click_time = millis()-power_btn_since;
if(click_time<short_click){
if(power_btn_dble_possible){
//TODO: changeMode
Serial.println("changeMode");
//rpiBoot();//TODO: delete
nextMode();
power_btn_dble_possible = false;
}else{
power_btn_dble_possible = true;
}
}else /*if(click_time<long_click)*/{
Serial.println("<sys>shutdown");
SYSshutdown();
}
}
power_btn_last_state = state[4];
power_btn_since=millis();
}
if(!power_btn_last_state && power_btn_dble_possible && (millis()-power_btn_since >= double_click_brake)){
power_btn_since=millis();
power_btn_dble_possible = false;
Serial.println("single_click");
innerModeClick();
}
/*if(shutdown&&millis()-shutdown_since>30000){
digitalWrite(relay_1, LOW);
delay(10000);
}*/
}
void SYSshutdown(){
LEDsetShutdown();
NRFshutdown();
if(rpiStatus == rpi_starting||rpiStatus==rpi_on||rpiStatus==rpi_stoping){
rpiShutdown();
power_btn_last_state = false;
bool crash = false;
while(rpiStatus!=rpi_off&&rpiStatus!=rpi_unknown&&!crash){
btns.readAll(state,values);
if(power_btn_last_state != state[4]){
if(!state[4]){
crash = true;
}
}
power_btn_last_state = state[4];
handelRpi();
}
}
delay(500);
digitalWrite(relay_2, LOW);
digitalWrite(relay_1, LOW);
delay(10000);
}
void rpiShutdown(){
rpiTarget = false;
if(rpiStatus==rpi_unknown){
rpiStatus=rpi_off;
}
handelRpi();
}
void rpiBoot(){
rpiTarget = true;
handelRpi();
}
#include "handelSerial.h"
void handelRpi(){
if(rpiStatus==rpi_starting){
if(millis()-rpiSince>rpi_max_boot_time){
rpiStatus = rpi_unknown;
rpiSince = millis();
}
}else if(rpiStatus==rpi_stoping){
if(millis()-rpiSince>rpi_stop_time){
rpiStatus = rpi_off;
digitalWrite(relay_2, LOW);
rpiSince = millis();
}
}else if(rpiStatus == rpi_off){
if(rpiTarget){
rpiStatus = rpi_starting;
rpiSince = millis();
digitalWrite(relay_2, HIGH);
}
}else if(rpiStatus == rpi_on){
if(!rpiTarget){
Serial.println("<sys>shutdown");
rpiStatus = rpi_stoping;
rpiSince = millis();
}
if(millis()-rpiSince>rpi_max_black_time){
rpiStatus = rpi_unknown;
rpiSince = millis();
}
}
if (Serial.available()>0){
handelSerial();
}
//digitalWrite(relay_2, HIGH);
}
/*void handelSerial(){
byte cmd = Serial.read();
if(cmd==cmd_check){
Serial.write(cmd_check);
Serial.write(Serial_end);
}
if(cmd==cmd_setMode){
byte nmode = Serial.read();
Serial.write(cmd_setMode);
if(nmode<3){
mode = nmode;
preferences.putChar("mode",mode);
Serial.write(1);
}else{
Serial.write(0);
}
Serial.write(Serial_end);
}
if(cmd==cmd_getStatus){
Serial.write(cmd_getStatus);
if(mode == mode_radio){
Serial.write(radio_connected&1);
}else if(mode == mode_bluetooth){
Serial.write(blex.isConnected()&1);
}else{
Serial.write(Serial_end);
}
}
while(Serial.available()>0&&Serial.read()!=Serial_end){delayMicroseconds(10);}
}
*/