Browse Source

support for DOut pulse gen

Ondřej Hruška 1 year ago
parent
commit
92d3724dcd
Signed by: Ondřej Hruška <ondra@ondrovo.com> GPG key ID: 2C5FD5035250423D
5 changed files with 67 additions and 9 deletions
  1. 15 0
      demo_neo2.py
  2. 9 0
      demo_pulse.py
  3. 2 1
      gex/TinyFrame.py
  4. 33 4
      gex/units/DOut.py
  5. 8 4
      test_adc.py

+ 15 - 0
demo_neo2.py View File

@@ -0,0 +1,15 @@
1
+#!/bin/env python3
2
+import gex
3
+import time
4
+
5
+with gex.Client(gex.TrxRawUSB()) as client:
6
+    # Neopixel strip
7
+    strip = gex.Neopixel(client, 'neo')
8
+    # Load RGB to the strip
9
+    strip.load([0xFF0000, 0xFFFF00, 0x00FF00, 0x0000FF, 0xFF00FF])
10
+    
11
+    for i in range(0,255):
12
+        strip.load([0xFF0000+i, 0xFFFF00, 0x00FF00, 0x0000FF, 0xFF00FF])
13
+        time.sleep(0.001)
14
+        
15
+    strip.clear()

+ 9 - 0
demo_pulse.py View File

@@ -0,0 +1,9 @@
1
+#!/bin/env python3
2
+import gex
3
+import time
4
+
5
+with gex.Client(gex.TrxRawUSB()) as client:
6
+    out = gex.DOut(client, 'out')
7
+
8
+    out.pulse_us([0], 20)
9
+    out.pulse_us([3], 10)

+ 2 - 1
gex/TinyFrame.py View File

@@ -301,7 +301,8 @@ class TinyFrame:
301 301
                 actual = self._cksum(self.rpayload)
302 302
 
303 303
                 if pck != actual:
304
-                    print("[TF] Payload checksum mismatch")
304
+                    print("[TF] Payload checksum mismatch (given %x, computed %x)" % (pck, actual))
305
+                    print(self.rpayload)
305 306
                     self.reset_parser()
306 307
                 else:
307 308
                     self.handle_rx_frame()

+ 33 - 4
gex/units/DOut.py View File

@@ -1,5 +1,11 @@
1 1
 import gex
2 2
 
3
+CMD_WRITE = 0
4
+CMD_SET = 1
5
+CMD_CLEAR = 2
6
+CMD_TOGGLE = 3
7
+CMD_PULSE = 4
8
+
3 9
 class DOut(gex.Unit):
4 10
     """
5 11
     Digital output port.
@@ -17,22 +23,45 @@ class DOut(gex.Unit):
17 23
         """ Set pins to a value - packed, as int """
18 24
         pb = gex.PayloadBuilder()
19 25
         pb.u16(pins)
20
-        self._send(0x00, pb.close(), confirm=confirm)
26
+        self._send(CMD_WRITE, pb.close(), confirm=confirm)
21 27
 
22 28
     def set(self, pins, confirm=True):
23 29
         """ Set pins high - packed, int or list """
24 30
         pb = gex.PayloadBuilder()
25 31
         pb.u16(self.pins2int(pins))
26
-        self._send(0x01, pb.close(), confirm=confirm)
32
+        self._send(CMD_SET, pb.close(), confirm=confirm)
27 33
 
28 34
     def clear(self, pins, confirm=True):
29 35
         """ Set pins low - packed, int or list """
30 36
         pb = gex.PayloadBuilder()
31 37
         pb.u16(self.pins2int(pins))
32
-        self._send(0x02, pb.close(), confirm=confirm)
38
+        self._send(CMD_CLEAR, pb.close(), confirm=confirm)
33 39
 
34 40
     def toggle(self, pins, confirm=True):
35 41
         """ Toggle pins - packed, int or list """
36 42
         pb = gex.PayloadBuilder()
37 43
         pb.u16(self.pins2int(pins))
38
-        self._send(0x03, pb.close(), confirm=confirm)
44
+        self._send(CMD_TOGGLE, pb.close(), confirm=confirm)
45
+
46
+    def pulse_ms(self, pins, length, active=True, confirm=True):
47
+        """ Send a pulse with length 1-65535 ms on selected pins """
48
+        pb = gex.PayloadBuilder()
49
+        pb.u16(self.pins2int(pins))
50
+        pb.bool(active)
51
+        pb.bool(False)
52
+        pb.u16(length)
53
+        self._send(CMD_PULSE, pb.close(), confirm=confirm)
54
+
55
+    def pulse_us(self, pins, length, active=True, confirm=True):
56
+        """ Send a pulse of 1-999 us on selected pins """
57
+        pb = gex.PayloadBuilder()
58
+        pb.u16(self.pins2int(pins))
59
+        pb.bool(active)
60
+        pb.bool(True)
61
+        pb.u16(length)
62
+        self._send(CMD_PULSE, pb.close(), confirm=confirm)
63
+
64
+
65
+
66
+
67
+

+ 8 - 4
test_adc.py View File

@@ -11,17 +11,21 @@ with gex.Client(gex.TrxRawUSB()) as client:
11 11
     adc = gex.ADC(client, 'a')
12 12
 
13 13
     adc.set_active_channels([3])
14
-    rate=44000
14
+    rate=500
15 15
     fs = adc.set_sample_rate(rate)
16 16
 
17
-    count = 44000*2
18
-    data = np.add(adc.capture(count) / 4096, -0.5)
17
+    count = 2000
18
+    data = adc.capture(count)
19 19
 
20
+    print("rx, %d samples" % len(data))
21
+    data = np.add(data / 4096, -0.5)
22
+
23
+    #
20 24
     if data is not None:
21 25
         # wavfile.write('file.wav', rate, data)
22 26
         # print("Ok")
23 27
 
24
-        plt.plot(data, 'r-', lw=1)
28
+        plt.plot(data, 'r.', lw=1)
25 29
         plt.show()
26 30
     else:
27 31
         print("Nothing rx")