Browse Source

add first draft for gui states

master
Schoenberger, Philipp 6 years ago
parent
commit
ee10db05c0
  1. 9
      Multiprotocol/Multiprotocol.h
  2. 31
      Multiprotocol/Multiprotocol.ino
  3. 10
      Multiprotocol/debug.h
  4. 10
      Multiprotocol/inputs.ino
  5. 139
      Multiprotocol/state.cpp
  6. 62
      Multiprotocol/state.h

9
Multiprotocol/Multiprotocol.h

@ -354,14 +354,7 @@ enum MultiPacketTypes
#define FAILSAFE_CHANNEL_HOLD 2047 #define FAILSAFE_CHANNEL_HOLD 2047
#define FAILSAFE_CHANNEL_NOPULSES 0 #define FAILSAFE_CHANNEL_NOPULSES 0
//********************
//** Debug messages **
//********************
uint16_t debug_time=0;
char buf[128];
#define debug(msg, ...) { sprintf(buf, msg, ##__VA_ARGS__); Serial.println(buf);}
#define debugln(msg, ...) { sprintf(buf, msg "\r\n", ##__VA_ARGS__); Serial.println(buf);}
#define debug_time(msg) { uint16_t debug_time_TCNT1=TCNT1; debug_time=debug_time_TCNT1-debug_time; debugln(msg "%u", debug_time); debug_time=debug_time_TCNT1; }
#include "debug.h"
//******************** //********************
//*** Blink timing *** //*** Blink timing ***
//******************** //********************

31
Multiprotocol/Multiprotocol.ino

@ -40,6 +40,7 @@
#include "Pins.h" #include "Pins.h"
#include "TX_Def.h" #include "TX_Def.h"
#include "Validate.h" #include "Validate.h"
#include "state.h"
//Global constants/variables //Global constants/variables
uint32_t MProtocol_id;//tx id, uint32_t MProtocol_id;//tx id,
@ -307,29 +308,7 @@ void setup()
#endif //ENABLE_PPM #endif //ENABLE_PPM
debugln("Init complete"); debugln("Init complete");
update_inputs(); update_inputs();
debugln("time now %lu ", micros());
delay(1000);
debugln("time after 1 sec %lu", micros());
#ifdef TCCR1A
while((TIFR1 & OCF1A_bm) == 0); // wait
unsigned long s = micros();
cli(); // Disable global int due to RW of 16 bits registers
OCR1A += 10000 * TIMER_PRESCALE; // set compare A for callback
TIFR1=OCF1A_bm; // clear compare A=callback flag
sei(); // enable global int
while((TIFR1 & OCF1A_bm) == 0); // wait 2ms...
unsigned long e = micros();
double a = e;
a -= s;
a = 10000/a;
debugln("time now %lu", s);
debugln("time after 1 sec %lu", e);
debugln("scalor %d", a);
TIMER_PRESCALE *= a;
#endif
init_state();
} }
// Main // Main
@ -397,8 +376,10 @@ void loop()
sei(); // enable global int sei(); // enable global int
#else #else
if (next_callback > 4000)
if (next_callback > 4000) {
update_inputs(); update_inputs();
update_state();
}
uint32_t wait_until = start + next_callback; uint32_t wait_until = start + next_callback;
end__ = micros(); end__ = micros();
if (end__-start < next_callback) { if (end__-start < next_callback) {
@ -1205,5 +1186,3 @@ static uint32_t random_id(uint16_t address, uint8_t create_new)
tx_resume(); tx_resume();
} }
#endif //ENABLE_SERIAL #endif //ENABLE_SERIAL

10
Multiprotocol/debug.h

@ -0,0 +1,10 @@
#ifndef DEBUG_H
#define DEBUG_H
//********************
//** Debug messages **
//********************
#define debug(msg, ...) { char buf[128]; sprintf(buf, msg, ##__VA_ARGS__); Serial.println(buf);}
#define debugln(msg, ...) { char buf[128]; sprintf(buf, msg "\r\n", ##__VA_ARGS__); Serial.println(buf);}
#define debug_time(msg) { uint16_t debug_time_TCNT1=TCNT1; debug_time=debug_time_TCNT1-debug_time; debugln(msg "%u", debug_time); debug_time=debug_time_TCNT1; }
#endif

10
Multiprotocol/inputs.ino

@ -5,7 +5,6 @@ void update_inputs(void) {
if (state < FRSKY_BIND_DONE) if (state < FRSKY_BIND_DONE)
return; return;
#ifdef Throttle_pin
uint16_t throttle = analogRead(Throttle_pin); uint16_t throttle = analogRead(Throttle_pin);
uint16_t yaw = analogRead(Yaw_pin); uint16_t yaw = analogRead(Yaw_pin);
uint16_t roll = analogRead(Roll_pin); uint16_t roll = analogRead(Roll_pin);
@ -34,13 +33,6 @@ void update_inputs(void) {
//debugln("T %d y %d r %d p %d a1 %d a2 %d",throttle,yaw,roll,pitch,aux1,aux2); //debugln("T %d y %d r %d p %d a1 %d a2 %d",throttle,yaw,roll,pitch,aux1,aux2);
//debugln("T %d y %d r %d p %d a1 %d a2 %d",Channel_data[THROTTLE],Channel_data[RUDDER],Channel_data[AILERON],Channel_data[ELEVATOR],Channel_data[CH5],Channel_data[CH6]); //debugln("T %d y %d r %d p %d a1 %d a2 %d",Channel_data[THROTTLE],Channel_data[RUDDER],Channel_data[AILERON],Channel_data[ELEVATOR],Channel_data[CH5],Channel_data[CH6]);
#else
//analogRead()
if (Channel_data[THROTTLE] < CHANNEL_MAX_100) {
Channel_data[THROTTLE] += 1;
} else {
Channel_data[THROTTLE] = CHANNEL_MIN_100;
}
#endif
} }

