working button presses

This commit is contained in:
marios8543 2025-05-07 17:47:03 +03:00
parent e58eda3523
commit 7b6bfb9657
3 changed files with 54 additions and 77 deletions

View File

@ -5,15 +5,14 @@
SoftwareSerial serial(3, 4); SoftwareSerial serial(3, 4);
enum State enum State {
{
IDLE, IDLE,
ACTIVE ACTIVE
}; };
byte HANDSHAKE[] = {0x01, 0x00}; byte HANDSHAKE[] = { 0x01, 0x00 };
byte PONG[] = {0x01, 0x01}; byte PONG[] = { 0x01, 0x01 };
byte BUTTON[] = {0x04, 0x82, 0x91, 0x00, 0x00}; byte BUTTON[] = { 0x04, 0x82, 0x91, 0x00, 0x05 };
State currentState = IDLE; State currentState = IDLE;
unsigned long lastChange = 0; unsigned long lastChange = 0;
@ -24,70 +23,52 @@ bool processButton = false;
byte readBuffer[16]; byte readBuffer[16];
bool newBuffer = false; bool newBuffer = false;
void up() void up() {
{
pinMode(MRQ_PIN, INPUT_PULLUP); pinMode(MRQ_PIN, INPUT_PULLUP);
} }
void down() void down() {
{
pinMode(MRQ_PIN, OUTPUT); pinMode(MRQ_PIN, OUTPUT);
digitalWrite(MRQ_PIN, LOW); digitalWrite(MRQ_PIN, LOW);
} }
void onRequest() void onRequest() {
{
while (digitalRead(MRQ_PIN)) while (digitalRead(MRQ_PIN))
; ;
if (!handshakeComplete) if (!handshakeComplete) {
{
Wire.write(HANDSHAKE, sizeof HANDSHAKE); Wire.write(HANDSHAKE, sizeof HANDSHAKE);
} } else {
else if (processButton) {
{
if (processButton)
{
processButton = false; processButton = false;
Wire.write(BUTTON, sizeof BUTTON); Wire.write(BUTTON, sizeof BUTTON);
} } else {
else
{
Wire.write(PONG, sizeof PONG); Wire.write(PONG, sizeof PONG);
} }
} }
} }
void onReceive(int bytes) void onReceive(int bytes) {
{
byte first_byte = Wire.read(); byte first_byte = Wire.read();
byte second_byte = Wire.read(); byte second_byte = Wire.read();
if (first_byte == 0x01 && second_byte == 0x11) if (first_byte == 0x01 && second_byte == 0x11) {
{ if (!handshakeComplete) {
if (!handshakeComplete)
{
handshakeComplete = true; handshakeComplete = true;
} }
} } else if (first_byte == 0x01 && second_byte == 0x10) {
else if (first_byte == 0x01 && second_byte == 0x10)
{
handshakeComplete = false; handshakeComplete = false;
} } else {
else
{
for (int i = 0; i < 16; i++) for (int i = 0; i < 16; i++)
readBuffer[i] = 0x0; readBuffer[i] = 0x0;
readBuffer[0] = first_byte; readBuffer[0] = first_byte;
readBuffer[1] = second_byte; readBuffer[1] = second_byte;
for (int i = 0; i < bytes - 2; i++) for (int i = 0; i < bytes - 2; i++) {
{
readBuffer[i + 2] = Wire.read(); readBuffer[i + 2] = Wire.read();
} }
newBuffer = true; newBuffer = true;
} }
} }
void setup() void setup() {
{
serial.begin(4800); serial.begin(4800);
Wire.setClock(10000); Wire.setClock(10000);
@ -98,20 +79,16 @@ void setup()
pinMode(MRQ_PIN, INPUT_PULLUP); pinMode(MRQ_PIN, INPUT_PULLUP);
} }
void loop() void loop() {
{ if (serial.available() >= 2) {
if (serial.available())
{
BUTTON[3] = serial.read(); BUTTON[3] = serial.read();
BUTTON[4] = serial.read(); BUTTON[4] = serial.read();
processButton = true; processButton = true;
currentState = ACTIVE; currentState = ACTIVE;
} }
if (newBuffer) if (newBuffer) {
{ for (int i = 0; i < 16; i++) {
for (int i = 0; i < 16; i++)
{
serial.write(readBuffer[i]); serial.write(readBuffer[i]);
} }
newBuffer = false; newBuffer = false;
@ -119,26 +96,22 @@ void loop()
} }
unsigned long now = millis(); unsigned long now = millis();
switch (currentState) switch (currentState) {
{ case IDLE:
case IDLE: up();
up(); if (now - lastChange >= 480) {
if (now - lastChange >= 480) if (digitalRead(MRQ_PIN)) {
{ lastChange = now;
if (digitalRead(MRQ_PIN)) currentState = ACTIVE;
{ }
lastChange = now;
currentState = ACTIVE;
} }
} break;
break; case ACTIVE:
case ACTIVE: down();
down(); if (now - lastChange >= 30) {
if (now - lastChange >= 30) lastChange = now;
{ currentState = IDLE;
lastChange = now; }
currentState = IDLE; break;
}
break;
} }
} }

View File

@ -5,6 +5,7 @@ import time
pi = pigpio.pi() pi = pigpio.pi()
if not pi.connected: if not pi.connected:
print("not connected")
exit(0) exit(0)
GPIO_PIN = 27 # GPIO pin 27 GPIO_PIN = 27 # GPIO pin 27
@ -25,16 +26,17 @@ try:
(count, data) = pi.bb_serial_read(GPIO_PIN) (count, data) = pi.bb_serial_read(GPIO_PIN)
if count > 0: if count > 0:
buffer += data buffer += data
while b'\r\n' in buffer: while b'\r\n' in buffer:
line, buffer = buffer.split(b'\r\n', 1) # Split at newline line, buffer = buffer.split(b'\r\n', 1) # Split at newline
# Print each byte in hex, separated by spaces # Print each byte in hex, separated by spaces
if not line == last_buffer: if not line is last_buffer:
last_buffer = line last_buffer = line
for c in line: for c in line:
print(hex(c), " ", end="") try:
print(c.decode("ascii"), " ", end="")
except:
print(hex(c), " ", end="")
print() print()
time.sleep(0.01)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass

View File

@ -35,7 +35,7 @@ class TunerListState:
@property @property
def is_playing_ext(self): def is_playing_ext(self):
return self.text == "TR 1 CD " and self.preset is not None return "TR 1 C" in self.text and self.preset is not None
def to_dict(self): def to_dict(self):
return { return {
@ -70,26 +70,27 @@ class TunerList:
self.preset = None self.preset = None
def _process_line(self, line): def _process_line(self, line):
text = ""
try: try:
if line[0] == 0x0F and line[1] == 0xFF: if line[0] == 0x0F and line[1] == 0xFF:
pass pass
elif line[0] == 0x0F: elif line[0] == 0x0F:
self.text = line[8:int(line[0]) + 1].decode("ascii").replace("\n", "") text = line[8:].decode("ascii").replace("\n", "").replace("\x00", "")
if (line[6] & 0xF0) == 0x70: if (line[6] & 0xF0) == 0x70:
self.preset = line[6] & 0x0F self.preset = line[6] & 0x0F
else: else:
self.preset = None self.preset = None
elif line[0] == 0x0C: elif line[0] == 0x0C:
self.text = line[5:int(line[0]) + 1].decode("ascii").replace("\n", "") text = line[5:].decode("ascii").replace("\n", "").replace("\x00", "")
if (line[3] != 0x20): if (line[3] != 0x20):
self.preset = line[3] & 0x0F self.preset = line[3] & 0x0F
else: else:
self.preset = None self.preset = None
elif line[0] == 0x39:
self.preset = None
self.text = ""
else: else:
pass pass
text = text.strip()
if text:
self.text = text
except Exception as e: except Exception as e:
print(e) print(e)
finally: finally:
@ -97,8 +98,9 @@ class TunerList:
self.queue.put_nowait(TunerListState(self.text, self.preset)) self.queue.put_nowait(TunerListState(self.text, self.preset))
async def send_button(self, btn): async def send_button(self, btn):
data = bytearray(BUTTONS[btn])
self.pi.wave_clear() self.pi.wave_clear()
self.pi.wave_add_serial(TX_PIN, BAUD_RATE, bytearray(BUTTONS[btn])) self.pi.wave_add_serial(TX_PIN, BAUD_RATE, data)
wave = self.pi.wave_create() wave = self.pi.wave_create()
self.pi.wave_send_once(wave) self.pi.wave_send_once(wave)
while self.pi.wave_tx_busy(): while self.pi.wave_tx_busy():
@ -120,7 +122,7 @@ class TunerList:
print(hex(c), " ", end="") print(hex(c), " ", end="")
print() print()
self._process_line(line) self._process_line(line)
await sleep(0) await sleep(0.1)
except CancelledError: except CancelledError:
self.pi.bb_serial_read_close(RX_PIN) self.pi.bb_serial_read_close(RX_PIN)
self.pi.stop() self.pi.stop()