Move touch functions to their own files
This commit is contained in:
parent
72fc92b584
commit
947624518d
130
nano_gui.cpp
130
nano_gui.cpp
|
@ -3,12 +3,11 @@
|
|||
#include "settings.h"
|
||||
#include "ubitx.h"
|
||||
#include "nano_gui.h"
|
||||
#include "touch.h"
|
||||
|
||||
#include <SPI.h>
|
||||
#include <avr/pgmspace.h>
|
||||
|
||||
struct Point ts_point;
|
||||
|
||||
/*
|
||||
* This formats the frequency given in f
|
||||
*/
|
||||
|
@ -47,122 +46,6 @@ void formatFreq(uint32_t freq, char* buff, uint16_t buff_size, uint8_t fixed_wid
|
|||
}
|
||||
}
|
||||
|
||||
void readTouchCalibration(){
|
||||
LoadSettingsFromEeprom();
|
||||
/* for debugging
|
||||
Serial.print(globalSettings.touchSlopeX); Serial.print(' ');
|
||||
Serial.print(globalSettings.touchSlopeY); Serial.print(' ');
|
||||
Serial.print(globalSettings.touchOffsetX); Serial.print(' ');
|
||||
Serial.println(globalSettings.touchOffsetY); Serial.println(' ');
|
||||
//*/
|
||||
}
|
||||
|
||||
void writeTouchCalibration(){
|
||||
SaveSettingsToEeprom();
|
||||
}
|
||||
|
||||
#define Z_THRESHOLD 400
|
||||
#define Z_THRESHOLD_INT 75
|
||||
#define MSEC_THRESHOLD 3
|
||||
|
||||
static uint32_t msraw=0x80000000;
|
||||
static int16_t xraw=0, yraw=0, zraw=0;
|
||||
static uint8_t rotation = 1;
|
||||
|
||||
static int16_t touch_besttwoavg( int16_t x , int16_t y , int16_t z ) {
|
||||
int16_t da, db, dc;
|
||||
int16_t reta = 0;
|
||||
if ( x > y ) da = x - y; else da = y - x;
|
||||
if ( x > z ) db = x - z; else db = z - x;
|
||||
if ( z > y ) dc = z - y; else dc = y - z;
|
||||
|
||||
if ( da <= db && da <= dc ) reta = (x + y) >> 1;
|
||||
else if ( db <= da && db <= dc ) reta = (x + z) >> 1;
|
||||
else reta = (y + z) >> 1; // else if ( dc <= da && dc <= db ) reta = (x + y) >> 1;
|
||||
|
||||
return (reta);
|
||||
}
|
||||
|
||||
static void touch_update(){
|
||||
int16_t data[6];
|
||||
|
||||
uint32_t now = millis();
|
||||
if (now - msraw < MSEC_THRESHOLD) return;
|
||||
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV8);//2MHz
|
||||
digitalWrite(CS_PIN, LOW);
|
||||
SPI.transfer(0xB1 /* Z1 */);
|
||||
int16_t z1 = SPI.transfer16(0xC1 /* Z2 */) >> 3;
|
||||
int z = z1 + 4095;
|
||||
int16_t z2 = SPI.transfer16(0x91 /* X */) >> 3;
|
||||
z -= z2;
|
||||
if (z >= Z_THRESHOLD) {
|
||||
SPI.transfer16(0x91 /* X */); // dummy X measure, 1st is always noisy
|
||||
data[0] = SPI.transfer16(0xD1 /* Y */) >> 3;
|
||||
data[1] = SPI.transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements
|
||||
data[2] = SPI.transfer16(0xD1 /* Y */) >> 3;
|
||||
data[3] = SPI.transfer16(0x91 /* X */) >> 3;
|
||||
}
|
||||
else data[0] = data[1] = data[2] = data[3] = 0; // Compiler warns these values may be used unset on early exit.
|
||||
data[4] = SPI.transfer16(0xD0 /* Y */) >> 3; // Last Y touch power down
|
||||
data[5] = SPI.transfer16(0) >> 3;
|
||||
digitalWrite(CS_PIN, HIGH);
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV2);//Return to full speed for TFT
|
||||
|
||||
if (z < 0) z = 0;
|
||||
if (z < Z_THRESHOLD) { // if ( !touched ) {
|
||||
// Serial.println();
|
||||
zraw = 0;
|
||||
return;
|
||||
}
|
||||
zraw = z;
|
||||
|
||||
int16_t x = touch_besttwoavg( data[0], data[2], data[4] );
|
||||
int16_t y = touch_besttwoavg( data[1], data[3], data[5] );
|
||||
|
||||
//Serial.printf(" %d,%d", x, y);
|
||||
//Serial.println();
|
||||
if (z >= Z_THRESHOLD) {
|
||||
msraw = now; // good read completed, set wait
|
||||
switch (rotation) {
|
||||
case 0:
|
||||
xraw = 4095 - y;
|
||||
yraw = x;
|
||||
break;
|
||||
case 1:
|
||||
xraw = x;
|
||||
yraw = y;
|
||||
break;
|
||||
case 2:
|
||||
xraw = y;
|
||||
yraw = 4095 - x;
|
||||
break;
|
||||
default: // 3
|
||||
xraw = 4095 - x;
|
||||
yraw = 4095 - y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool readTouch(Point *const touch_point_out){
|
||||
touch_update();
|
||||
if (zraw >= Z_THRESHOLD) {
|
||||
touch_point_out->x = xraw;
|
||||
touch_point_out->y = yraw;
|
||||
//Serial.print(ts_point.x); Serial.print(",");Serial.println(ts_point.y);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void scaleTouch(Point *const p){
|
||||
p->x = ((long)(p->x - globalSettings.touchOffsetX) * 10L)/ (long)globalSettings.touchSlopeX;
|
||||
p->y = ((long)(p->y - globalSettings.touchOffsetY) * 10L)/ (long)globalSettings.touchSlopeY;
|
||||
|
||||
//Serial.print(p->x); Serial.print(",");Serial.println(p->y);
|
||||
}
|
||||
|
||||
/*****************
|
||||
* Begin TFT functions
|
||||
*****************/
|
||||
|
@ -173,12 +56,6 @@ PDQ_ILI9341 tft;
|
|||
|
||||
#include "nano_font.h"
|
||||
|
||||
|
||||
void xpt2046_Init(){
|
||||
pinMode(CS_PIN, OUTPUT);
|
||||
digitalWrite(CS_PIN, HIGH);
|
||||
}
|
||||
|
||||
void displayInit(void){
|
||||
//Pulling this low 6 times should exit deep sleep mode
|
||||
pinMode(TFT_CS,OUTPUT);
|
||||
|
@ -194,8 +71,6 @@ void displayInit(void){
|
|||
tft.setTextColor(DISPLAY_GREEN,DISPLAY_BLACK);
|
||||
tft.setTextSize(1);
|
||||
tft.setRotation(1);
|
||||
|
||||
xpt2046_Init();
|
||||
}
|
||||
|
||||
void displayPixel(unsigned int x, unsigned int y, unsigned int c){
|
||||
|
@ -258,6 +133,7 @@ void displayText(char *text, int x1, int y1, int w, int h, int color, int backgr
|
|||
|
||||
void setupTouch(){
|
||||
int x1, y1, x2, y2, x3, y3, x4, y4;
|
||||
Point ts_point;
|
||||
|
||||
displayClear(DISPLAY_BLACK);
|
||||
strncpy_P(b,(const char*)F("Click on the cross"),sizeof(b));
|
||||
|
@ -349,7 +225,7 @@ void setupTouch(){
|
|||
Serial.print(globalSettings.touchOffsetX); Serial.print(' ');
|
||||
Serial.println(globalSettings.touchOffsetY); Serial.println(' ');
|
||||
*/
|
||||
writeTouchCalibration();
|
||||
SaveSettingsToEeprom();
|
||||
displayClear(DISPLAY_BLACK);
|
||||
}
|
||||
|
||||
|
|
11
nano_gui.h
11
nano_gui.h
|
@ -1,12 +1,6 @@
|
|||
#ifndef _NANO_GUI_H_
|
||||
#define _NANO_GUI_H_
|
||||
|
||||
/* UI functions */
|
||||
struct Point {
|
||||
int x, y;
|
||||
};
|
||||
extern struct Point ts_point;
|
||||
|
||||
enum TextJustification_e : uint8_t
|
||||
{
|
||||
Left,
|
||||
|
@ -26,11 +20,6 @@ void displayText(char *text, int x1, int y1, int w, int h, int color, int backgr
|
|||
|
||||
void formatFreq(uint32_t freq, char* buff, uint16_t buff_size, uint8_t fixed_width = 0);
|
||||
|
||||
/* touch functions */
|
||||
bool readTouch(Point *const p);
|
||||
|
||||
void scaleTouch(Point *const p);
|
||||
|
||||
#define TEXT_LINE_HEIGHT 18
|
||||
#define TEXT_LINE_INDENT 5
|
||||
|
||||
|
|
|
@ -0,0 +1,111 @@
|
|||
#include "touch.h"
|
||||
|
||||
#include <SPI.h>
|
||||
|
||||
#include "ubitx.h"//pin assignments
|
||||
|
||||
#define Z_THRESHOLD 400
|
||||
#define Z_THRESHOLD_INT 75
|
||||
#define MSEC_THRESHOLD 3
|
||||
|
||||
static uint32_t msraw=0x80000000;
|
||||
static int16_t xraw=0, yraw=0, zraw=0;
|
||||
static uint8_t rotation = 1;
|
||||
|
||||
int16_t touch_besttwoavg( int16_t x , int16_t y , int16_t z ) {
|
||||
int16_t da, db, dc;
|
||||
int16_t reta = 0;
|
||||
if ( x > y ) da = x - y; else da = y - x;
|
||||
if ( x > z ) db = x - z; else db = z - x;
|
||||
if ( z > y ) dc = z - y; else dc = y - z;
|
||||
|
||||
if ( da <= db && da <= dc ) reta = (x + y) >> 1;
|
||||
else if ( db <= da && db <= dc ) reta = (x + z) >> 1;
|
||||
else reta = (y + z) >> 1; // else if ( dc <= da && dc <= db ) reta = (x + y) >> 1;
|
||||
|
||||
return (reta);
|
||||
}
|
||||
|
||||
void touch_update(){
|
||||
int16_t data[6];
|
||||
|
||||
uint32_t now = millis();
|
||||
if (now - msraw < MSEC_THRESHOLD) return;
|
||||
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV8);//2MHz
|
||||
digitalWrite(CS_PIN, LOW);
|
||||
SPI.transfer(0xB1 /* Z1 */);
|
||||
int16_t z1 = SPI.transfer16(0xC1 /* Z2 */) >> 3;
|
||||
int z = z1 + 4095;
|
||||
int16_t z2 = SPI.transfer16(0x91 /* X */) >> 3;
|
||||
z -= z2;
|
||||
if (z >= Z_THRESHOLD) {
|
||||
SPI.transfer16(0x91 /* X */); // dummy X measure, 1st is always noisy
|
||||
data[0] = SPI.transfer16(0xD1 /* Y */) >> 3;
|
||||
data[1] = SPI.transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements
|
||||
data[2] = SPI.transfer16(0xD1 /* Y */) >> 3;
|
||||
data[3] = SPI.transfer16(0x91 /* X */) >> 3;
|
||||
}
|
||||
else data[0] = data[1] = data[2] = data[3] = 0; // Compiler warns these values may be used unset on early exit.
|
||||
data[4] = SPI.transfer16(0xD0 /* Y */) >> 3; // Last Y touch power down
|
||||
data[5] = SPI.transfer16(0) >> 3;
|
||||
digitalWrite(CS_PIN, HIGH);
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV2);//Return to full speed for TFT
|
||||
|
||||
if (z < 0) z = 0;
|
||||
if (z < Z_THRESHOLD) { // if ( !touched ) {
|
||||
// Serial.println();
|
||||
zraw = 0;
|
||||
return;
|
||||
}
|
||||
zraw = z;
|
||||
|
||||
int16_t x = touch_besttwoavg( data[0], data[2], data[4] );
|
||||
int16_t y = touch_besttwoavg( data[1], data[3], data[5] );
|
||||
|
||||
//Serial.printf(" %d,%d", x, y);
|
||||
//Serial.println();
|
||||
if (z >= Z_THRESHOLD) {
|
||||
msraw = now; // good read completed, set wait
|
||||
switch (rotation) {
|
||||
case 0:
|
||||
xraw = 4095 - y;
|
||||
yraw = x;
|
||||
break;
|
||||
case 1:
|
||||
xraw = x;
|
||||
yraw = y;
|
||||
break;
|
||||
case 2:
|
||||
xraw = y;
|
||||
yraw = 4095 - x;
|
||||
break;
|
||||
default: // 3
|
||||
xraw = 4095 - x;
|
||||
yraw = 4095 - y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void initTouch(){
|
||||
pinMode(CS_PIN, OUTPUT);
|
||||
digitalWrite(CS_PIN, HIGH);
|
||||
}
|
||||
|
||||
bool readTouch(Point *const touch_point_out){
|
||||
touch_update();
|
||||
if (zraw >= Z_THRESHOLD) {
|
||||
touch_point_out->x = xraw;
|
||||
touch_point_out->y = yraw;
|
||||
//Serial.print(ts_point.x); Serial.print(",");Serial.println(ts_point.y);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void scaleTouch(Point *const touch_point_in_out){
|
||||
touch_point_in_out->x = ((long)(touch_point_in_out->x - globalSettings.touchOffsetX) * 10L)/ (long)globalSettings.touchSlopeX;
|
||||
touch_point_in_out->y = ((long)(touch_point_in_out->y - globalSettings.touchOffsetY) * 10L)/ (long)globalSettings.touchSlopeY;
|
||||
|
||||
//Serial.print(p->x); Serial.print(",");Serial.println(p->y);
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#include "point.h"
|
||||
|
||||
void initTouch();
|
||||
|
||||
//Returns true if touched, false otherwise
|
||||
bool readTouch(Point *const touch_point_out);
|
||||
|
||||
//Applies the touch calibration the point passed in
|
||||
void scaleTouch(Point *const touch_point_in_out);
|
|
@ -0,0 +1,31 @@
|
|||
#include "ui_touch.h"
|
||||
|
||||
#include <Arduino.h>//delay
|
||||
|
||||
#include "button_timing.h"
|
||||
#include "touch.h"
|
||||
|
||||
ButtonPress_e checkTouch(Point *const touch_point_out){
|
||||
if (!readTouch(touch_point_out)){
|
||||
return ButtonPress_e::NotPressed;
|
||||
}
|
||||
delay(DEBOUNCE_DELAY_MS);
|
||||
if (!readTouch(touch_point_out)){//debounce
|
||||
return ButtonPress_e::NotPressed;
|
||||
}
|
||||
|
||||
uint16_t down_time = 0;
|
||||
while(readTouch(touch_point_out) && (down_time < LONG_PRESS_TIME_MS)){
|
||||
delay(LONG_PRESS_POLL_TIME_MS);
|
||||
down_time += LONG_PRESS_POLL_TIME_MS;
|
||||
}
|
||||
|
||||
scaleTouch(touch_point_out);
|
||||
|
||||
if(down_time < LONG_PRESS_TIME_MS){
|
||||
return ButtonPress_e::ShortPress;
|
||||
}
|
||||
else{
|
||||
return ButtonPress_e::LongPress;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "menu.h"
|
||||
#include "point.h"
|
||||
|
||||
ButtonPress_e checkTouch(Point *const touch_point_out);
|
Loading…
Reference in New Issue