Browse Source

transmit 16bit values, add filtering and raw values

Thomas B 3 months ago
parent
commit
70e23b5890
5 changed files with 55 additions and 22 deletions
  1. 20
    6
      client/fetch.py
  2. 2
    1
      sensor/include/adc.h
  3. 1
    1
      sensor/reset.py
  4. 20
    6
      sensor/src/adc.c
  5. 12
    8
      sensor/src/usb.c

+ 20
- 6
client/fetch.py View File

@@ -7,6 +7,9 @@ import time
7 7
 CUSTOM_RQ_ECHO = 0 # send back wValue and wIndex, for testing comms reliability
8 8
 CUSTOM_RQ_RESET = 1 # reset to bootloader
9 9
 CUSTOM_RQ_GET = 2 # get ldr value
10
+CUSTOM_RQ_RAW = 3 # get ldr value
11
+
12
+max_comm_retries = 5;
10 13
 
11 14
 def is_target_device(dev):
12 15
     if dev.manufacturer == "xythobuz.de" and dev.product == "AutoBrightness":
@@ -23,16 +26,27 @@ if dev is None:
23 26
 # configure the device
24 27
 dev.set_configuration()
25 28
 
26
-def read_u32(req, a=0, b=0):
27
-    r = dev.ctrl_transfer(usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_IN, req, a, b, 4)
28
-    return int.from_bytes(r, "little")
29
+def read_val(req, w, a=0, b=0):
30
+    for attempts in range(0, max_comm_retries):
31
+        try:
32
+            r = dev.ctrl_transfer(usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_IN, req, a, b, w)
33
+            return int.from_bytes(r, "little")
34
+        except usb.core.USBError as e:
35
+            if attempts >= (max_comm_retries - 1):
36
+                raise e
29 37
 
30 38
 # check for proper connectivity
31
-if read_u32(CUSTOM_RQ_ECHO, 42, 23) != (42 | (23 << 16)):
39
+if read_val(CUSTOM_RQ_ECHO, 4, 42, 23) != (42 | (23 << 16)):
32 40
     raise ValueError("invalid echo response")
33 41
 
34 42
 # repeatedly print value
35 43
 while True:
36
-    r = read_u32(CUSTOM_RQ_GET)
37
-    print(time.time(), ";", r, flush=True)
44
+    v = read_val(CUSTOM_RQ_GET, 2)
45
+    print(time.time(), ";", v, flush=True)
46
+
47
+    #r = read_val(CUSTOM_RQ_RAW, 2)
48
+    #print(time.time(), ";", r, flush=True)
49
+
50
+    #print(time.time(), ";", v, ";", r, flush=True)
51
+
38 52
     time.sleep(0.25)

+ 2
- 1
sensor/include/adc.h View File

@@ -22,6 +22,7 @@
22 22
 #include <stdint.h>
23 23
 
24 24
 void adcInit(void);
25
-uint32_t adcGet(void);
25
+uint16_t adcGet(void);
26
+uint16_t adcRaw(void);
26 27
 
27 28
 #endif // __ADC_H__

+ 1
- 1
sensor/reset.py View File

@@ -31,7 +31,7 @@ r = dev.ctrl_transfer(usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_IN, CUSTOM_RQ_RE
31 31
 print("reset-err", r)
32 32
 
33 33
 # value
34
-r = dev.ctrl_transfer(usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_IN, CUSTOM_RQ_GET, 0, 0, 4)
34
+r = dev.ctrl_transfer(usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_IN, CUSTOM_RQ_GET, 0, 0, 2)
35 35
 print("value", r)
36 36
 
37 37
 # reset ok

+ 20
- 6
sensor/src/adc.c View File

@@ -22,17 +22,23 @@
22 22
 
23 23
 #include "adc.h"
24 24
 
25
-static uint32_t adc_value = 0;
25
+static int32_t adc_filter = 0;
26
+static uint16_t adc_prev = 0;
26 27
 
27 28
 void adcInit(void) {
28 29
     ADMUX = (1 << MUX0); // Vcc reference, ADC1 / PB2 in
29 30
     ADCSRA = (1 << ADEN) | (1 << ADIE) // adc and interrupt enabled
30
-            | (1 << ADPS2) | (1 << ADPS1) // prescaler 64
31
+            | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0) // prescaler 128
31 32
             | (1 << ADSC); // start first conversion
32 33
 }
33 34
 
34 35
 static inline void adcFilter(uint16_t val) {
35
-    adc_value = val; // TODO
36
+    adc_prev = val;
37
+
38
+    int32_t input = val;
39
+    input <<= 16; // 10bit val --> 26bit input
40
+
41
+    adc_filter = adc_filter + ((input - adc_filter) >> 5);
36 42
 }
37 43
 
38 44
 ISR(ADC_vect) {
@@ -43,10 +49,18 @@ ISR(ADC_vect) {
43 49
     ADCSRA |= (1 << ADSC);
44 50
 }
45 51
 
46
-uint32_t adcGet(void) {
47
-    uint32_t val;
52
+uint16_t adcGet(void) {
53
+    uint16_t val;
54
+    ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
55
+        val = (adc_filter >> 16) + ((adc_filter & 0x00008000) >> 15);
56
+    }
57
+    return val;
58
+}
59
+
60
+uint16_t adcRaw(void) {
61
+    uint16_t val;
48 62
     ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
49
-        val = adc_value;
63
+        val = adc_prev;
50 64
     }
51 65
     return val;
52 66
 }

+ 12
- 8
sensor/src/usb.c View File

@@ -26,7 +26,8 @@
26 26
 
27 27
 #define CUSTOM_RQ_ECHO 0 // send back wValue and wIndex, for testing comms reliability
28 28
 #define CUSTOM_RQ_RESET 1 // reset to bootloader
29
-#define CUSTOM_RQ_GET 2 // get ldr value
29
+#define CUSTOM_RQ_GET 2 // get ldr value, filtered
30
+#define CUSTOM_RQ_RAW 3 // get raw ldr value
30 31
 
31 32
 usbMsgLen_t usbFunctionSetup(uchar data[8]) {
32 33
     usbRequest_t *rq = (void *)data;
@@ -52,15 +53,18 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) {
52 53
             dataBuffer[0] = 0; // error code
53 54
         }
54 55
         return 1;
55
-    } else if (rq->bRequest == CUSTOM_RQ_GET) {
56
-        uint32_t ldr_value = adcGet();
57
-        dataBuffer[0] = (ldr_value & 0x000000FF) >> 0;
58
-        dataBuffer[1] = (ldr_value & 0x0000FF00) >> 8;
59
-        dataBuffer[2] = (ldr_value & 0x00FF0000) >> 16;
60
-        dataBuffer[3] = (ldr_value & 0xFF000000) >> 24;
56
+    } else if ((rq->bRequest == CUSTOM_RQ_GET) || (rq->bRequest == CUSTOM_RQ_RAW)) {
57
+        uint16_t ldr_value;
58
+        if (rq->bRequest == CUSTOM_RQ_GET) {
59
+            ldr_value = adcGet();
60
+        } else {
61
+            ldr_value = adcRaw();
62
+        }
63
+        dataBuffer[0] = (ldr_value & 0x00FF) >> 0;
64
+        dataBuffer[1] = (ldr_value & 0xFF00) >> 8;
61 65
 
62 66
         usbMsgPtr = dataBuffer; // tell the driver which data to return
63
-        return 4; // tell the driver to send 4 bytes
67
+        return 2; // tell the driver to send 2 bytes
64 68
     }
65 69
 
66 70
     // default for not implemented requests: return no data back to host

Loading…
Cancel
Save