This commit is contained in:
jusax23 2022-08-21 14:00:32 +02:00
commit fbb17037ad
9 changed files with 1659 additions and 0 deletions

211
PadController.ino Normal file
View file

@ -0,0 +1,211 @@
#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);}
}
*/

182
ble.h Normal file
View file

@ -0,0 +1,182 @@
#define BLEcacheMediaKey 16
#define BLEcacheGamepadbtns 14
#define BLEcacheHAT 4
#define BLEcacheMouse 5
#define BLEcacheKeyboard 255
#define BLEAnalog 12
byte BLEBTNcacheMediaKey[BLEcacheMediaKey];
byte BLEBTNcacheGamepadbtns[BLEcacheGamepadbtns];
byte BLEBTNcacheHAT[BLEcacheHAT];
byte BLEBTNcacheMouse[BLEcacheMouse];
byte BLEBTNcacheKeyboard[BLEcacheKeyboard];
int16_t BLEAnalogSet[BLEAnalog];
int32_t BLEAnalogincrement[BLEAnalog];
String BLEProgramCache[5];
int BLEProgramActive = 0;
void iniBLE(bool first = true){
for (int i = 0; i < BLEcacheMediaKey; i++) {
BLEBTNcacheMediaKey[i] = 0;
}
for (int i = 0; i < BLEcacheGamepadbtns; i++) {
BLEBTNcacheGamepadbtns[i] = 0;
}
for (int i = 0; i < BLEcacheHAT; i++) {
BLEBTNcacheHAT[i] = 0;
}
for (int i = 0; i < BLEcacheMouse; i++) {
BLEBTNcacheMouse[i] = 0;
}
for (int i = 0; i < BLEcacheKeyboard; i++) {
BLEBTNcacheKeyboard[i] = 0;
}
for (byte i = 0; i < BLEAnalog; i++) {
BLEAnalogSet[i] = 0;
BLEAnalogincrement[i] = 0;
}
if(first)blex.begin();
loadProgram(BLEProgramCache[BLEProgramActive]);
}
/*
[1] //scancode
[0-255]
[2] //MediaKey
[0-15]
[3] //Gamepadbtns
[0-13]
[4] //HAT
[0-3]
[5] //Mouse
[0-4]
*/
void BLEpress(byte type, byte btn){
if(type == 1){ //scancode [0-255]
if(BLEBTNcacheKeyboard[btn]<255)BLEBTNcacheKeyboard[btn]++;
blex.press(btn);
}
if(type == 2){ //MediaKey [0-15]
if(BLEBTNcacheMediaKey[btn]<255)BLEBTNcacheMediaKey[btn]++;
}
if(type == 3){ //Gamepadbtns [0-13]
if(BLEBTNcacheGamepadbtns[btn]<255)BLEBTNcacheGamepadbtns[btn]++;
}
if(type == 4){ //HAT [0-3]
if(BLEBTNcacheHAT[btn]<255)BLEBTNcacheHAT[btn]++;
}
if(type == 5){ //Mouse[0-4]
if(BLEBTNcacheMouse[btn]<255)BLEBTNcacheMouse[btn]++;
}
}
void BLErelese(byte type, byte btn){
if(type == 1){ //scancode [0-255]
if(BLEBTNcacheKeyboard[btn]>0)BLEBTNcacheKeyboard[btn]--;
if(BLEBTNcacheKeyboard[btn]==0)blex.release(btn);
}
if(type == 2&&btn<=15){ //MediaKey [0-15]
if(BLEBTNcacheMediaKey[btn]>0)BLEBTNcacheMediaKey[btn]--;
}
if(type == 3&&btn<=13){ //Gamepadbtns [0-13]
if(BLEBTNcacheGamepadbtns[btn]>0)BLEBTNcacheGamepadbtns[btn]--;
}
if(type == 4&&btn<=3){ //HAT [0-3]
if(BLEBTNcacheHAT[btn]>0)BLEBTNcacheHAT[btn]--;
}
if(type == 5&&btn<=4){ //Mouse[0-4]
if(BLEBTNcacheMouse[btn]>0)BLEBTNcacheMouse[btn]--;
}
}
void BLEsetAxis(byte axe, int16_t value){
BLEAnalogSet[axe] = value;
}
void BLEincreseAxis(byte axe, int16_t value){
int32_t now = BLEAnalogincrement[axe];
now+=(int32_t) value;
if(now>65536)now = 65536;
if(now<-65536)now = -65536;
NRFAnalogincrement[axe] = (int32_t) now;
}
int16_t BLEcalcAxis(byte i){
int32_t now = (int32_t)BLEAnalogSet[i];
now+=(int32_t) BLEAnalogincrement[i];
if(now>32767)now = 32767;
if(now<-32767)now = -32767;
//if(NRFAnalogSet[i] == 0){
NRFAnalogincrement[i] = 0;
//}
NRFAnalogSet[i] = 0;
return (int16_t) now;
}
uint16_t mediaLast = 0;
int16_t BlEAnalogCache[BLEAnalog];
void BLEsend(){
uint8_t mbuttons = 0;
uint16_t k_16 = 0;
/*for (byte i = 0; i < BLEcacheKeyboard; i++) {
if(BLEBTNcacheKeyboard[i]>0){
}
}*/
for (byte i = 0; i < BLEcacheMediaKey; i++) {
if(BLEBTNcacheMediaKey[i]>0){
k_16 |= (uint16_t)1<<i;
//BLEBTNcacheMediaKey[i] = 0;
}
}
for (byte i = 0; i < BLEcacheGamepadbtns; i++) {
if(BLEBTNcacheGamepadbtns[i]>0)blex.gpress((uint16_t)1<<i,false);
else blex.grelease((uint16_t)1<<i,false);
}
for (byte i = 0; i < BLEcacheHAT; i++) {
if(BLEBTNcacheHAT[i]>0)blex.pressHat(i,false);
else blex.releaseHat(i,false);
}
for (byte i = 0; i < BLEcacheMouse; i++) {
if(BLEBTNcacheMouse[i]>0){
mbuttons |= 1<<i;
}
}
//lx, ly, rx, ry, lt, rt, lr, rr, mx, my, sm, sh
blex.setAxes(BLEcalcAxis(0),BLEcalcAxis(1),BLEcalcAxis(2),BLEcalcAxis(3),BLEcalcAxis(4),BLEcalcAxis(5),false);
BLEcalcAxis(6);
BLEcalcAxis(7);
blex.move(
max(min((float)BLEcalcAxis(8)*currentTime/1000.0/10.0,127.0),-127.0),
max(min((float)-BLEcalcAxis(9)*currentTime/1000.0/10.0,127.0),-127.0),
max(min((float)BLEcalcAxis(10)*currentTime/1000.0/200.0,127.0),-127.0),
max(min((float)BLEcalcAxis(11)*currentTime/1000.0/200.0,127.0),-127.0),
mbuttons);
if(k_16!=mediaLast){
mediaLast = k_16;
MediaKeyReport _mediaKeyReport;
_mediaKeyReport[0] = (uint8_t)((k_16 & 0xFF00) >> 8);
_mediaKeyReport[1] = (uint8_t)(k_16 & 0x00FF);
blex.sendReport(&_mediaKeyReport);
}
blex.sendGamepadReport();
leds[21] = blex.isConnected() ? BLEColorConnected : BLEColorNotConnected;
}
void BLEnextProgram(){
BLEProgramActive++;
if(BLEProgramActive>4)BLEProgramActive = 0;
if(BLEProgramActive<0)BLEProgramActive = 0;
while(BLEProgramActive!=0&&BLEProgramCache[BLEProgramActive].length()){
BLEProgramActive++;
if(BLEProgramActive>4)BLEProgramActive = 0;
}
loadProgram(BLEProgramCache[BLEProgramActive]);
}
void BLEclick(){
BLEnextProgram();
}

