2019-12-18 01:32:44 -05:00
|
|
|
#include <Arduino.h>
|
2020-02-09 18:56:43 -05:00
|
|
|
#include "colors.h"
|
2020-01-04 02:11:55 -05:00
|
|
|
#include "settings.h"
|
2019-12-18 01:32:44 -05:00
|
|
|
#include "ubitx.h"
|
|
|
|
#include "nano_gui.h"
|
2020-02-09 21:41:13 -05:00
|
|
|
#include "touch.h"
|
2019-12-18 01:32:44 -05:00
|
|
|
|
|
|
|
#include <SPI.h>
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
|
2020-01-15 02:33:11 -05:00
|
|
|
|
2019-12-31 20:31:05 -05:00
|
|
|
/*****************
|
|
|
|
* Begin TFT functions
|
|
|
|
*****************/
|
|
|
|
#define ILI9341_CS_PIN TFT_CS
|
|
|
|
#define ILI9341_DC_PIN TFT_DC
|
|
|
|
#include "PDQ_MinLib/PDQ_ILI9341.h"
|
|
|
|
PDQ_ILI9341 tft;
|
2019-12-18 01:32:44 -05:00
|
|
|
|
2019-12-31 20:31:05 -05:00
|
|
|
#include "nano_font.h"
|
2019-12-18 01:32:44 -05:00
|
|
|
|
2019-12-31 20:31:05 -05:00
|
|
|
void displayInit(void){
|
2020-01-08 02:07:44 -05:00
|
|
|
//Pulling this low 6 times should exit deep sleep mode
|
|
|
|
pinMode(TFT_CS,OUTPUT);
|
|
|
|
for(uint8_t i = 0; i < 6; ++i){
|
|
|
|
digitalWrite(TFT_CS,HIGH);
|
|
|
|
digitalWrite(TFT_CS,LOW);
|
|
|
|
}
|
|
|
|
digitalWrite(TFT_CS,HIGH);//Disable writing for now
|
|
|
|
|
2019-12-31 20:31:05 -05:00
|
|
|
tft.begin();
|
2020-01-01 23:49:35 -05:00
|
|
|
tft.setFont(ubitx_font);
|
2020-01-19 15:56:47 -05:00
|
|
|
tft.setTextWrap(true);
|
2019-12-31 20:31:05 -05:00
|
|
|
tft.setTextColor(DISPLAY_GREEN,DISPLAY_BLACK);
|
|
|
|
tft.setTextSize(1);
|
|
|
|
tft.setRotation(1);
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:31:05 -05:00
|
|
|
void displayPixel(unsigned int x, unsigned int y, unsigned int c){
|
|
|
|
tft.drawPixel(x,y,c);
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:31:05 -05:00
|
|
|
void displayHline(unsigned int x, unsigned int y, unsigned int w, unsigned int c){
|
|
|
|
tft.drawFastHLine(x,y,w,c);
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
2019-12-31 20:31:05 -05:00
|
|
|
void displayVline(unsigned int x, unsigned int y, unsigned int l, unsigned int c){
|
|
|
|
tft.drawFastVLine(x,y,l,c);
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-04 02:11:55 -05:00
|
|
|
void displayClear(unsigned int color){
|
2019-12-31 20:31:05 -05:00
|
|
|
tft.fillRect(0,0,320,240,color);
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void displayRect(unsigned int x,unsigned int y,unsigned int w,unsigned int h,unsigned int c){
|
2019-12-31 20:31:05 -05:00
|
|
|
tft.drawRect(x,y,w,h,c);
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void displayFillrect(unsigned int x,unsigned int y,unsigned int w,unsigned int h,unsigned int c){
|
2019-12-31 20:31:05 -05:00
|
|
|
tft.fillRect(x,y,w,h,c);
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void displayChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg) {
|
2019-12-31 20:31:05 -05:00
|
|
|
tft.drawCharGFX(x,y,c,color,bg,1);
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-19 15:56:47 -05:00
|
|
|
void displayRawText(char *text, int x1, int y1, int w, int color, int background){
|
|
|
|
tft.setTextColor(color,background);
|
|
|
|
tft.setCursor(x1,y1);
|
|
|
|
tft.setBound(x1,x1+w);
|
2019-12-31 20:31:05 -05:00
|
|
|
tft.print(text);
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-28 02:08:32 -05:00
|
|
|
void displayText(char *text, int x1, int y1, int w, int h, int color, int background, int border, TextJustification_e justification)
|
|
|
|
{
|
2019-12-18 01:32:44 -05:00
|
|
|
displayFillrect(x1, y1, w ,h, background);
|
|
|
|
displayRect(x1, y1, w ,h, border);
|
|
|
|
|
2019-12-31 20:31:05 -05:00
|
|
|
int16_t x1_out;
|
|
|
|
int16_t y1_out;
|
|
|
|
uint16_t width_out;
|
|
|
|
uint16_t height_out;
|
2020-01-19 15:56:47 -05:00
|
|
|
tft.getTextBounds(text,x1,y1,&x1_out,&y1_out,&width_out,&height_out,w);
|
2020-01-28 02:08:32 -05:00
|
|
|
if(TextJustification_e::Center == justification){
|
|
|
|
x1 += (w - ( (int32_t)width_out + (x1_out-x1)))/2;
|
|
|
|
}
|
|
|
|
else if(TextJustification_e::Right == justification){
|
|
|
|
x1 += w - ((int32_t)width_out + (x1_out-x1));
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
x1 += 2;//Give a little bit of padding from the border
|
|
|
|
}
|
2020-01-20 01:54:42 -05:00
|
|
|
y1 += (ubitx_font->yAdvance + h - ( (int32_t)height_out))/2;
|
2020-01-19 15:56:47 -05:00
|
|
|
displayRawText(text,x1,y1,w,color,background);
|
2019-12-18 01:32:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void setupTouch(){
|
|
|
|
int x1, y1, x2, y2, x3, y3, x4, y4;
|
2020-02-09 21:41:13 -05:00
|
|
|
Point ts_point;
|
2019-12-18 01:32:44 -05:00
|
|
|
|
|
|
|
displayClear(DISPLAY_BLACK);
|
2020-01-26 13:42:20 -05:00
|
|
|
strncpy_P(b,(const char*)F("Click on the cross"),sizeof(b));
|
|
|
|
displayText(b, 20,100, 200, 50, DISPLAY_WHITE, DISPLAY_BLACK, DISPLAY_BLACK);
|
2019-12-18 01:32:44 -05:00
|
|
|
|
|
|
|
// TOP-LEFT
|
|
|
|
displayHline(10,20,20,DISPLAY_WHITE);
|
|
|
|
displayVline(20,10,20, DISPLAY_WHITE);
|
|
|
|
|
2020-02-09 20:58:45 -05:00
|
|
|
while(!readTouch(&ts_point))
|
2019-12-18 01:32:44 -05:00
|
|
|
delay(100);
|
2020-02-09 20:58:45 -05:00
|
|
|
while(readTouch(&ts_point))
|
2019-12-18 01:32:44 -05:00
|
|
|
delay(100);
|
|
|
|
x1 = ts_point.x;
|
|
|
|
y1 = ts_point.y;
|
|
|
|
|
|
|
|
//rubout the previous one
|
|
|
|
displayHline(10,20,20,DISPLAY_BLACK);
|
|
|
|
displayVline(20,10,20, DISPLAY_BLACK);
|
|
|
|
|
|
|
|
delay(1000);
|
|
|
|
|
|
|
|
//TOP RIGHT
|
|
|
|
displayHline(290,20,20,DISPLAY_WHITE);
|
|
|
|
displayVline(300,10,20, DISPLAY_WHITE);
|
|
|
|
|
2020-02-09 20:58:45 -05:00
|
|
|
while(!readTouch(&ts_point))
|
2019-12-18 01:32:44 -05:00
|
|
|
delay(100);
|
2020-02-09 20:58:45 -05:00
|
|
|
while(readTouch(&ts_point))
|
2019-12-18 01:32:44 -05:00
|
|
|
delay(100);
|
|
|
|
x2 = ts_point.x;
|
|
|
|
y2 = ts_point.y;
|
|
|
|
|
|
|
|
displayHline(290,20,20,DISPLAY_BLACK);
|
|
|
|
displayVline(300,10,20, DISPLAY_BLACK);
|
|
|
|
|
|
|
|
delay(1000);
|
|
|
|
|
|
|
|
//BOTTOM LEFT
|
|
|
|
displayHline(10,220,20,DISPLAY_WHITE);
|
|
|
|
displayVline(20,210,20, DISPLAY_WHITE);
|
|
|
|
|
2020-02-09 20:58:45 -05:00
|
|
|
while(!readTouch(&ts_point))
|
2019-12-18 01:32:44 -05:00
|
|
|
delay(100);
|
|
|
|
x3 = ts_point.x;
|
|
|
|
y3 = ts_point.y;
|
|
|
|
|
2020-02-09 20:58:45 -05:00
|
|
|
while(readTouch(&ts_point))
|
2019-12-18 01:32:44 -05:00
|
|
|
delay(100);
|
|
|
|
displayHline(10,220,20,DISPLAY_BLACK);
|
|
|
|
displayVline(20,210,20, DISPLAY_BLACK);
|
|
|
|
|
|
|
|
delay(1000);
|
|
|
|
|
|
|
|
//BOTTOM RIGHT
|
|
|
|
displayHline(290,220,20,DISPLAY_WHITE);
|
|
|
|
displayVline(300,210,20, DISPLAY_WHITE);
|
|
|
|
|
2020-02-09 20:58:45 -05:00
|
|
|
while(!readTouch(&ts_point))
|
2019-12-18 01:32:44 -05:00
|
|
|
delay(100);
|
|
|
|
x4 = ts_point.x;
|
|
|
|
y4 = ts_point.y;
|
|
|
|
|
|
|
|
|
|
|
|
displayHline(290,220,20,DISPLAY_BLACK);
|
|
|
|
displayVline(300,210,20, DISPLAY_BLACK);
|
|
|
|
|
|
|
|
// we average two readings and divide them by half and store them as scaled integers 10 times their actual, fractional value
|
|
|
|
//the x points are located at 20 and 300 on x axis, hence, the delta x is 280, we take 28 instead, to preserve fractional value,
|
|
|
|
//there are two readings (x1,x2) and (x3, x4). Hence, we have to divide by 28 * 2 = 56
|
2020-01-04 02:11:55 -05:00
|
|
|
globalSettings.touchSlopeX = ((x4 - x3) + (x2 - x1))/56;
|
2019-12-18 01:32:44 -05:00
|
|
|
//the y points are located at 20 and 220 on the y axis, hence, the delta is 200. we take it as 20 instead, to preserve the fraction value
|
|
|
|
//there are two readings (y1, y2) and (y3, y4). Hence we have to divide by 20 * 2 = 40
|
2020-01-04 02:11:55 -05:00
|
|
|
globalSettings.touchSlopeY = ((y3 - y1) + (y4 - y2))/40;
|
2019-12-18 01:32:44 -05:00
|
|
|
|
|
|
|
//x1, y1 is at 20 pixels
|
2020-01-04 02:11:55 -05:00
|
|
|
globalSettings.touchOffsetX = x1 + -((20 * globalSettings.touchSlopeX)/10);
|
|
|
|
globalSettings.touchOffsetY = y1 + -((20 * globalSettings.touchSlopeY)/10);
|
2019-12-18 01:32:44 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
Serial.print(x1);Serial.print(':');Serial.println(y1);
|
|
|
|
Serial.print(x2);Serial.print(':');Serial.println(y2);
|
|
|
|
Serial.print(x3);Serial.print(':');Serial.println(y3);
|
|
|
|
Serial.print(x4);Serial.print(':');Serial.println(y4);
|
|
|
|
|
|
|
|
//for debugging
|
2020-01-04 02:11:55 -05:00
|
|
|
Serial.print(globalSettings.touchSlopeX); Serial.print(' ');
|
|
|
|
Serial.print(globalSettings.touchSlopeY); Serial.print(' ');
|
|
|
|
Serial.print(globalSettings.touchOffsetX); Serial.print(' ');
|
|
|
|
Serial.println(globalSettings.touchOffsetY); Serial.println(' ');
|
2019-12-18 01:32:44 -05:00
|
|
|
*/
|
2020-02-09 21:41:13 -05:00
|
|
|
SaveSettingsToEeprom();
|
2019-12-18 01:32:44 -05:00
|
|
|
displayClear(DISPLAY_BLACK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|