From f3920743aa2434c22a8d968ca333b0b1c802d1f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 4 Mar 2018 11:27:16 +0100 Subject: [PATCH] example with auto + cleaning --- .gitignore | 2 + ndir_leds.py => demo_ndir_leds.py | 0 ndir_test.py => demo_ndir_usart.py | 0 demo_neopixel.py | 63 +++++++++++----------- pymodoro.py => demo_pymodoro.py | 0 gex/PayloadParser.py | 2 +- gex/units/ADC.py | 15 +++--- test_adc.py | 28 ++++++++++ pcap.py => test_freq_cap.py | 0 owtest.py => test_onewire.py | 19 ++++++- pwmbeep.py => test_pwmdim_music.py | 0 pwmbeep2.py => test_pwmdim_sweep.py | 0 omicron_mb_test.py => test_sipo_omicron.py | 0 ttouch.py => test_touch.py | 0 14 files changed, 87 insertions(+), 42 deletions(-) rename ndir_leds.py => demo_ndir_leds.py (100%) rename ndir_test.py => demo_ndir_usart.py (100%) rename pymodoro.py => demo_pymodoro.py (100%) create mode 100644 test_adc.py rename pcap.py => test_freq_cap.py (100%) rename owtest.py => test_onewire.py (78%) rename pwmbeep.py => test_pwmdim_music.py (100%) rename pwmbeep2.py => test_pwmdim_sweep.py (100%) rename omicron_mb_test.py => test_sipo_omicron.py (100%) rename ttouch.py => test_touch.py (100%) diff --git a/.gitignore b/.gitignore index 48853df..3e75939 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ __javascript__/ *.py[cod] *$py.class + +*.wav .idea/ # C extensions diff --git a/ndir_leds.py b/demo_ndir_leds.py similarity index 100% rename from ndir_leds.py rename to demo_ndir_leds.py diff --git a/ndir_test.py b/demo_ndir_usart.py similarity index 100% rename from ndir_test.py rename to demo_ndir_usart.py diff --git a/demo_neopixel.py b/demo_neopixel.py index 338306c..a3b8a18 100644 --- a/demo_neopixel.py +++ b/demo_neopixel.py @@ -1,36 +1,35 @@ #!/bin/env python3 import gex -client = gex.Client(port='/dev/ttyACM0') - -# Neopixel strip -strip = gex.Neopixel(client, 'npx') -# Load RGB to the strip -strip.load([0xFF0000, 0x00FF00, 0x0000FF, 0xFF00FF]) - -# I2C bus -i2c = gex.I2C(client, 'i2c') -# Read device register -print(i2c.read_reg(address=0x76, reg=0xD0)) -# Write value to a register -i2c.write_reg(address=0x76, reg=0xF4, value=0xFA) - -# SPI -spi = gex.SPI(client, 'spi') -# Query slave 0 -print(spi.query(0, [0xAA, 0xBB, 0xCC, 0xDD], rlen=2, rskip=4)) -# Write slaves 0 and 2 -spi.multicast(0b101, [0xDE, 0xAD, 0xBE, 0xEF]) - -# USART -usart = gex.USART(client, 'serial') -# Handle received data -usart.listen(lambda x: print(x, end='', flush=True)) -# Write a string -usart.write("AHOJ\r\n") - -# Digital output (8 pins) -display = gex.DOut(client, 'display') -display.write(0b10110011) -display.toggle(0b00010010) +with gex.Client(gex.TrxRawUSB()) as client: + # Neopixel strip + strip = gex.Neopixel(client, 'npx') + # Load RGB to the strip + strip.load([0xFF0000, 0x00FF00, 0x0000FF, 0xFF00FF]) + # + # # I2C bus + # i2c = gex.I2C(client, 'i2c') + # # Read device register + # print(i2c.read_reg(address=0x76, reg=0xD0)) + # # Write value to a register + # i2c.write_reg(address=0x76, reg=0xF4, value=0xFA) + # + # # SPI + # spi = gex.SPI(client, 'spi') + # # Query slave 0 + # print(spi.query(0, [0xAA, 0xBB, 0xCC, 0xDD], rlen=2, rskip=4)) + # # Write slaves 0 and 2 + # spi.multicast(0b101, [0xDE, 0xAD, 0xBE, 0xEF]) + # + # # USART + # usart = gex.USART(client, 'serial') + # # Handle received data + # usart.listen(lambda x: print(x, end='', flush=True)) + # # Write a string + # usart.write("AHOJ\r\n") + # + # # Digital output (8 pins) + # display = gex.DOut(client, 'display') + # display.write(0b10110011) + # display.toggle(0b00010010) diff --git a/pymodoro.py b/demo_pymodoro.py similarity index 100% rename from pymodoro.py rename to demo_pymodoro.py diff --git a/gex/PayloadParser.py b/gex/PayloadParser.py index ed42c6c..f7e5ef6 100644 --- a/gex/PayloadParser.py +++ b/gex/PayloadParser.py @@ -23,7 +23,7 @@ class PayloadParser: def _slice(self, n:int) -> bytearray: """ Extract a slice and advance the read pointer for the next slice """ if self.ptr + n > len(self.buf): - raise Exception("Payload parser underrun") + raise Exception("Payload parser underrun - frame: %s" % str(self.buf)) slice = self.buf[self.ptr:self.ptr + n] self.ptr += n diff --git a/gex/units/ADC.py b/gex/units/ADC.py index 0f6ecdf..1fa9dac 100644 --- a/gex/units/ADC.py +++ b/gex/units/ADC.py @@ -361,15 +361,16 @@ class ADC(gex.Unit): def lst(frame): pp = gex.PayloadParser(frame.data) - index = pp.u8() - if index != self._bcap_next_id: - self._bcap_done = True - raise Exception("Lost capture data frame! Expected %d, got %d" % (self._bcap_next_id, index)) - #return TF.CLOSE XXX + if frame.type == EVT_CAPT_MORE or len(frame.data) != 0: + index = pp.u8() + if index != self._bcap_next_id: + self._bcap_done = True + raise Exception("Lost capture data frame! Expected %d, got %d" % (self._bcap_next_id, index)) + #return TF.CLOSE XXX - self._bcap_next_id = (self._bcap_next_id + 1) % 256 + self._bcap_next_id = (self._bcap_next_id + 1) % 256 - buffer.extend(pp.tail()) + buffer.extend(pp.tail()) if frame.type == EVT_CAPT_DONE: self._bcap_done = True diff --git a/test_adc.py b/test_adc.py new file mode 100644 index 0000000..f5dcf6d --- /dev/null +++ b/test_adc.py @@ -0,0 +1,28 @@ +#!/bin/env python3 +import time + +import gex +import numpy as np +from matplotlib import pyplot as plt + +from scipy.io import wavfile + +with gex.Client(gex.TrxRawUSB()) as client: + adc = gex.ADC(client, 'a') + + adc.set_active_channels([3]) + rate=44000 + fs = adc.set_sample_rate(rate) + + count = 44000*2 + data = np.add(adc.capture(count) / 4096, -0.5) + + if data is not None: + # wavfile.write('file.wav', rate, data) + # print("Ok") + + plt.plot(data, 'r-', lw=1) + plt.show() + else: + print("Nothing rx") + diff --git a/pcap.py b/test_freq_cap.py similarity index 100% rename from pcap.py rename to test_freq_cap.py diff --git a/owtest.py b/test_onewire.py similarity index 78% rename from owtest.py rename to test_onewire.py index 9bd503c..d0a051a 100644 --- a/owtest.py +++ b/test_onewire.py @@ -15,9 +15,24 @@ with gex.Client(gex.TrxRawUSB()) as client: pp = gex.PayloadParser(data) return pp.i16() * 0.0625 + def meas2(addr, addr2): + ow.write([0x44], addr=addr) + ow.write([0x44], addr=addr2) + ow.wait_ready() + + data = ow.query([0xBE], 9, addr=addr) + pp = gex.PayloadParser(data) + a = pp.i16() * 0.0625 + + data = ow.query([0xBE], 9, addr=addr2) + pp = gex.PayloadParser(data) + b = pp.i16() * 0.0625 + return a, b + while True: - a = meas(6558392391241695016) - b = meas(1802309978572980008) + (a, b) = meas2(6558392391241695016, 1802309978572980008) + # a = meas(6558392391241695016) + # b = meas(1802309978572980008) print("in: %.2f °C, out: %f °C" % (a, b)) diff --git a/pwmbeep.py b/test_pwmdim_music.py similarity index 100% rename from pwmbeep.py rename to test_pwmdim_music.py diff --git a/pwmbeep2.py b/test_pwmdim_sweep.py similarity index 100% rename from pwmbeep2.py rename to test_pwmdim_sweep.py diff --git a/omicron_mb_test.py b/test_sipo_omicron.py similarity index 100% rename from omicron_mb_test.py rename to test_sipo_omicron.py diff --git a/ttouch.py b/test_touch.py similarity index 100% rename from ttouch.py rename to test_touch.py