139
Multiprotocol/state.cpp

@ -0,0 +1,139 @@
#include <LiquidCrystal_I2C.h>
#include "state.h"
#include "Arduino.h"
#include "debug.h"
LiquidCrystal_I2C lcd(0x27,16,2);
State *curr_state = NULL;
State *new_state = NULL;
State *s_init = new LCD_state_init();
State *s_bind = new LCD_state_bind();
enum lcd_special_chars {
battery_66 = 0,
battery_33 = 1,
battery_0 = 2,
battery_100 = 3,
rssiantenna = 4,
rssi_bars_1 = 5,
rssi_bars_2 = 6,
rssi_bars_3 = 7,
MAX_SPECIAL_CHARS =8,
};
// 6 Byte-Arrays für 6 verschiedene Batteriesymbole
__extension__ struct lcd_special_chars_data { byte data[MAX_SPECIAL_CHARS]; }
lcd_special_chars_data[MAX_SPECIAL_CHARS] =
{
//[battery_0] =
{ 0b01110, 0b11011, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b11111 },
//[battery_33] =
{ 0b01110, 0b11011, 0b10001, 0b10001, 0b10001, 0b11111, 0b11111, 0b11111 },
//[battery_66] =
{ 0b01110, 0b11011, 0b10001, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111 },
//[battery_100] =
{ 0b01110, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111 },
//[rssiantenna] =
{ 0b10101, 0b10101, 0b01110, 0b00100, 0b00100, 0b00101, 0b00101, 0b00101 },
//[rssi_bars_1] =
{ 0b00001, 0b00001, 0b00001, 0b00001, 0b00101, 0b00101, 0b10101, 0b10101 },
//[rssi_bars_2] =
{ 0b00001, 0b00001, 0b00001, 0b00001, 0b10101, 0b10101, 0b10101, 0b10101 },
//[rssi_bars_3] =
{ 0b00001, 0b00001, 0b00101, 0b00101, 0b10101, 0b10101, 0b10101, 0b10101 },
};
void install_special_caracters(void)
{
for(int i = 0; i < MAX_SPECIAL_CHARS; i ++) {
lcd.createChar(i, lcd_special_chars_data[i].data);
}
}
void init_state(void) {
Wire.setSDA(PB9);
Wire.setSCL(PB8);
Wire.begin();
lcd.init();
lcd.backlight();
curr_state = NULL;
new_state = s_init;
install_special_caracters();
update_state();
}
void update_state(void) {
if(curr_state == new_state) {
if(curr_state)
curr_state->update();
} else {
if(curr_state)
curr_state->leave();
curr_state = new_state;
if(curr_state)
curr_state->enter();
}
}
//LCD_state_init
LCD_state_init::LCD_state_init(void) {
snprintf(this->line1,sizeof(this->line2)," wellcome ");
snprintf(this->line2,sizeof(this->line2)," phschoen ");
}
void LCD_state_init::enter(void) {
lcd.setCursor(0,0);
lcd.print(this->line1);
lcd.setCursor(0,1);
lcd.print(this->line2);
time_enter = millis();
}
void LCD_state_init::update(void)
{
uint32_t diff;
diff = millis()-time_enter;
if (diff > 5 * 1000) {
new_state = s_bind;
}
}
void LCD_state_init::leave(void)
{
}
//LCD_state_bind
LCD_state_bind::LCD_state_bind(void) {
snprintf(this->line1,sizeof(this->line2),"bind mode ");
snprintf(this->line2,sizeof(this->line2)," ");
this->bind_time = 20;
}
void LCD_state_bind::enter(void) {
lcd.setCursor(0,0);
lcd.print(this->line1);
lcd.setCursor(0,1);
lcd.print(this->line2);
this->time_enter = millis();
}
void LCD_state_bind::update(void)
{
debugln("blubber\n");
unsigned long remain = millis() - this->time_enter;
remain = remain/1000; // to sec
remain = this->bind_time - remain;
snprintf(this->line2,sizeof(this->line2),"remaining sec %02d",remain);
lcd.setCursor(0,1);
lcd.print(this->line2);
time_enter = millis();
}
void LCD_state_bind::leave(void)
{
}

62
Multiprotocol/state.h

@ -0,0 +1,62 @@
#ifndef _STATE_H_
#define _STATE_H_
#include <LiquidCrystal_I2C.h>
void init_state(void);
void update_state(void);
class State {
protected:
char line1[17];
char line2[17];
public:
virtual void enter(void) {
}
virtual void update(void) {
}
virtual void leave(void) {
}
};
extern State *new_state;
class LCD_state_init: public State {
private:
unsigned long time_enter;
public:
LCD_state_init(void);
void enter(void);
void update(void);
void leave(void);
};
class LCD_state_bind: public State {
private:
unsigned long time_enter;
unsigned long bind_time;
public:
LCD_state_bind(void);
void enter(void);
void update(void);
void leave(void);
};
class LCD_state_flight: public State {
private:
unsigned long time_enter;
public:
void enter(void);
void update(void);
void leave(void);
};
#endif /*_STATE_H_*/
Loading…
Cancel
Save