184
handelSerial.h Normal file
View file

@ -0,0 +1,184 @@
void handelSerial(){
String serialin = Serial.readStringUntil('\n');
if(rpiStatus == rpi_on){
rpiSince = millis();
}
if(rpiStatus == rpi_starting){
rpiStatus = rpi_on;
rpiSince = millis();
}
if(!serialin.startsWith("<"))return;
String command = serialin.substring(1,serialin.indexOf(">"));
String goonin = serialin.substring(serialin.indexOf(">")+1);
if(command == "ping"){
Serial.println("<ok>");
return;
}
if(command=="setBLE"){
changeMode(mode_bluetooth);
return;
}
if(command=="setNRF"){
changeMode(mode_radio);
//goonin == "0" //idel
if(goonin=="1"){ //scan
}
if(goonin=="2"){ //auto connect
}
return;
}
if(command=="readAnalogRaw"){
Serial.println(readAnalog(hextonum(goonin),true));
}
if(command=="readAnalog"){
Serial.println(readAnalog(hextonum(goonin)));
}
if(command=="setDEBUG"){
changeMode(mode_debug);
return;
}
if(command=="readMPU"){
if(NRFisConnected==1){
uint16_t buf = 1;
uint16_t kind = goonin.toInt();
if(kind>2){
Serial.println("<readMPU>error");
return;
}
buf |= kind<<8;
if(sendRCdata(&buf,2)){
Serial.println("<readMPU>send");
}else{
Serial.println("<readMPU>lost");
}
}else{
Serial.println("<readMPU>noconn");
}
return;
}
if(command=="readCOMP"){
if(NRFisConnected==1){
uint16_t buf = 2;
if(sendRCdata(&buf,2)){
Serial.println("<readCOMP>send");
}else{
Serial.println("<readCOMP>lost");
}
}else{
Serial.println("<readCOMP>noconn");
}
return;
}
if(command=="readGNSS"){
if(NRFisConnected==1){
uint16_t buf = 3;
uint16_t kind = goonin.toInt();
if(kind>7){
Serial.println("<readGNSS>error");
return;
}
buf |= kind<<8;
if(sendRCdata(&buf,2)){
Serial.println("<readGNSS>send");
}else{
Serial.println("<readGNSS>lost");
}
}else{
Serial.println("<readGNSS>noconn");
}
return;
}
if(command=="readAnalogDesc"){
if(NRFisConnected==1){
uint16_t buf = 50;
uint16_t kind = goonin.toInt();
if(kind>11){
Serial.println("<readAnalogDesc>error");
return;
}
buf |= kind<<8;
if(sendRCdata(&buf,2)){
Serial.println("<readAnalogDesc>send");
}else{
Serial.println("<readAnalogDesc>lost");
}
}else{
Serial.println("<readAnalogDesc>noconn");
}
return;
}
if(command=="readButtonDesc"){
if(NRFisConnected==1){
uint16_t buf = 51;
uint16_t kind = goonin.toInt();
if(kind>31){
Serial.println("<readButtonDesc>error");
return;
}
buf |= kind<<8;
if(sendRCdata(&buf,2)){
Serial.println("<readButtonDesc>send");
}else{
Serial.println("<readButtonDesc>lost");
}
}else{
Serial.println("<readButtonDesc>noconn");
}
return;
}
if(command=="getARC"){
if(NRFisConnected==1){
Serial.print("<getARC>");
Serial.println(radio.getARC());
}else{
Serial.println("<getARC>noconn");
}
return;
}
if(command=="loadCalibrationData"){
loadCalibrationData(goonin);
}
if(command=="loadLEDProgram"){
loadLEDProgram(goonin);
}
if(command=="putBLEProgram0"){
BLEProgramCache[0] = goonin;
preferences.putString("BLEPchache_0",goonin);
}
if(command=="putBLEProgram1"){
BLEProgramCache[1] = goonin;
preferences.putString("BLEPchache_1",goonin);
}
if(command=="putBLEProgram2"){
BLEProgramCache[2] = goonin;
preferences.putString("BLEPchache_2",goonin);
}
if(command=="putBLEProgram3"){
BLEProgramCache[3] = goonin;
preferences.putString("BLEPchache_3",goonin);
}
if(command=="putBLEProgram4"){
BLEProgramCache[4] = goonin;
preferences.putString("BLEPchache_4",goonin);
}
if(command=="BLEnextProgram"){
BLEnextProgram();
}
if(command=="BLEgetProgramId"){
Serial.print("<BLEgetProgramId>");
Serial.println(BLEProgramActive);
}
if(command=="BLEsetProgramId"){
Serial.print("<BLEgetProgramId>");
BLEProgramActive = goonin.toInt()-1;
BLEnextProgram();
}
if(command=="reset"){
preferences.clear();
ESP.restart();
}
}

712
input.h Normal file
View file

@ -0,0 +1,712 @@
void loadProgram(String program);
#include "nrf.h"
#include "ble.h"
#include <btnMatrix.h>
#define aBtnBorder 22936
#define aBtnIncBorder 500
btnMatrix btns(25,26,27,14 ,34,35,32,33);
//animation Store
struct animation{
unsigned int totaltime;
String data;
};
animation A_store[NUM_BTNS * 4];
struct buttonlink{
byte clock; //0 system, 1 own, 2 own + reset
unsigned int offset;
byte id;
};
buttonlink B_store [NUM_BTNS][4]; //notused,released,pressed,changing
//running
byte A_programId[NUM_BTNS];
byte A_state[NUM_BTNS]; //0-3 2-Dim in B_store
//round
String A_program[NUM_BTNS];
unsigned long A_clock[NUM_BTNS]; //begin of the time
byte A_clocktype[NUM_BTNS]; //wich time should be used
unsigned int A_offset[NUM_BTNS]; //time offset
unsigned int A_totalroundtime[NUM_BTNS]; //total time
unsigned int A_roundtime[NUM_BTNS]; //time passed
//step
boolean A_slow[NUM_BTNS];
unsigned int A_steptime[NUM_BTNS];
CRGB A_color[NUM_BTNS];
boolean A_next_slow[NUM_BTNS];
unsigned int A_next_steptime[NUM_BTNS];
CRGB A_next_color[NUM_BTNS];
//read in
bool state[64];
int values[64];
//btns
bool lastState[NUM_BTNS]; //first interpreter
bool switchState[NUM_BTNS];
byte buttonaction[NUM_BTNS][7];
//analog
int16_t lastValue[NUM_ANALOG];
double analogCalibrate[NUM_ANALOG][3];
byte analogaction[NUM_ANALOG][5];
/*analog -> btns
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0: lx rx lt rt sy 1 5 9
16: ly ry lr rr sy 2 6 10
32: lt rt ll rl tl 3 7
48: lr rr lb rb tr 4 8
0-1 analog
2-3 HAT
4 2xsys 2xtrigger
5-7 10x btn
buttons: hat left -> hat right -> (l->r) trigger -> left to right bottom btn
*/
boolean readBTN(byte i){
//const byte pos[] = {2,18,34,50, 3,19,35,51, 36,52, 5,21,37,53,6,22,38,54,7,23};
const byte pos[] = {36, 2,18,34,50, 5,21,37,53,6,22,38,54,7,23, 51,19,35,3,/*3,19,35,51,*/ 52};
return state[pos[i]];
}
double readAnalog(byte i, bool raw = false){
const byte pos[] = {0,16,32,48, 1,17,33,49};
byte j = pos[i];
double value = values[j];
return raw?value:(value*value*analogCalibrate[i][0]+value*analogCalibrate[i][1]+analogCalibrate[i][2]);
}
void pressBTN(byte amode, byte value){
if(amode==0)return;
if(mode==mode_radio&&value<32){
NRFpress(value);
}
if(mode==mode_bluetooth){
BLEpress(amode,value);
}
}
void releaseBTN(byte amode, byte value){
if(amode==0)return;
if(mode==mode_radio&&value<32){
NRFrelese(value);
}
if(mode==mode_bluetooth){
BLErelese(amode,value);
}
}
void increseAxis(byte analog_num, int16_t value){
if(mode==mode_radio){
NRFincreseAxis(analog_num,value/**currentTime/1000*/);
}
if(mode==mode_bluetooth){
BLEincreseAxis(analog_num,value/**currentTime/1000*/);
}
}
void setAxis(byte analog_num, int16_t value){
if(mode==mode_radio){
NRFsetAxis(analog_num,value);
}
if(mode==mode_bluetooth){
BLEsetAxis(analog_num,value);
}
}
void datasend(){
if(mode==mode_radio){
NRFsend();
//BLEnotsend();
}
if(mode==mode_bluetooth){
BLEsend();
NRFnotsend();
}
}
void innerModeClick(){
if(mode==mode_radio){
NRFclick();
}
if(mode==mode_bluetooth){
BLEclick();
}
}
void load_next_step(byte i){
unsigned int cutpos = 0;
cutpos = cutcheck(A_program[i].indexOf(","),A_program[i].length());
String now = A_program[i].substring(0,cutpos);
A_program[i] = A_program[i].substring(cutpos+1);
//add roundtime
A_roundtime[i] += A_steptime[i];
//move old
A_slow[i] = A_next_slow[i];
A_steptime[i] = A_next_steptime[i];
A_color[i] = A_next_color[i];
//load new
//slow?
cutpos = cutcheck(now.indexOf(" "),now.length());
String nowin = now.substring(0,cutpos);
now = now.substring(cutpos+1);
A_next_slow[i]=false;
if(nowin.startsWith("1")){
A_next_slow[i]=true;
}
//time
cutpos = cutcheck(now.indexOf(" "),now.length());
nowin = now.substring(0,cutpos);
now = now.substring(cutpos+1);
A_next_steptime[i] = nowin.toInt();
//color
cutpos = cutcheck(now.indexOf(" "),now.length());
nowin = now.substring(0,cutpos);
now = now.substring(cutpos+1);
A_next_color[i] = hextocolor(nowin);
if(A_program[i].length()<2){
A_program[i] = A_store[A_programId[i]].data;
}
}
void loadC(byte state,byte i){
A_state[i] = state;
buttonlink B_now = B_store[i][state];
A_programId[i] = B_now.id > 255?0:B_now.id;
animation A_now = A_store[A_programId[i]];
A_totalroundtime[i] = A_now.totaltime;
A_offset[i] = B_now.offset;
A_clocktype[i] = B_now.clock;
if(B_now.clock>0){
A_clock[i] = millis();
}
A_program[i] = A_now.data;
load_next_step(i);
load_next_step(i);
leds[i] = A_color[i];
A_roundtime[i] = 0;
}
float lastBright = getBrightness();
void animateBTNs(){
for (int i = 0; i < NUM_BTNS; i++) {
unsigned long time = millis()-(A_clocktype[i]==0?0:A_clock[i])+A_offset[i];
unsigned int roundtime = time%A_totalroundtime[i];
if(roundtime<A_roundtime[i]){//overrun
load_next_step(i);
leds[i] = A_color[i];
A_roundtime[i]=0;
}
if(roundtime>=A_roundtime[i]+A_steptime[i]){
load_next_step(i);
leds[i] = A_color[i];
}
if(A_roundtime[i]>=A_totalroundtime[i])A_roundtime[i]=0;
if(A_slow[i]){
float per = 100.0*(roundtime-A_roundtime[i])/((float) A_steptime[i]);
leds[i] = CRGB(
round((float)(A_color[i][0] * (100.0-per)/100.0) + (A_next_color[i][0] * per/100.0)),
round((float)(A_color[i][1] * (100.0-per)/100.0) + (A_next_color[i][1] * per/100.0)),
round((float)(A_color[i][2] * (100.0-per)/100.0) + (A_next_color[i][2] * per/100.0))
);
}
}
lastBright = lastBright * 0.999 + getBrightness() * 0.001;
FastLED.setBrightness(lastBright);
FastLED.show();
}
void handelInput(){
unsigned long nowTime = millis();
currentTime = nowTime-lastTime;
lastTime = nowTime;
btns.readAll(state,values,8);
btns.analogGlide(values,0,0.3);
btns.analogGlide(values,1,0.3);
btns.analogGlide(values,0,0.1);
btns.analogGlide(values,1,0.1);
for(int i = 0; i < NUM_ANALOG; i++){
if(analogaction[i][0]>0){
double analoginR = readAnalog(i);
int16_t analogin;
if(analoginR>32767){
analogin = 32767;
}else if(analoginR<-32767){
analogin = -32767;
}else{
analogin = (int16_t)analoginR;
}
if(analogaction[i][0]==1){
//press up
if(analogin>=aBtnBorder&&lastValue[i]<aBtnBorder)pressBTN(analogaction[i][1],analogaction[i][2]);
//press down
if(analogin<=-aBtnBorder&&lastValue[i]>-aBtnBorder)pressBTN(analogaction[i][3],analogaction[i][4]);
//release up
if(analogin<aBtnBorder&&lastValue[i]>=aBtnBorder)releaseBTN(analogaction[i][1],analogaction[i][2]);
//release down
if(analogin>-aBtnBorder&&lastValue[i]<=-aBtnBorder)releaseBTN(analogaction[i][3],analogaction[i][4]);
}
if(analogaction[i][0]==2){
setAxis(analogaction[i][1],analogin);
}
if(analogaction[i][0]==3){
if(abs(analogin)>aBtnIncBorder)
increseAxis(analogaction[i][1],round((double)analogin * ((double) ((int16_t)((analogaction[i][2]<<8)+analogaction[i][3])) )/32767.0) );
}
lastValue[i] = analogin;
}
}
for (int i = 0; i < NUM_BTNS; i++) {
boolean btnstate = readBTN(i);
if(buttonaction[i][0]==1){
if(btnstate&&!lastState[i]){
loadC(2,i);
pressBTN(buttonaction[i][1],buttonaction[i][2]);
pressBTN(buttonaction[i][3],buttonaction[i][4]);
pressBTN(buttonaction[i][5],buttonaction[i][6]);
}
if(!btnstate&&lastState[i]){
loadC(1,i);
releaseBTN(buttonaction[i][1],buttonaction[i][2]);
releaseBTN(buttonaction[i][3],buttonaction[i][4]);
releaseBTN(buttonaction[i][5],buttonaction[i][6]);
}
}
if(buttonaction[i][0]==2){
if(btnstate&&!lastState[i]){
loadC(3,i);
pressBTN(buttonaction[i][1],buttonaction[i][2]);
pressBTN(buttonaction[i][3],buttonaction[i][4]);
pressBTN(buttonaction[i][5],buttonaction[i][6]);
}
if(!btnstate&&lastState[i]){
switchState[i]=!switchState[i];
if(switchState[i]){
loadC(2,i);
}else{
loadC(1,i);
}
releaseBTN(buttonaction[i][1],buttonaction[i][2]);
releaseBTN(buttonaction[i][3],buttonaction[i][4]);
releaseBTN(buttonaction[i][5],buttonaction[i][6]);
}
}
if(buttonaction[i][0]==3){
if(btnstate&&!lastState[i]){
loadC(3,i);
}
if(!btnstate&&lastState[i]){
switchState[i]=!switchState[i];
if(switchState[i]){
loadC(2,i);
pressBTN(buttonaction[i][1],buttonaction[i][2]);
pressBTN(buttonaction[i][3],buttonaction[i][4]);
pressBTN(buttonaction[i][5],buttonaction[i][6]);
}else{
loadC(1,i);
releaseBTN(buttonaction[i][1],buttonaction[i][2]);
releaseBTN(buttonaction[i][3],buttonaction[i][4]);
releaseBTN(buttonaction[i][5],buttonaction[i][6]);
}
}
}
if(buttonaction[i][0]==4){
if(btnstate&&!lastState[i]){
loadC(2,i);
}
if(!btnstate&&lastState[i]){
loadC(1,i);
}
if(btnstate){
setAxis(buttonaction[i][1],(buttonaction[i][2]<<8)+buttonaction[i][3]);
}
}
if(buttonaction[i][0]==5){
if(btnstate&&!lastState[i]){
loadC(2,i);
}
if(!btnstate&&lastState[i]){
loadC(1,i);
}
if(btnstate){
increseAxis(buttonaction[i][1],(buttonaction[i][2]<<8)+buttonaction[i][3]);
}
}
lastState[i] = btnstate;
}
animateBTNs();
datasend();
}
void LEDsetShutdown(){
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(100,0,0);
}
FastLED.show();
}
void loadProgram(String program){
unsigned int cutpos = 0;
cutpos = cutcheck(program.indexOf(";"),program.length());
String btns = program.substring(0,cutpos);
String analog = program.substring(cutpos+1);
int i = 0;
for(i = 0; i < NUM_BTNS; i++){
lastState[i] = false;
switchState[i] = false;
buttonaction[i][0] = 0;
buttonaction[i][1] = 0;
buttonaction[i][2] = 0;
buttonaction[i][3] = 0;
buttonaction[i][4] = 0;
buttonaction[i][5] = 0;
buttonaction[i][6] = 0;
}
i=0;
while(btns.length()>1){
if(i>=NUM_BTNS)break;
cutpos = cutcheck(btns.indexOf(","),btns.length());
String now = btns.substring(0,cutpos);
btns = btns.substring(cutpos+1);
/*Serial.print("signle btn: ");
Serial.println(now);*/
int j = 0;
if(now.startsWith("00")){
loadC(0,i);
}else{
loadC(1,i);
}
while(now.length()>j*2+1&&j<7){
String now2 = now.substring(j*2,j*2+2);
/*Serial.print("read [");
Serial.print(i);
Serial.print("] [");
Serial.print(j);
Serial.print("] : ");*/
buttonaction[i][j] = hextonum(now2);
/*Serial.print(buttonaction[i][j]);
Serial.print(" (");
Serial.print(now2);
Serial.println(")");*/
j++;
}
i++;
}
//analog
for(i = 0; i < NUM_ANALOG; i++){
lastValue[i] = 0;
analogaction[i][0] = 0;
analogaction[i][1] = 0;
analogaction[i][2] = 0;
analogaction[i][3] = 0;
analogaction[i][4] = 0;
}
i = 0;
while(analog.length()>1){
if(i>=NUM_ANALOG)break;
cutpos = cutcheck(analog.indexOf(","),analog.length());
String now = analog.substring(0,cutpos);
analog = analog.substring(cutpos+1);
int j = 0;
while(now.length()>j*2+1&&j<5){
String now2 = now.substring(j*2,j*2+2);
analogaction[i][j] = hextonum(now2);
j++;
}
i++;
}
}
void loadLEDProgram(String program){
preferences.putString("LEDProgram",program);
unsigned int cutpos = 0;
cutpos = cutcheck(program.indexOf("|"),program.length());
String prog = program.substring(0,cutpos);
String links = program.substring(cutpos+1);
byte i = 0;
while(prog.length()>2&&i<NUM_BTNS*4){
cutpos = cutcheck(prog.indexOf(";"),prog.length());
String now = prog.substring(0,cutpos);
prog = prog.substring(cutpos+1);
cutpos = cutcheck(now.indexOf(":"),now.length());
A_store[i].totaltime = max((long int)1, ((String) now.substring(0,cutpos)).toInt());
A_store[i].data = now.substring(cutpos+1);
i++;
}
i=0;
while(links.length()>2&&i<NUM_BTNS){
cutpos = cutcheck(links.indexOf(";"),links.length());
String btn = links.substring(0,cutpos);
links = links.substring(cutpos+1);
byte j = 0;
while(btn.length()>2&&j<4){
cutpos = cutcheck(btn.indexOf(","),btn.length());
String now = btn.substring(0,cutpos);
btn = btn.substring(cutpos+1);
cutpos = cutcheck(now.indexOf(" "),now.length());
String nowin = now.substring(0,cutpos);
now = now.substring(cutpos+1);
B_store[i][j].clock = nowin=="0"?(0):(nowin=="2"?(2):(1));
cutpos = cutcheck(now.indexOf(" "),now.length());
nowin = now.substring(0,cutpos);
now = now.substring(cutpos+1);
B_store[i][j].offset = nowin.toInt();
cutpos = cutcheck(now.indexOf(" "),now.length());
nowin = now.substring(0,cutpos);
now = now.substring(cutpos+1);
B_store[i][j].id = (byte) nowin.toInt();
j++;
}
i++;
}
}
void loadCalibrationData(String data){
unsigned int cutpos = 0;
int i = 0;
while(data.length()>1){
if(i>=NUM_ANALOG)break;
cutpos = cutcheck(data.indexOf(";"),data.length());
String now = data.substring(0,cutpos);
data = data.substring(cutpos+1);
for (int j = 0; j < 3; j++) {
if(now.length()>0){
cutpos = cutcheck(now.indexOf(","),now.length());
String now2 = now.substring(0,cutpos);
now = now.substring(cutpos+1);
analogCalibrate[i][j] = now2.toFloat();
}
}
i++;
}
preferences.putBytes("analogCalib",analogCalibrate, sizeof(analogCalibrate));
}
void iniInput(){
if(firstLaunch){
//loadProgram("010500,02020b,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00;00,00,00,00,00,00,00,00");
//loadProgram("010304,010400,010401,010403,010402,01040a,010408,01020e,01020d,02020b,02020a,010209,010208,010409,01040b,0103000,010301,010302,010303,010305;0208,0209,00,00,020b,020a,00,00");
loadCalibrationData(
(String)"0.00016901909683607273,23.823860249481648,-41965.23905167896;"+
"0.0032568361858686832,14.552576203914427,-35492.98520912674;"+
"-0.0000019086881625810545,16.011234881444572,-32767;"+
"-0.0000019086881625810545,16.011234881444572,-32767;"+
"-0.00019162251342654416,20.605461485605023,-35751.76305206794;"+
"-0.0008308262876500306,22.692086412128443,-35479.08647091325;"+
"-0.0000019086881625810545,16.011234881444572,-32767;"+
"-0.0000019086881625810545,16.011234881444572,-32767;"
);
}else{
preferences.getBytes("analogCalib",analogCalibrate, sizeof(analogCalibrate));
}
loadLEDProgram(preferences.getString("LEDProgram","10000:1 5000 880000,1 5000 004444;2000:1 1000 ff0000,1 1000 dd1111;2000:1 1000 00ff00,1 1000 11dd11;2000:1 1000 eeee00,1 1000 bb2222;|0 0 0,0 0 1,0 0 2,0 0 3;0 500 0,0 0 1,0 0 2,0 0 3;0 1000 0,0 0 1,0 0 2,0 0 3;0 1500 0,0 0 1,0 0 2,0 0 3;0 2000 0,0 0 1,0 0 2,0 0 3;0 2500 0,0 0 1,0 0 2,0 0 3;0 3000 0,0 0 1,0 0 2,0 0 3;0 3500 0,0 0 1,0 0 2,0 0 3;0 4000 0,0 0 1,0 0 2,0 0 3;0 4500 0,0 0 1,0 0 2,0 0 3;0 5000 0,0 0 1,0 0 2,0 0 3;0 5500 0,0 0 1,0 0 2,0 0 3;0 6000 0,0 0 1,0 0 2,0 0 3;0 6500 0,0 0 1,0 0 2,0 0 3;0 7000 0,0 0 1,0 0 2,0 0 3;0 7500 0,0 0 1,0 0 2,0 0 3;0 8000 0,0 0 1,0 0 2,0 0 3;0 8500 0,0 0 1,0 0 2,0 0 3;0 9000 0,0 0 1,0 0 2,0 0 3;0 9500 0,0 0 1,0 0 2,0 0 3;"));
BLEProgramCache[0] = preferences.getString("BLEPchache_0","010304,010400,010401,010403,010402,01040a,010408,01020e,01020d,02020b,02020a,010209,010208,010409,01040b,0103000,010301,010302,010303,010305;0208,0209,00,00,020b,020a,00,00");
BLEProgramCache[1] = preferences.getString("BLEPchache_1","");
BLEProgramCache[2] = preferences.getString("BLEPchache_2","");
BLEProgramCache[3] = preferences.getString("BLEPchache_3","");
BLEProgramCache[4] = preferences.getString("BLEPchache_4","");
FastLED.addLeds<WS2812B, LED_DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(100);
}
/*
analogCalibrate Date:
wert_0_0,wert_0_1,wert_0_2;
*/
/*
empty Program
010500,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00;00,00,00,00,00,00,00,00
*/
/*
//all numbers are in hexadecimal
mode submode value
//for btns
[0] //not unsed
[1] //normal btns
--> see btns
--> see btns
--> see btns
[2] //seemingly switch
--> see btns
--> see btns
--> see btns
[3] //switch
--> see btns
--> see btns
--> see btns
[4] //setAnaloge
[0-11] //analoge Values
[-32767 - 32767]
[5] //incrementalchange
[0-11] //analoge Values
[-32767 - 32767]
//for analog
[0] //not unsed
[1] //normal btns //min 70%
--> see btns (up)
--> see btns (down)
[2] //setAnaloge
[0-11] //analoge Values
[3] //incrementalchange
[0-11] //analoge Values
[-32767 - 32767] //multiplayer of in (-1 to 1)
--> btns
[0] empty
//ble
[1] //scancode
[0-255]
[2] //MediaKey
[0-15]
[3] //Gamepadbtns
[0-13]
[4] //HAT
[0-3]
[5] //Mouse
[0-4]
//nrf
[ff] //bool chanels
[0-31]
--> analoge
//ble -> lx, ly, rx, ry, lt, rt, lr, rr, mx, my, sm, sh
//nrf -> 0-11 raw send
//saved as float rounded for send to int16_t
*/
/*
nrfpackage
[4] reserved
[4] 32x btns
[24] 12x 2byte analoge outputs
== 32
*/
/*
new animation idea
from
0x00: 1000:1 500 880000,1 500 004444; //totaltime:slow steptime color, slow steptime color, ...;
0x01: 1000:1 500 880000,1 500 004444;
....
0x50: 1000:1 500 880000,1 500 004444;
to max 0x50 (80) |
0x00 0 0 0,1 0 1,0 0 2,0 0 3; //clock offset id,clock offset id,clock offset id,clock offset id;
0x01 0 0 0,1 0 1,0 0 2,0 0 3;
0x14 0 0 0,1 0 1,0 0 2,0 0 3;
detail specs, look old structur v
*/
/*
animation: {
totaltime, [in ms]
clock, [0: systemclock, 1 (and else): Button clock, 2 Button clock with reset]
offset: posnumber,
data:[
slow, [1: slow, 0: hard]
time,
color
]
}
*/
/*
"10000:1 5000 880000,1 5000 004444;"+
"2000:1 1000 ff0000,1 1000 dd1111;"+
"2000:1 1000 00ff00,1 1000 11dd11;"+
"2000:1 1000 eeee00,1 1000 bb2222;"+
"|"+
"0 0 0,0 0 1,0 0 2,0 0 3;"+
"0 500 0,0 0 1,0 0 2,0 0 3;"+
"0 1000 0,0 0 1,0 0 2,0 0 3;"+
"0 1500 0,0 0 1,0 0 2,0 0 3;"+
"0 2000 0,0 0 1,0 0 2,0 0 3;"+
"0 2500 0,0 0 1,0 0 2,0 0 3;"+
"0 3000 0,0 0 1,0 0 2,0 0 3;"+
"0 3500 0,0 0 1,0 0 2,0 0 3;"+
"0 4000 0,0 0 1,0 0 2,0 0 3;"+
"0 4500 0,0 0 1,0 0 2,0 0 3;"+
"0 5000 0,0 0 1,0 0 2,0 0 3;"+
"0 5500 0,0 0 1,0 0 2,0 0 3;"+
"0 6000 0,0 0 1,0 0 2,0 0 3;"+
"0 6500 0,0 0 1,0 0 2,0 0 3;"+
"0 7000 0,0 0 1,0 0 2,0 0 3;"+
"0 7500 0,0 0 1,0 0 2,0 0 3;"+
"0 8000 0,0 0 1,0 0 2,0 0 3;"+
"0 8500 0,0 0 1,0 0 2,0 0 3;"+
"0 9000 0,0 0 1,0 0 2,0 0 3;"+
"0 9500 0,0 0 1,0 0 2,0 0 3;"
*/
/*
example:
1000:0:0:1 500 880000,1 500 004444;
10000:0:0:0 10000 ff0000;
10000:0:0:0 10000 00ff00;
10000:0:0:0 10000 eeee00|
"10000:0:0:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:500:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:1000:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:1500:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:2000:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:2500:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:3000:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:3500:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:4000:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:4500:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:5000:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:5500:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:6000:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:6500:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:7000:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:7500:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:8000:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:8500:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:9000:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"+
"10000:0:9500:1 5000 880000,1 5000 004444;10000:0:0:0 10000 ff0000;10000:0:0:0 10000 00ff00;10000:0:0:0 10000 eeee00|"
*/

