sets the initial brightness based on the time
This commit is contained in:
parent
50b724dabc
commit
041c51042d
2 changed files with 190 additions and 25 deletions
|
|
@ -5,6 +5,7 @@
|
|||
#include "Adafruit_LEDBackpack.h" //for screen
|
||||
#include <stdlib.h>
|
||||
#include <atomic>
|
||||
#include <SolarCalculator.h> //for determining startup brightness
|
||||
#define DISPLAY_ADDRESS 0x70 //I2C address of screen
|
||||
#define GPS_WAIT_TIME 1100
|
||||
#define GPS_UPDATE_FREQUENCY 10 //GPS update frequency per sec
|
||||
|
|
@ -13,6 +14,13 @@
|
|||
|
||||
//GPS
|
||||
SFE_UBLOX_GPS myGPS;
|
||||
double lat = 0;
|
||||
double lng = 0;
|
||||
int tzOff = 12; //local TZ offset
|
||||
bool centuryBit; //needed to get month from RTC
|
||||
double az, elev; //for sun location in the sky
|
||||
double transit, sunrise, sunset; //sun related times
|
||||
double civilTransit, civilRiise, civilSet; //astro related times
|
||||
|
||||
//threading
|
||||
std::atomic<float> speed(0);
|
||||
|
|
@ -29,10 +37,11 @@ unsigned long timeOfLastFix = 0UL;
|
|||
|
||||
//button
|
||||
volatile bool buttonPushed = false;
|
||||
volatile int screenBrightnessButton = 15;
|
||||
volatile int screenBrightnessCurrent = screenBrightnessButton;
|
||||
volatile int ScreenBrightnessUser = 15;
|
||||
volatile int screenBrightnessCurrent = ScreenBrightnessUser;
|
||||
volatile unsigned long lastButtonPush = 0UL;
|
||||
|
||||
|
||||
void chageBrightness() {
|
||||
if (millis() - lastButtonPush > DEBOUNCE_DELAY) {
|
||||
buttonPushed = true;
|
||||
|
|
@ -42,14 +51,14 @@ void chageBrightness() {
|
|||
|
||||
void connectGPS() { //need to do this everytime the GPS turns back on
|
||||
do { //set gps to 115200 baudrate code comes from sparkfun examples
|
||||
Serial.println("GNSS: trying 115200 baud");
|
||||
// Serial.println("GNSS: trying 115200 baud");
|
||||
Serial1.begin(115200);
|
||||
if (myGPS.begin(Serial1) == true) break;
|
||||
delay(100);
|
||||
Serial.println("GNSS: trying 9600 baud");
|
||||
//Serial.println("GNSS: trying 9600 baud");
|
||||
Serial1.begin(9600);
|
||||
if (myGPS.begin(Serial1) == true) {
|
||||
Serial.println("GNSS: connected at 9600 baud, switching to 115200");
|
||||
// Serial.println("GNSS: connected at 9600 baud, switching to 115200");
|
||||
myGPS.setSerialRate(115200);
|
||||
delay(100);
|
||||
} else {
|
||||
|
|
@ -62,18 +71,33 @@ void connectGPS() { //need to do this everytime the GPS turns back on
|
|||
myGPS.setDynamicModel(DYN_MODEL_AUTOMOTIVE); //set dynamic model of GPS to automotive to more accutate results
|
||||
}
|
||||
|
||||
bool setStartBrightness() {
|
||||
while (!(myGPS.getFixType() > 0)) {}
|
||||
double GPSTime=myGPS.getHour()+(myGPS.getMinute()/60);
|
||||
|
||||
//get sunset time
|
||||
calcSunriseSunset(myGPS.getYear(), myGPS.getMonth(), myGPS.getDay(), lat, lng, transit, sunrise, sunset);
|
||||
//get civilSet time
|
||||
calcCivilDawnDusk(myGPS.getYear(), myGPS.getMonth(), myGPS.getDay(), lat, lng, civilTransit, civilRiise, civilSet);
|
||||
|
||||
if(GPSTime<sunset){
|
||||
screenBrightnessCurrent=15;
|
||||
} else if ((GPSTime>sunrise)&&(GPSTime<civilSet)){
|
||||
screenBrightnessCurrent=MIDDLE_SCREEN_BRIGHTNESS;
|
||||
} else if (GPSTime>civilSet){
|
||||
screenBrightnessCurrent=0;
|
||||
}
|
||||
disp.setBrightness(screenBrightnessCurrent);
|
||||
return true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
//setup screen
|
||||
Wire.setSCL(17);
|
||||
Wire.setSDA(16);
|
||||
//Wire.setClock(100000);
|
||||
disp.begin(DISPLAY_ADDRESS);
|
||||
disp.setBrightness(screenBrightnessButton);
|
||||
disp.clear();
|
||||
disp.writeDigitAscii(0, 71); //"G"
|
||||
disp.writeDigitAscii(1, 80); //"P"
|
||||
disp.writeDigitAscii(2, 83); //"S"
|
||||
disp.writeDisplay();
|
||||
disp.setBrightness(ScreenBrightnessUser);
|
||||
|
||||
//screen brightness
|
||||
pinMode(20, INPUT_PULLUP);
|
||||
|
|
@ -86,30 +110,29 @@ void setup1() {
|
|||
Serial1.setRX(1);
|
||||
Serial1.setTX(0);
|
||||
connectGPS();
|
||||
setStartBrightness();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
|
||||
//change brightness
|
||||
if (buttonPushed == true) {
|
||||
if (screenBrightnessButton == 15) {
|
||||
screenBrightnessButton = MIDDLE_SCREEN_BRIGHTNESS;
|
||||
} else if (screenBrightnessButton == MIDDLE_SCREEN_BRIGHTNESS) {
|
||||
screenBrightnessButton = 0;
|
||||
if (ScreenBrightnessUser == 15) {
|
||||
ScreenBrightnessUser = MIDDLE_SCREEN_BRIGHTNESS;
|
||||
} else if (ScreenBrightnessUser == MIDDLE_SCREEN_BRIGHTNESS) {
|
||||
ScreenBrightnessUser = 0;
|
||||
} else {
|
||||
screenBrightnessButton = 15;
|
||||
ScreenBrightnessUser = 15;
|
||||
}
|
||||
disp.setBrightness(screenBrightnessButton);
|
||||
screenBrightnessCurrent=screenBrightnessButton;
|
||||
disp.setBrightness(ScreenBrightnessUser);
|
||||
screenBrightnessCurrent = ScreenBrightnessUser;
|
||||
buttonPushed = false;
|
||||
}
|
||||
|
||||
disp.clear();
|
||||
if (gpsFixType.load() > 0) {
|
||||
if (screenBrightnessCurrent != screenBrightnessButton){
|
||||
disp.setBrightness(screenBrightnessButton);
|
||||
screenBrightnessCurrent=screenBrightnessButton;
|
||||
if (screenBrightnessCurrent != ScreenBrightnessUser) {
|
||||
disp.setBrightness(ScreenBrightnessUser);
|
||||
screenBrightnessCurrent = ScreenBrightnessUser;
|
||||
}
|
||||
timeOfLastFix = millis(); //reset no gps screen display
|
||||
int temp = speed.load() * 10;
|
||||
|
|
@ -128,9 +151,9 @@ void loop() {
|
|||
disp.writeDigitAscii(2, 48 + digit2, true);
|
||||
disp.writeDigitAscii(3, 48 + digit3);
|
||||
} else {
|
||||
if (screenBrightnessCurrent!=0){
|
||||
if (screenBrightnessCurrent != 0) {
|
||||
disp.setBrightness(0);
|
||||
screenBrightnessCurrent=0;
|
||||
screenBrightnessCurrent = 0;
|
||||
}
|
||||
unsigned long currentTime = millis();
|
||||
unsigned long timeSinceLastFIx = timeOfLastFix - currentTime;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue