diff --git a/arduino/tunerlist_i2c_attiny85/tunerlist_i2c_attiny85.ino b/arduino/tunerlist_i2c_attiny85/tunerlist_i2c_attiny85.ino index 454e44e..24b37b6 100644 --- a/arduino/tunerlist_i2c_attiny85/tunerlist_i2c_attiny85.ino +++ b/arduino/tunerlist_i2c_attiny85/tunerlist_i2c_attiny85.ino @@ -5,15 +5,14 @@ SoftwareSerial serial(3, 4); -enum State -{ +enum State { IDLE, ACTIVE }; -byte HANDSHAKE[] = {0x01, 0x00}; -byte PONG[] = {0x01, 0x01}; -byte BUTTON[] = {0x04, 0x82, 0x91, 0x00, 0x00}; +byte HANDSHAKE[] = { 0x01, 0x00 }; +byte PONG[] = { 0x01, 0x01 }; +byte BUTTON[] = { 0x04, 0x82, 0x91, 0x00, 0x05 }; State currentState = IDLE; unsigned long lastChange = 0; @@ -24,70 +23,52 @@ bool processButton = false; byte readBuffer[16]; bool newBuffer = false; -void up() -{ +void up() { pinMode(MRQ_PIN, INPUT_PULLUP); } -void down() -{ +void down() { pinMode(MRQ_PIN, OUTPUT); digitalWrite(MRQ_PIN, LOW); } -void onRequest() -{ +void onRequest() { while (digitalRead(MRQ_PIN)) ; - if (!handshakeComplete) - { + if (!handshakeComplete) { Wire.write(HANDSHAKE, sizeof HANDSHAKE); - } - else - { - if (processButton) - { + } else { + if (processButton) { processButton = false; Wire.write(BUTTON, sizeof BUTTON); - } - else - { + } else { Wire.write(PONG, sizeof PONG); } } } -void onReceive(int bytes) -{ +void onReceive(int bytes) { byte first_byte = Wire.read(); byte second_byte = Wire.read(); - if (first_byte == 0x01 && second_byte == 0x11) - { - if (!handshakeComplete) - { + if (first_byte == 0x01 && second_byte == 0x11) { + if (!handshakeComplete) { handshakeComplete = true; } - } - else if (first_byte == 0x01 && second_byte == 0x10) - { + } else if (first_byte == 0x01 && second_byte == 0x10) { handshakeComplete = false; - } - else - { + } else { for (int i = 0; i < 16; i++) readBuffer[i] = 0x0; readBuffer[0] = first_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(); } newBuffer = true; } } -void setup() -{ +void setup() { serial.begin(4800); Wire.setClock(10000); @@ -98,20 +79,16 @@ void setup() pinMode(MRQ_PIN, INPUT_PULLUP); } -void loop() -{ - if (serial.available()) - { +void loop() { + if (serial.available() >= 2) { BUTTON[3] = serial.read(); BUTTON[4] = serial.read(); processButton = true; currentState = ACTIVE; } - if (newBuffer) - { - for (int i = 0; i < 16; i++) - { + if (newBuffer) { + for (int i = 0; i < 16; i++) { serial.write(readBuffer[i]); } newBuffer = false; @@ -119,26 +96,22 @@ void loop() } unsigned long now = millis(); - switch (currentState) - { - case IDLE: - up(); - if (now - lastChange >= 480) - { - if (digitalRead(MRQ_PIN)) - { - lastChange = now; - currentState = ACTIVE; + switch (currentState) { + case IDLE: + up(); + if (now - lastChange >= 480) { + if (digitalRead(MRQ_PIN)) { + lastChange = now; + currentState = ACTIVE; + } } - } - break; - case ACTIVE: - down(); - if (now - lastChange >= 30) - { - lastChange = now; - currentState = IDLE; - } - break; + break; + case ACTIVE: + down(); + if (now - lastChange >= 30) { + lastChange = now; + currentState = IDLE; + } + break; } } \ No newline at end of file diff --git a/tunerlistd/test.py b/tunerlistd/test.py index 5f15e8a..2ea7432 100644 --- a/tunerlistd/test.py +++ b/tunerlistd/test.py @@ -5,6 +5,7 @@ import time pi = pigpio.pi() if not pi.connected: + print("not connected") exit(0) GPIO_PIN = 27 # GPIO pin 27 @@ -25,16 +26,17 @@ try: (count, data) = pi.bb_serial_read(GPIO_PIN) if count > 0: buffer += data - while b'\r\n' in buffer: line, buffer = buffer.split(b'\r\n', 1) # Split at newline # Print each byte in hex, separated by spaces - if not line == last_buffer: + if not line is last_buffer: last_buffer = line for c in line: - print(hex(c), " ", end="") + try: + print(c.decode("ascii"), " ", end="") + except: + print(hex(c), " ", end="") print() - time.sleep(0.01) except KeyboardInterrupt: pass diff --git a/tunerlistd/tunerlist.py b/tunerlistd/tunerlist.py index f0aedb6..bb4a37b 100644 --- a/tunerlistd/tunerlist.py +++ b/tunerlistd/tunerlist.py @@ -35,7 +35,7 @@ class TunerListState: @property 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): return { @@ -70,26 +70,27 @@ class TunerList: self.preset = None def _process_line(self, line): + text = "" try: if line[0] == 0x0F and line[1] == 0xFF: pass 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: self.preset = line[6] & 0x0F else: self.preset = None 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): self.preset = line[3] & 0x0F else: self.preset = None - elif line[0] == 0x39: - self.preset = None - self.text = "" else: pass + text = text.strip() + if text: + self.text = text except Exception as e: print(e) finally: @@ -97,8 +98,9 @@ class TunerList: self.queue.put_nowait(TunerListState(self.text, self.preset)) async def send_button(self, btn): + data = bytearray(BUTTONS[btn]) 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() self.pi.wave_send_once(wave) while self.pi.wave_tx_busy(): @@ -120,7 +122,7 @@ class TunerList: print(hex(c), " ", end="") print() self._process_line(line) - await sleep(0) + await sleep(0.1) except CancelledError: self.pi.bb_serial_read_close(RX_PIN) self.pi.stop()