21
mode.h Normal file
View file

@ -0,0 +1,21 @@
void changeMode(byte i){
Serial.print("Changing from: ");
Serial.print(mode);
Serial.print(" to: ");
Serial.println(i);
if(i==0){
}else if(i==1){
iniBLE(false);
}else{
leds[21] = DebugColor;
}
mode = i;
}
void nextMode(){
byte newMode = mode+1;
if(newMode>2) newMode = 0;
changeMode(newMode);
}

123
nrf.h Normal file
View file

@ -0,0 +1,123 @@
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
//pevieas 1.3.2
#define NUM_ANALOG_channels 12
#define NUM_BTN_channels 32
byte NRFBTNcache[NUM_BTN_channels];
int16_t NRFAnalogSet[NUM_ANALOG_channels];
int32_t NRFAnalogincrement[NUM_ANALOG_channels];
#define multicastChannel 124
#define multicastAddress 0xF0F0F0F0FF
byte NRFisConnected = 0;
uint64_t myPipe = 0 ;
byte myChannel = 0;
unsigned long lastMulticast = 0;
//3 (CE) 4
//4 (CSN) 5
//5 (SCK) 18
//6 (MOSI) 23
//7 (MISO) 19
// CE CSN
RF24 radio(4,5);
#include "nrfCom.h"
void iniNRF(){
radio.begin();
radio.enableDynamicPayloads();
radio.setRetries(5,15);
radio.setAutoAck(false);
radio.setChannel(multicastChannel);
radio.openReadingPipe(0,multicastAddress);
radio.startListening();
for (byte i = 0; i < NUM_BTN_channels; i++) {
NRFBTNcache[i] = 0;
}
for (byte i = 0; i < NUM_ANALOG_channels; i++) {
NRFAnalogSet[i] = 0;
NRFAnalogincrement[i] = 0;
}
}
void NRFpress(byte btn){
if(NRFBTNcache[btn]<255)NRFBTNcache[btn]++;
}
void NRFrelese(byte btn){
if(NRFBTNcache[btn]>0)NRFBTNcache[btn]--;
}
void NRFsetAxis(byte axe, int16_t value){
NRFAnalogSet[axe] = value;
}
void NRFincreseAxis(byte axe, int16_t value){
int32_t now = NRFAnalogincrement[axe];
now+=(int32_t) value;
if(now>65536)now = 65536;
if(now<-65536)now = -65536;
NRFAnalogincrement[axe] = (int32_t) now;
}
void NRFsend(){
NRFreceive();
ndata.btns = 0;
for (byte i = 0; i < NUM_BTN_channels; i++) {
if(NRFBTNcache[i]>0){
ndata.btns |= 1<<i;
}
}
for (byte i = 0; i < NUM_ANALOG_channels; i++) {
int32_t now = (int32_t)NRFAnalogSet[i];
now+=(int32_t) NRFAnalogincrement[i];
if(now>32767)now = 32767;
if(now<-32767)now = -32767;
ndata.analog[i] = (int16_t) now;
NRFAnalogSet[i] = 0;
NRFAnalogincrement[i] = 0;
}
leds[21] = NRFisConnected == 1 ? NRFColorConnected : NRFColorNotConnected;
if(NRFisConnected==1)sendRCdata(&ndata,sizeof(ndata));
}
byte notsendcount = 0;
const uint16_t emptyBuffer = 0xffff;
void NRFnotsend(){
NRFreceive();
if(NRFisConnected==1&&((notsendcount++)%40)==0)sendRCdata(&emptyBuffer, 2, true);
}
void NRFclick(){
if(NRFisConnected != 1){
if(myPipe != 0&&millis()-lastMulticast < 2000){
radio.stopListening();
radio.setAutoAck(true);
radio.setChannel(myChannel);
radio.openWritingPipe(myPipe);
radio.openReadingPipe(0,myPipe);
radio.startListening();
NRFisConnected = 1;
}
}else{
radio.stopListening();
radio.setAutoAck(false);
radio.setChannel(multicastChannel);
radio.openReadingPipe(0,multicastAddress);
radio.startListening();
NRFisConnected = 2;
}
}
void NRFshutdown(){
}

