Added 5 way keypad sensing

This commit is contained in:
rljonesau 2018-11-21 20:53:12 +11:00
parent 63800ec900
commit 831f2ab516
7 changed files with 245 additions and 7 deletions

View file

@ -79,6 +79,7 @@
#include "display.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include "keypad.h"
#define FAILEDSSID "BTCESP32"
@ -111,6 +112,7 @@ static HardwareSerial& BlueWireSerial(Serial1);
void initBlueWireSerial();
bool validateFrame(const CProtocol& frame, const char* name);
void doKeyPad();
// DS18B20 temperature sensor support
OneWire ds(DS18B20_Pin); // on pin 5 (a 4.7K resistor is necessary)
@ -136,6 +138,7 @@ const CProtocol* pRxFrame = NULL;
const CProtocol* pTxFrame = NULL;
unsigned long moderator;
int TestKeys = 0;
////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -196,6 +199,8 @@ void setup() {
// DO THIS BEFORE WE TRY AND SEND DEBUG INFO!
DebugPort.begin(115200);
initKeyPad();
// initialise DS18B20 temperature sensor(s)
TempSensor.begin();
TempSensor.setWaitForConversion(false);
@ -335,6 +340,7 @@ void loop()
}
}
doKeyPad();
Bluetooth.check(); // check for Bluetooth activity
@ -765,4 +771,54 @@ bool validateFrame(const CProtocol& frame, const char* name)
return false;
}
return true;
}
}
void doKeyPad()
{
static uint8_t lastKey = 0;
static unsigned long lastHoldTime = 0;
static unsigned long holdTimeout = 0;
uint8_t newKey = readKeys();
uint8_t Change = newKey ^ lastKey;
uint8_t Press = Change & newKey; // bits set upon intial press, ONLY
uint8_t Release = Change & ~newKey; // bits set upon intial release, ONLY
uint8_t Repeat = 0;
lastKey = newKey;
if(Press) {
Serial.println("PRESS");
lastHoldTime = millis();
holdTimeout = 350; // initial hold delay
}
if(Release) {
Serial.println("RELEASE");
holdTimeout = 0; // cancel repeat
}
if(holdTimeout && ((millis() - lastHoldTime) > holdTimeout)) {
lastHoldTime += holdTimeout;
Serial.println("REPEAT");
holdTimeout = 150; // repeat delay
Repeat = newKey;
}
if((Press | Repeat) & keyPress_Left) {
TestKeys--;
}
if((Press | Repeat) & keyPress_Right) {
TestKeys++;
}
if((Press | Repeat) & keyPress_Up) {
TestKeys += 10;
}
if((Press | Repeat) & keyPress_Down) {
TestKeys -= 10;
}
if((Press | Repeat) & keyPress_Centre) {
TestKeys = 0;
}
}

View file

@ -0,0 +1,99 @@
/*
* DrawFont.cpp
*
* Created on: Aug 6, 2014
* Author: ray
*/
#include "CustomFont.h"
#include "DebugPort.h"
#define DBG DebugPort.print
#define DBGln DebugPort.println
//#define DEBUG_FONT
CCustomFont::CCustomFont(int8_t DC, int8_t CS, int8_t RST) : Adafruit_SH1106(DC, CS, RST)
{
m_pFontInfo = NULL;
}
size_t CCustomFont::write(uint8_t c)
{
if(m_pFontInfo) {
if (c == '\n') {
cursor_y += textsize*8;
cursor_x = 0;
} else if (c == '\r') {
// skip em
} else {
int xsize, ysize;
drawDotFactoryChar(cursor_x, cursor_y, c, textcolor, textbgcolor, m_pFontInfo, xsize, ysize);
cursor_x += xsize + m_pFontInfo->SpaceWidth;
if (wrap && (cursor_x > (_width - 8))) {
cursor_y += ysize;
cursor_x = 0;
}
}
}
else {
Adafruit_SH1106::write(c);
}
#if ARDUINO >= 100
return 1;
#endif
}
void
CCustomFont::drawDotFactoryChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, const FONT_INFO* pFontDescriptor, int& xsize, int& ysize)
{
#ifdef DEBUG_FONT
char pr = c;
DBG(pr); DBG(F(" fg=")); DBG(color); DBG(F(" bg=")); DBGln(bg);
#endif
uint16_t char2print = c;
if(c >= pFontDescriptor->StartChar && c <= pFontDescriptor->EndChar) {
#ifdef DEBUG_FONT
char pr = c;
DBG(pr);
#endif
// point to info for selected character
const FONT_CHAR_INFO* pCharInfo = &pFontDescriptor->pCharInfo[c - pFontDescriptor->StartChar];
// and extract info from flash (program) storage
int BmpOffset = pgm_read_byte(&pCharInfo->Offset);
xsize = pgm_read_byte(&pCharInfo->Width);
ysize = pgm_read_byte(&pCharInfo->Height);
// point to bitmap data for selected character
const uint8_t* pBitmap = &pFontDescriptor->pBitmaps[BmpOffset];
#ifdef DEBUG_FONT
DBG(F(" [")); DBG(int(pCharInfo)); DBG(']');
DBG(F(" (")); DBG(xsize); DBG(','); DBG(ysize); DBGln(')');
delay(1000);
#endif
uint8_t mask = 0x80;
uint8_t line = 0;
for(int8_t j=0; j < xsize/*pCharInfo->Width*/; j++) {
for (int8_t i=0; i < ysize/*pCharInfo->Height*/; i++ ) {
if((i & 0x07) == 0) {
line = pgm_read_byte(pBitmap++);
}
if(line & mask) {
drawPixel(x+j, y+i, color);
}
else if(bg != color) {
drawPixel(x+j, y+i, bg);
}
line <<= 1;
}
}
}
}

View file

@ -0,0 +1,18 @@
#include "Adafruit_SH1106.h"
#include "FontTypes.h"
class CCustomFont : public Adafruit_SH1106 {
const FONT_INFO* m_pFontInfo;
public:
CCustomFont(int8_t DC, int8_t CS, int8_t RST);
void drawDotFactoryChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, const FONT_INFO* pFontDescriptor, int& xsize, int& ysize);
void setFontInfo(const FONT_INFO* pFontInfo) { m_pFontInfo = pFontInfo; };
void offsetCursor(int16_t x, int16_t y) {
cursor_x += x;
cursor_y += y;
};
size_t write(uint8_t c);
};

View file

@ -59,7 +59,7 @@ bool animatePump = false;
bool animateRPM = false;
bool animateGlow = false;
extern int TestKeys;
extern float fFilteredTemperature;
extern CBluetoothAbstract& getBluetoothClient();
@ -143,6 +143,9 @@ void updateOLED(const CProtocol& CtlFrame, const CProtocol& HtrFrame)
showRunState(runstate, errstate);
display.setCursor(20,45);
display.print(TestKeys);
#ifdef DEMO_LARGEFONT
display.fillRect(20,20, 80,16, BLACK);
display.setCursor(20,20);

View file

@ -0,0 +1,47 @@
#include <Arduino.h>
#include "keypad.h"
#include "pins.h"
const unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void initKeyPad()
{
pinMode(keyLeft_pin, INPUT);
pinMode(keyRight_pin, INPUT);
pinMode(keyCentre_pin, INPUT);
pinMode(keyUp_pin, INPUT);
pinMode(keyDown_pin, INPUT);
}
uint8_t readKeys()
{
static uint8_t debouncedKey = 0;
static unsigned long lastDebounceTime = 0;
uint8_t newKey = 0;
if(digitalRead(keyLeft_pin) == LOW) newKey |= keyPress_Left;
if(digitalRead(keyRight_pin) == LOW) newKey |= keyPress_Right;
if(digitalRead(keyCentre_pin) == LOW) newKey |= keyPress_Centre;
if(digitalRead(keyUp_pin) == LOW) newKey |= keyPress_Up;
if(digitalRead(keyDown_pin) == LOW) newKey |= keyPress_Down;
static uint8_t prevKey = 0;
if(newKey != prevKey) {
lastDebounceTime = millis();
prevKey = newKey;
}
unsigned long elapsed = millis() - lastDebounceTime;
if (elapsed > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// Serial.println("debounce");
// if the button state has changed:
if (newKey != debouncedKey) {
debouncedKey = newKey;
}
}
return debouncedKey;
}

View file

@ -0,0 +1,15 @@
#ifndef __BTC_KEYPAD_H__
#define __BTC_KEYPAD_H__
#include "stdint.h"
void initKeyPad();
uint8_t readKeys();
const uint8_t keyPress_Left = 0x01;
const uint8_t keyPress_Right = 0x02;
const uint8_t keyPress_Centre = 0x04;
const uint8_t keyPress_Up = 0x08;
const uint8_t keyPress_Down = 0x10;
#endif

View file

@ -17,11 +17,11 @@ const uint8_t HC05_SensePin = 23;
const uint8_t OLED_DC_pin = 26;
const uint8_t OLED_CS_pin = 27;
// uncommitted
const uint8_t pin32 = 32; // GPIO
const uint8_t pin33 = 33; // GPIO
const uint8_t pin34 = 34; // input only, no chip pullup
const uint8_t pin35 = 35; // input only, no chip pullup
const uint8_t keyLeft_pin = 25;
const uint8_t keyRight_pin = 33;
const uint8_t keyCentre_pin = 32;
const uint8_t keyUp_pin = 35;
const uint8_t keyDown_pin = 34;
const uint8_t ListenOnlyPin = 36; // input only, no chip pullup
const uint8_t WiFi_TriggerPin = 39; // input only, no chip pullup