TeensyPwm/display.cpp
2015-02-15 18:27:54 +01:00

102 lines
1.9 KiB
C++

#include <Arduino.h>
#include <stdint.h>
#include <LiquidCrystal.h>
#include "hardware.h"
#include "display.h"
#include "pwm.h"
#include "rotary.h"
LiquidCrystal lcd(LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
const uint32_t DISPLAY_UPDATE_TIME = 5e5; // microseconds
void displayInit() {
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.print("Teensy SMPS");
}
void displayExec() {
static uint8_t state = 0;
static uint32_t lastDisplayCycle = 0;
uint32_t currentTime = micros();
if ((lastDisplayCycle + DISPLAY_UPDATE_TIME <= currentTime) || (lastDisplayCycle > currentTime)) {
lastDisplayCycle = currentTime;
int rotaryCount = getAndResetRotaryCount();
bool switchState = getAndResetSwitchState();
lcd.clear();
lcd.home();
lcd.print(getUDes(), 0);
lcd.print("V ");
lcd.print(getUCur(), 0);
lcd.print("V ");
lcd.print(getDutyCycle(), 0);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print(getP(), 1);
lcd.print(" ");
lcd.print(getI(), 1);
lcd.print(" ");
lcd.print(getD(), 1);
lcd.print(" ");
switch (state) {
case 0:
lcd.setCursor(15, 0);
lcd.print(" ");
if (switchState) {
state = 1;
}
break;
case 1:
if (switchState) {
state = 2;
}
lcd.setCursor(15, 0);
lcd.print("U");
if (rotaryCount != 0) {
setUDes(getUDes() + rotaryCount);
}
break;
case 2:
if (switchState) {
state = 3;
}
lcd.setCursor(15, 0);
lcd.print("P");
if (rotaryCount != 0) {
setP(getP() + 0.1 * rotaryCount);
}
break;
case 3:
if (switchState) {
state = 4;
}
lcd.setCursor(15, 0);
lcd.print("I");
if (rotaryCount != 0) {
setI(getI() + 0.1 * rotaryCount);
}
break;
case 4:
if (switchState) {
state = 5;
}
lcd.setCursor(15, 0);
lcd.print("D");
if (rotaryCount != 0) {
setD(getD() + 0.1 * rotaryCount);
}
break;
default:
state = 0;
break;
}
}
}