104
nrfCom.h Normal file
View file

@ -0,0 +1,104 @@
void loadMulticast();
typedef struct{
byte type;
byte command;
uint32_t btns;
int16_t analog[NUM_ANALOG_channels];
}nrfdata;
typedef struct{
byte type; //jType_ANALOG_Desc || jType_BUTTON_Desc
byte command; //0-31 button channel || 0-11
byte use = 0xff; //0-3 || 0-1
char text[29] = "error"; //Text
}Channel_Desc_Send;
nrfdata ndata;
byte jRCbuff[32];
String SerialOutBuffer = "";
const String b16 = "0123456789abcdef";
void NRFreceive(){
if(radio.available()){
uint8_t payloadSize = radio.getDynamicPayloadSize();
if(payloadSize>=1){
radio.read( &jRCbuff, payloadSize );
switch (jRCbuff[0]) {
case 0xff:{
loadMulticast();
}break;
case 1:
case 2:
case 3: {
SerialOutBuffer = "<sensor>";
for (size_t i = 0; i < payloadSize; i++) {
SerialOutBuffer+=b16[(jRCbuff[i]>>4)&0xf];
SerialOutBuffer+=b16[jRCbuff[i]&0xf];
}
Serial.println(SerialOutBuffer);
}break;
case 50:{ //jType_ANALOG_Desc
SerialOutBuffer = "<analogDesc>";
Channel_Desc_Send data;
memmove(&data, &jRCbuff, sizeof(data));
SerialOutBuffer+=String(data.command);
SerialOutBuffer+="|";
SerialOutBuffer+=String(data.use);
SerialOutBuffer+="|";
SerialOutBuffer+=String(data.text);
Serial.println(SerialOutBuffer);
}break;
case 51:{ //jType_BUTTON_Desc
SerialOutBuffer = "<buttonDesc>";
Channel_Desc_Send data;
memmove(&data, &jRCbuff, sizeof(data));
SerialOutBuffer+=String(data.command);
SerialOutBuffer+="|";
SerialOutBuffer+=String(data.use);
SerialOutBuffer+="|";
SerialOutBuffer+=String(data.text);
Serial.println(SerialOutBuffer);
}break;
}
}
}
}
typedef struct{
byte type = 0xff;
uint64_t pipe;
byte channel;
}nrfMulticast;
void loadMulticast(){
if(NRFisConnected==1)return;
nrfMulticast data;
memmove(&data, &jRCbuff, sizeof(data));
myPipe = data.pipe;
myChannel = data.channel;
lastMulticast = millis();
Serial.print("<multicast>");
Serial.print(myChannel);
Serial.print("|");
for (int i = 9; i >=0; i--) {
byte id = (myPipe>>(i*4))&0xf;
if(id>15){
Serial.print("-");
}else Serial.print(b16[id]);
}
Serial.println();
}
bool sendRCdata(const void* buf, uint8_t length, bool multicast = false){
radio.stopListening();
bool succ = radio.write( buf, length, multicast );
radio.startListening();
return succ;
}

103
static.h Normal file
View file

@ -0,0 +1,103 @@
#define relay_1 12
#define relay_2 13
#define LIGHT_SENSOR_PIN 36
//System btns color
#define BLEColorNotConnected CRGB(0,0,255)
#define BLEColorConnected CRGB(50,50,255)
#define NRFColorNotConnected CRGB(0,255,0)
#define NRFColorConnected CRGB(50,255,50)
#define DebugColor CRGB(255,255,0)
//input numbers
#define NUM_BTNS 20
#define LED_DATA_PIN 15
#define NUM_ANALOG 8
#define NUM_LEDS NUM_BTNS+2
//button timing
#define short_click 500
#define long_click 1000
#define double_click_brake 250
//mode
#define mode_radio 0
#define mode_bluetooth 1
#define mode_debug 2
//rpi
#define rpi_off 0
#define rpi_starting 1
#define rpi_on 2
#define rpi_stoping 3
#define rpi_unknown 4 //shut be on
#define rpi_max_boot_time 30000
#define rpi_stop_time 30000
#define rpi_max_black_time 10000
//Seriel commands
#define Serial_end 0xff
//rpi --> esp32 (back is awnser)
#define cmd_check 0b00000001
#define cmd_setMode 0b00000010
// > uint8_t:mode
// < bool:ok
#define cmd_getStatus 0b00000011
// < uint8_t:mode, bool:connected
//esp32 --> rpi (back is awnser)
#define cmd_empty 0b00000000
//radio
//types
//hid (input)
/*
[byte] mode|submode, [byte] value
//color if btn(default)
mode: #00, value:? unsigned 1
mode: #1t, value: x normal btn 2 (off/on)
mode: #2t, value: x seemingly switch 3 (off/down/on)
mode: #3t, value: x switch 3 (off/down/on)
//t
0: x = 0-255 scancode
1: x = 1-16 MediaKey
2: x = 1-13 Gamepadbtns
3: x = 0-3 HAT
4: x = 0-4 Mouse
mode: #4j, value: value to set or analoge input setAnaloge 2 (off/on)
mode: #5j, value: amount or multiplay of joystick percentage incrementalchange (down) 2 (off/on)
mode: #6j, value: amount or multiplay of joystick percentage incrementalchange (up) 2 (off/on)
//j ble
0-7 analog inputs (lx, ly, rx, ry, lt, rt, lr, rr)
8-9 mouse directions (x,y)
a-b mouse wheel (v,h)
//j nrf
0-f analoge value
function: [{ mode|submode, value }, ...] for every
// 8x analoge (ilx, ily, irx, iry, ilt, irt, ilr, irr)
8x hat (lt, lr, ld, ll, rt, rr, rb, rl)
10x btns (from left to right)
theme: [{color1 (off), color2 (on), color2 (down)}, ...]
// 8x hat (lt, lr, ld, ll, rt, rr, rb, rl)
10x btns (from left to right)
*/

19
tools.h Normal file
View file

@ -0,0 +1,19 @@
#include <FastLED.h>
//const String B64 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
const String B16 = "0123456789abcdef";
byte getBrightness(){
return map(analogRead(LIGHT_SENSOR_PIN),0,4095,15,255);
}
unsigned int cutcheck(unsigned int a, unsigned int b){
if(a==-1) return b;
if(b==-1) return a;
return min(a,b);
}
byte hextonum(String hex){
return (B16.indexOf(hex.substring(0,1))*16)+B16.indexOf(hex.substring(1,2));
}
CRGB hextocolor(String hex){
return CRGB(hextonum(hex.substring(0,2)),hextonum(hex.substring(2,4)),hextonum(hex.substring(4,6)));
}