battery stuff, gui

master
ThirstyShark 5 years ago
parent 02c6d58080
commit ecc3a022f1

@ -1,25 +1,27 @@
#include <EveryTimer.h>
#include <OneShotTimer.h>
#include <TimeLib.h>
#include <TFT_FastPin.h>
#include <TFT_ILI9341.h>
#include <User_Setup.h>
#include <./User_Setup.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
//#include "./PDQ_ILI9341_config.h"
//#include "PDQ_GFX.h"
//#include "PDQ_ILI9341.h"
#include "SD.h"
#include <Fonts/FreeSerif12pt7b.h> // include fancy serif font
#include <Fonts/FreeSans12pt7b.h>
// For the Adafruit shield, these are the default.
#define TFT_DC 48
#define TFT_CS 49
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
TFT_ILI9341 tft;
TFT_ILI9341 tft = TFT_ILI9341();
extern "C" char __data_start[]; // start of SRAM data
extern "C" char _end[]; // end of SRAM data (used to check amount of SRAM this program's variables use)
extern "C" char __data_load_end[]; // end of FLASH (used to check amount of Flash this program's code and data uses)
EveryTimer batteryupd;
EveryTimer water;
#include <TouchScreen.h>
#include <avr/power.h>
@ -125,19 +127,31 @@ word pinA15 = A15;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 333);
String outtext = "";
String oldtext = "";
boolean fastboot = false;
boolean debug = true;
boolean debug;
boolean ps3 = false;
boolean upd;
boolean upd = true;
boolean s1, s2, s3, s4;
String outtext2 = "";
int linecount = 0;
void setup(void) {
batteryupd.Every(10000, checkBat);
water.Every(1800000, runWater);
debug = true;
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
pinMode(9, INPUT_PULLUP);
setTime(0);
Serial.begin(9600);
//tft.reset();
#if defined(ILI9341_RST_PIN) // reset like Adafruit does
@ -174,7 +188,8 @@ void setup(void) {
printFastCText("[FAILED] SD Card not detected!");
}
printFastCText("[ OK ] Enabling powersaving features... "); // 53 characters wide example
enablePS(3);
enablePS(0);
pinMode(A0, INPUT);
upd = true;
if (!soil1.begin(0x36)) {
@ -213,10 +228,22 @@ void setup(void) {
printFastCText(out);
s4 = true;
}
getMoisture();
debug = false;
//getMoisture();
}
const int sec = 1000;
const int min = 1000 * 60;
const int hr = 1000 * 60 * 60;
void loop(void) {
TSPoint p = ts.getPoint();
if (p.z > ts.pressureThreshhold) {
p.x = map(p.x, TS_MINX, TS_MAXX, tft.height(), 0);
p.y = map(p.y, TS_MINY, TS_MAXY, tft.width(), 0);
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\tPressure = "); Serial.println(p.z);
}
if (ps3) {
ADCSRA = 0;
}
@ -261,184 +288,122 @@ void loop(void) {
}
tmp += mil % 1000;
tmp += "]";*/
//printConsoleText(tmp);
//printFastCText(tmp);
//tft.scrollTo(320);
if(mil % 60000 < 3000) {
/*if(mil % 20000 < 500) {
getMoisture();
delay(500);
//tft.scrollTo(480);
//tft.setAddrWindow(320, 0, 320, 240);
}
upd = true;
}*/
// change upd to true if screen update needed
if(!debug && upd) {
tft.fillScreen(ILI9341_BLACK);
yield();
tft.fillRect(0, 0, 320, 20, ILI9341_WHITE);
tft.fillRect(0, 0, 160, 20, ILI9341_WHITE);
tft.setCursor(2, 2);
tft.setTextSize(2);
tft.setTextFont(4);
tft.setTextColor(ILI9341_BLACK);
tft.print("Water Level: ");
String out = "Water Level:";
tft.drawString(out.c_str(), 2, 2, 2);
drawButton(0, 20, 160, 110, "Settings", "ILI9341_YELLOW");
drawButton(160, 20, 160, 110, "Stats", "ILI9341_GREEN");
upd = false;
}
batteryupd.Update();
water.Update();
}
#define BUFFPIXEL 80
/*
void bmpDraw(char *filename, int16_t x, int16_t y) {
File bmpFile;
int bmpWidth, bmpHeight; // W+H in pixels
uint8_t bmpDepth; // Bit depth (currently must be 24)
uint32_t bmpImageoffset; // Start of image data in file
uint32_t rowSize; // Not always = bmpWidth; may have padding
uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
boolean goodBmp = false; // Set to true on valid header parse
boolean flip = true; // BMP is stored bottom-to-top
int w, h, row, col, x2, y2, bx1, by1;
uint8_t r, g, b;
uint32_t pos = 0, startTime = millis();
if((x >= tft.width()) || (y >= tft.height())) return;
Serial.println();
Serial.print(F("Loading image '"));
Serial.print(filename);
Serial.println('\'');
// Open requested file on SD card
if ((bmpFile = SD.open(filename)) == NULL) {
Serial.print(F("File not found"));
return;
}
// Parse BMP header
if(read16(bmpFile) == 0x4D42) { // BMP signature
Serial.print(F("File size: ")); Serial.println(read32(bmpFile));
(void)read32(bmpFile); // Read & ignore creator bytes
bmpImageoffset = read32(bmpFile); // Start of image data
Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
// Read DIB header
Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
bmpWidth = read32(bmpFile);
bmpHeight = read32(bmpFile);
if(read16(bmpFile) == 1) { // # planes -- must be '1'
bmpDepth = read16(bmpFile); // bits per pixel
Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
goodBmp = true; // Supported BMP format -- proceed!
Serial.print(F("Image size: "));
Serial.print(bmpWidth);
Serial.print('x');
Serial.println(bmpHeight);
// BMP rows are padded (if needed) to 4-byte boundary
rowSize = (bmpWidth * 3 + 3) & ~3;
// If bmpHeight is negative, image is in top-down order.
// This is not canon but has been observed in the wild.
if(bmpHeight < 0) {
bmpHeight = -bmpHeight;
flip = false;
}
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog
} // end of WDT_vect
// Crop area to be loaded
x2 = x + bmpWidth - 1; // Lower-right corner
y2 = y + bmpHeight - 1;
if((x2 >= 0) && (y2 >= 0)) { // On screen?
w = bmpWidth; // Width/height of section to load/display
h = bmpHeight;
bx1 = by1 = 0; // UL coordinate in BMP file
if(x < 0) { // Clip left
bx1 = -x;
x = 0;
w = x2 + 1;
}
if(y < 0) { // Clip top
by1 = -y;
y = 0;
h = y2 + 1;
}
if(x2 >= tft.width()) w = tft.width() - x; // Clip right
if(y2 >= tft.height()) h = tft.height() - y; // Clip bottom
// Set TFT address window to clipped image bounds
tft.startWrite(); // Requires start/end transaction now
tft.setAddrWindow(x, y, w, h);
for (row=0; row<h; row++) { // For each scanline...
// Seek to start of scan line. It might seem labor-
// intensive to be doing this on every line, but this
// method covers a lot of gritty details like cropping
// and scanline padding. Also, the seek only takes
// place if the file position actually needs to change
// (avoids a lot of cluster math in SD library).
if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
pos = bmpImageoffset + (bmpHeight - 1 - (row + by1)) * rowSize;
else // Bitmap is stored top-to-bottom
pos = bmpImageoffset + (row + by1) * rowSize;
pos += bx1 * 3; // Factor in starting column (bx1)
if(bmpFile.position() != pos) { // Need seek?
tft.endWrite(); // End TFT transaction
bmpFile.seek(pos);
buffidx = sizeof(sdbuffer); // Force buffer reload
tft.startWrite(); // Start new TFT transaction
}
for (col=0; col<w; col++) { // For each pixel...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
tft.endWrite(); // End TFT transaction
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
tft.startWrite(); // Start new TFT transaction
}
// Convert pixel from BMP to TFT format, push to display
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
tft.writePixel(tft.color565(r,g,b));
} // end pixel
} // end scanline
tft.endWrite(); // End last TFT transaction
} // end onscreen
Serial.print(F("Loaded in "));
Serial.print(millis() - startTime);
Serial.println(" ms");
} // end goodBmp
void checkBat() {
if(debug) {
String out = "[ OK ] Battery: ";
int y = 0;
for(int x = 0; x < 20; x++) {
y += analogRead(0);
delay(10);
}
if(analogRead(1) > 910) {
out += map(y / 20, 572, 859, 0, 100) - 20;
} else {
out += map(y / 20, 572, 859, 0, 100);
}
out += "%, Raw: ";
out += y/20;
out += ".";
if(analogRead(1) > 910) {
out += ". Battery Charging.";
}
printFastCText(out);
} else {
String out = "Battery: ";
int y = 0;
for(int x = 0; x < 20; x++) {
y += analogRead(0);
delay(10);
}
if(analogRead(1) > 910) {
out += map(y / 20, 572, 859, 0, 100) - 20;
} else {
out += map(y / 20, 572, 859, 0, 100);
}
out += "%";
if(analogRead(1) > 910) {
out += " Charging";
}
tft.fillRect(160, 0, 160, 20, ILI9341_WHITE);
tft.drawString(out.c_str(), 162, 2, 2);
}
bmpFile.close();
if(!goodBmp) Serial.println(F("BMP format not recognized."));
}
*/
uint16_t read16(File &f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t read32(File &f) {
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
void runWater() {
getMoisture();
if(s1 && map(soil1.touchRead(0), 0, 1023, 0, 100) <= 50) {
int td = 50 - map(soil1.touchRead(0), 0, 1023, 0, 100);
String out = "[ INFO ] Running water on box 1 for ";
out += td / 10.0;
out += " sec.";
printFastCText(out);
digitalWrite(2, LOW);
delay(td * 100);
digitalWrite(2, HIGH);
}
if(s2 && map(soil2.touchRead(0), 0, 1023, 0, 100) <= 50) {
int td = 50 - map(soil2.touchRead(0), 0, 1023, 0, 100);
String out = "[ INFO ] Running water on box 2 for ";
out += td / 10.0;
out += " sec.";
printFastCText(out);
digitalWrite(3, LOW);
delay(td * 100);
digitalWrite(3, HIGH);
}
if(s3 && map(soil3.touchRead(0), 0, 1023, 0, 100) <= 50) {
int td = 50 - map(soil3.touchRead(0), 0, 1023, 0, 100);
String out = "[ INFO ] Running water on box 3 for ";
out += td / 10.0;
out += " sec.";
printFastCText(out);
digitalWrite(4, LOW);
delay(td * 100);
digitalWrite(4, HIGH);
}
if(s4 && map(soil4.touchRead(0), 0, 1023, 0, 100) <= 50) {
int td = 50 - map(soil4.touchRead(0), 0, 1023, 0, 100);
String out = "[ INFO ] Running water on box 4 for ";
out += td / 10.0;
out += " sec.";
printFastCText(out);
digitalWrite(5, LOW);
delay(td * 100);
digitalWrite(5, HIGH);
}
}
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog
} // end of WDT_vect
void getMoisture() {
if (s1) {
float t1 = soil1.getTemp();
@ -662,3 +627,9 @@ void scrollAddress(uint16_t vsp) {
tft.writedata(vsp>>8);
tft.writedata(vsp);
}
void drawButton(int x, int y, int w, int h, char* text, char* color) {
tft.setTextFont(4);
tft.fillRect(x, y, w, h, color);
tft.drawCentreString(text, x + w/2, y + h/2 - 15, 4);
}

@ -80,5 +80,5 @@
// run slightly faster, so leave it commented out unless you need it!
// Transaction support is needed to work with SD libraru but not needed with TFT_SdFat
// #define SUPPORT_TRANSACTIONS
#define SUPPORT_TRANSACTIONS

Loading…
Cancel
Save