Commit 92e0d801 authored by Kittipong's avatar Kittipong
Browse files

Add self resetting function and optimize the code.

parent fd749687
......@@ -194,7 +194,7 @@ const uint16_t NEUTRAL = 1500;
const uint16_t P_MIN = 1000;
const uint16_t P_MAX = 2000;
const uint8_t LIMIT = 10;
const uint8_t LIMIT = 50;
const uint64_t DEFAULT_ADDR PROGMEM = 0xE1E1E1E1E1;
#endif
\ No newline at end of file
......@@ -48,6 +48,10 @@ public:
void controlMain(void);
void controlSetting(void);
uint16_t SET_P = 0;
uint16_t SET_I = 0;
uint16_t SET_D = 0;
private:
uint16_t filter( uint16_t newSample, uint16_t oldSample, float alpha);
};
......
......@@ -6,15 +6,16 @@ const char menu_batt1[] = "Lithium";
const char menu_batt2[] = "Lipo";
const char feedback_rx_confirmed[] = "Rx Confirmed";
const char feedback_rx_synced[] = "Rx Synced";
const char feedback_not_saved[] = "Not saved";
const char feedback_msg_1[] = "Connecting...";
const char feedback_msg_2[] = "No Connection...";
const char feedback_msg_4[] = ">> Switch Profile";
const char feedback_msg_8_1[] = "Connecting...";
const char feedback_msg_8_2[] = "No Connection...";
const char feedback_msg_10[] = "Resetting ADDR";
const char feedback_msg_12[] = "Saving Settings";
const char feedback_msg_103[] = ">> Connected";
const char feedback_msg_109[] = ">> Changed ADDR";
const char feedback_msg_111[] = ">> Reset ADDR";
const char feedback_msg_113[] = ">> Saving";
const char feedback_msg_113_1[] = "Rx synced";
const char feedback_msg_120[] = ">> Exiting";
const char feedback_msg_120_1[] = "Not saved";
\ No newline at end of file
const char feedback_msg_connected[] = ">> Connected";
const char feedback_msg_change_addr[] = ">> Changed ADDR";
const char feedback_msg_reset_addr[] = ">> Reset ADDR";
const char feedback_msg_save[] = ">> Saving";
const char feedback_msg_exit[] = ">> Exiting";
\ No newline at end of file
......@@ -37,6 +37,7 @@ private:
void drawString(String text, uint8_t x, uint8_t y);
void drawGauge(uint8_t x, uint8_t y, uint8_t r, int16_t input, int16_t inLow, int16_t inHigh, uint16_t strD, uint16_t endD, bool reverse=false);
uint16_t mapDegree(int16_t input, int16_t inLow, int16_t inHigh, uint16_t strD, uint16_t endD, bool reverse=false);
uint16_t trigonometryDegree(int16_t degree);
};
#endif
\ No newline at end of file
#ifndef _TRANSMIT_H
#define _TRANSMIT_H
#include <RF24.h>
#include <nRF24L01.h>
#include "Config.h"
......@@ -18,6 +19,7 @@ public:
TRANSMIT(void);
void start(CONTROLLER * CP);
bool checkNRF24(void);
void send(void);
String uint64ToAddress(uint64_t number);
bool checkAddress(uint64_t addr);
......
void(* resetFunc) (void) = 0;
#include "Controller.h"
CONTROLLER controller;
......@@ -11,18 +13,22 @@ void setup(){
}
}
void loop(){
controller.readBattery();
controller.readHallSensor();
if(!controller.NRF.checkNRF24()){
delay(300);
resetFunc();
}else{
controller.readBattery();
controller.readHallSensor();
switch (controller.NRF.Px.type){
case 1:
controller.controlMain();
break;
case 2:
controller.controlSetting();
break;
switch (controller.NRF.Px.type){
case 1:
controller.controlMain();
break;
case 2:
controller.controlSetting();
break;
}
}
controller.NRF.send();
controller.OLED.update(50);
}
\ No newline at end of file
......@@ -39,7 +39,6 @@ void SCREEN::update(uint8_t refresh_ms){
}
}
}
switch(CP->NRF.sent){
case 4:
drawString(feedback_msg_4, 0, 12);
......@@ -47,9 +46,9 @@ void SCREEN::update(uint8_t refresh_ms){
break;
case 8:
if(CP->NRF.status){
drawString(feedback_msg_8_1, 0, 20);
drawString(feedback_msg_1, 0, 20);
}else{
drawString(feedback_msg_8_2, 0, 20);
drawString(feedback_msg_2, 0, 20);
}
break;
case 10:
......@@ -59,26 +58,29 @@ void SCREEN::update(uint8_t refresh_ms){
drawString(feedback_msg_12, 0, 20);
break;
case 103:
drawString(feedback_msg_103, 0, 20);
drawString(feedback_msg_connected, 0, 20);
break;
case 105:
drawString(feedback_msg_connected, 0, 20);
break;
case 109:
drawString(feedback_msg_109, 0, 12);
drawString(feedback_msg_change_addr, 0, 12);
drawString(feedback_rx_confirmed, 18, 30);
break;
case 111:
drawString(feedback_msg_111, 0, 12);
drawString(feedback_msg_reset_addr, 0, 12);
drawString(feedback_rx_confirmed, 18, 30);
break;
case 113:
drawString(feedback_msg_113, 0, 12);
drawString(feedback_msg_113_1, 18, 30);
drawString(feedback_msg_save, 0, 12);
drawString(feedback_rx_synced, 18, 30);
break;
case 120:
drawString(feedback_msg_120, 0, 12);
drawString(feedback_msg_120_1, 18, 30);
drawString(feedback_msg_exit, 0, 12);
drawString(feedback_not_saved, 18, 30);
break;
default:
drawString("Status Feedback : "+(CP->NRF.sent), 0, 20);
drawString("Status Feedback : "+String(CP->NRF.sent), 0, 20);
}
}else{
showSetting();
......@@ -341,37 +343,20 @@ void SCREEN::drawConnection(uint8_t x, uint8_t y){
}
Time.count++;
}else{
if(CP->NRF.ping < LIMIT/3){
U8G2->drawXBMP(x, y, 10, 10, connectedIcon);
}else if(CP->NRF.ping < LIMIT/2){
U8G2->drawXBMP(x, y, 10, 10, ConnectHigh);
}else if(CP->NRF.ping < LIMIT){
U8G2->drawXBMP(x, y, 10, 10, ConnectLow);
}else{
U8G2->drawXBMP(x, y, 10, 10, NotConnect);
}
U8G2->drawXBMP(x, y, 10, 10, connectedIcon);
}
}
void SCREEN::drawString(String text, uint8_t x, uint8_t y){
static char drawString_buffer[18];
static char drawString_buffer[32];
text.toCharArray(drawString_buffer, text.length()+1);
U8G2->drawStr(x, y, drawString_buffer);
}
void SCREEN::drawGauge(uint8_t x, uint8_t y, uint8_t r, int16_t input, int16_t inLow, int16_t inHigh, uint16_t strD, uint16_t endD, bool reverse){
static uint8_t multiply;
if(mapDegree(input, inLow, inHigh, strD, endD, reverse)>0){
if(dg.result > 90 && dg.result > (90 * multiply)){
multiply++;
}
if(dg.result > 90){
dg.result = (180+(90* multiply)) - (dg.result - (270-(90 * multiply)));
}else{
dg.result = (90 - dg.result);
multiply = 0;
}
if(mapDegree(input, inLow, inHigh, strD, endD, reverse)){
dg.result = trigonometryDegree(dg.result);
for(uint8_t i=0; i<6; i++){
if(i % 2 == 1){
float rad = ((dg.result+i)*PI)/180;
......@@ -383,8 +368,8 @@ void SCREEN::drawGauge(uint8_t x, uint8_t y, uint8_t r, int16_t input, int16_t i
uint16_t SCREEN::mapDegree(int16_t input, int16_t inLow, int16_t inHigh, uint16_t strD, uint16_t endD, bool reverse){
if(strD > 360 or endD > 360 or strD <= 0 or endD <= 0){
return 0;
if(strD > 360 or endD > 360 or strD < 1 or endD < 1){
return false;
}else{
if(reverse){
if(strD > endD){
......@@ -428,4 +413,19 @@ uint16_t SCREEN::mapDegree(int16_t input, int16_t inLow, int16_t inHigh, uint16_
}
return dg.result;
}
uint16_t SCREEN::trigonometryDegree(int16_t degree){
static uint8_t multiply;
if(degree > 90 && degree > (90 * multiply)){
multiply++;
}
if(degree > 90){
degree = (180+(90* multiply)) - (degree - (270-(90 * multiply)));
}else{
degree = (90 - degree);
multiply = 0;
}
return degree;
}
\ No newline at end of file
......@@ -52,6 +52,10 @@ uint16_t SETTING::getValue(uint8_t index){
case 11: return e.HallMax; break;
case 13: return Optional; break;
case 14: return Optional; break;
case 15: return CP->SET_P; break;
case 16: return CP->SET_I; break;
case 17: return CP->SET_D; break;
}
return 0;
}
......@@ -75,6 +79,10 @@ void SETTING::setValue(uint8_t index, uint16_t value){
case 11: e.HallMax = value; break;
case 13: Optional = value; break;
case 14: Optional = value; break;
case 15: CP->SET_P = value; break;
case 16: CP->SET_I = value; break;
case 17: CP->SET_D = value; break;
}
}
......
......@@ -5,6 +5,10 @@ TRANSMIT::TRANSMIT(){
radio = new RF24(PIN_CE, PIN_CS);
}
bool TRANSMIT::checkNRF24(){
return radio->isChipConnected();
}
void TRANSMIT::start(CONTROLLER * CP){
this->CP = CP;
......@@ -33,9 +37,6 @@ void TRANSMIT::send(){
}
if((millis()-CP->Time.Lock) > 300){
if(!status){
CP->Time.Lock = millis();
}
switch(Px.type){
case 1:
if(radio->write(&Px, sizeof(Px))){
......@@ -50,19 +51,13 @@ void TRANSMIT::send(){
}
}
if(ping > 1){
ping--;
ping = 0;
if(!status){
status = true;
CP->Lock = true;
}
}
}else{
if(ping <= LIMIT){
ping++;
}
}
break;
case 2:
if(openpipe > 1){
openpipe--;
......@@ -94,6 +89,9 @@ void TRANSMIT::send(){
case 12: //Save and Exit.
CP->ROM.save();
break;
case 120: // Exit without Saving
reloadTx();
break;
}
if(Rx.BatteryType<1){
......@@ -103,15 +101,11 @@ void TRANSMIT::send(){
if(radio->write(&Tx, sizeof(Tx))){
Tx.type = 0;
if(ping > 1){
ping--;
ping = 0;
if(!status){
status = true;
}
}
}else{
if(ping <= LIMIT){
ping++;
}
}
if(radio->isAckPayloadAvailable()){
......@@ -145,14 +139,21 @@ void TRANSMIT::send(){
}
}
break;
}
}
if(!status){
CP->Time.Lock = millis();
}
}
if(ping >= LIMIT){
if(ping > LIMIT){
if(status){
radio->flush_tx();
status = false;
CP->Lock = true;
}
}else{
ping++;
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment