Added button handler, program selects

This commit is contained in:
true 2023-11-05 11:56:41 -08:00
parent 7013cac8f1
commit aa7bcf6c03
3 changed files with 111 additions and 0 deletions

25
include/btn.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef __INC_BTN_H
#define __INC_BTN_H
#include <stdint.h>
#define SR_CLK 0
#define SR_DAT 1
#define SR_LAT 16
#define BTN_STICK_UP (1 << 14)
#define BTN_STICK_DN (1 << 11)
#define BTN_STICK_LF (1 << 12)
#define BTN_STICK_RT (1 << 15)
#define BTN_STICK_SEL (1 << 13)
#define BTN_READ0 17
#define BTN_READ1 18
uint16_t btn_read();
#endif

47
src/btn.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <Arduino.h>
#include "btn.h"
uint16_t btn_mask;
uint16_t btn_last;
uint16_t btn_flip;
void sr_send(uint16_t dat)
{
digitalWrite(SR_LAT, LOW);
uint16_t mask = 0x8000;
for (uint8_t i = 0; i < 16; i++) {
digitalWrite(SR_DAT, dat & mask ? HIGH : LOW);
digitalWrite(SR_CLK, HIGH);
mask >>= 1;
digitalWrite(SR_CLK, LOW);
}
digitalWrite(SR_LAT, HIGH);
__NOP(); __NOP();
digitalWrite(SR_LAT, LOW);
}
uint16_t btn_read()
{
btn_last = btn_mask;
// read joystick only for now
for (uint8_t i = 11; i < 16; i++) {
sr_send(1 << i);
btn_mask &= ~(1 << i);
if (!digitalRead(BTN_READ1)) {
btn_mask |= (1 << i);
}
}
btn_flip = btn_mask ^ btn_last;
return btn_flip & btn_mask;
}

View File

@ -9,6 +9,8 @@ extern "C" {
#include "sin1.h"
}
#include "btn.h"
const char name[] = "Your Name";
@ -64,6 +66,14 @@ void setup()
drot->begin();
drot->setFont(u8g2_font_cubic11_h_cjk); // u8g2_font_spleen16x32_mr
// buttons
pinMode(SR_CLK, OUTPUT);
pinMode(SR_DAT, OUTPUT);
pinMode(SR_LAT, OUTPUT);
pinMode(BTN_READ0, INPUT);
pinMode(BTN_READ1, INPUT);
}
int16_t render_drot_printchar(const char *str, uint8_t idx, uint8_t size, uint16_t color)
@ -399,4 +409,33 @@ void loop()
}
dout->flush();
// read buttons
uint16_t btn;
btn = btn_read();
switch (btn) {
case BTN_STICK_UP: {
bgprog++;
bgprog %= 7;
break;
}
case BTN_STICK_DN: {
if (!bgprog) {
bgprog = 6;
} else bgprog--;
break;
}
case BTN_STICK_RT: {
fgprog++;
fgprog %= 1;
break;
}
case BTN_STICK_LF: {
if (!fgprog) {
fgprog = 0;
} else fgprog--;
}
}
}