Просмотр исходного кода

add proper IR rx function and example

Oliver Fabel 1 год назад
Родитель
Сommit
717070e798

+ 31 - 4
examples/flipperzero_infrared_test.py

@@ -1,14 +1,41 @@
 import flipperzero as f0
 import time
 
-signal = f0.infrared_receive()
+raw_signal = f0.infrared_receive()
 
-durations = map(lambda v:str(v), signal)
-value = ','.join(durations)
+signal = map(lambda v: int(v / 100), raw_signal)
+
+level = False
+
+i = -1
+x = 0
+y_low = 32
+y_high = 40
+y_level = y_low
 
 f0.canvas_clear()
 
-f0.canvas_set_text(10, 32, value)
+for duration in signal:
+    i += 1
+
+    if i < 10:
+        continue
+
+    if level:
+        f0.canvas_draw_line(x, y_low, x, y_high)
+        y_level = y_high
+    else:
+        f0.canvas_draw_line(x, y_high, x, y_low)
+        y_level = y_low
+
+    f0.canvas_draw_line(x, y_level, x + duration, y_level)
+
+    x += duration
+
+    level = not level
+
+    if x > f0.canvas_width():
+        break
 
 f0.canvas_update()
 

+ 1 - 1
lib/micropython

@@ -1 +1 @@
-Subproject commit 1868de0b8710f10574a17e0f8e517105f608fd63
+Subproject commit 7a3539090c46072f59c0323b4efc7dbad933ee4c

+ 10 - 0
lib/micropython-port/mp_flipper_context.h

@@ -12,6 +12,15 @@
 
 typedef uint16_t mp_flipper_gpio_pin_t;
 
+#define MP_FLIPPER_INFRARED_RX_BUFFER_SIZE (1024)
+
+typedef struct {
+    uint16_t size;
+    uint32_t* buffer;
+    uint16_t pointer;
+    bool running;
+} mp_flipper_infrared_rx_t;
+
 typedef struct {
     Gui* gui;
     ViewPort* view_port;
@@ -24,4 +33,5 @@ typedef struct {
     const char* dialog_message_button_right;
     FuriHalAdcHandle* adc_handle;
     mp_flipper_gpio_pin_t* gpio_pins;
+    mp_flipper_infrared_rx_t* infrared_rx;
 } mp_flipper_context_t;

+ 12 - 17
lib/micropython-port/mp_flipper_modflipperzero_infrared.c

@@ -6,16 +6,14 @@
 
 #include "mp_flipper_context.h"
 
-typedef struct {
-    uint32_t* buffer;
-    uint16_t pointer;
-    bool running;
-} mp_flipper_infrared_rx_session_t;
-
 static void infrared_receive_callback(void* ctx, bool level, uint32_t duration) {
-    mp_flipper_infrared_rx_session_t* session = ctx;
+    mp_flipper_infrared_rx_t* session = ctx;
+
+    if(session->pointer == 0 && !level) {
+        return;
+    }
 
-    if(session->pointer < MP_FLIPPER_INFRARED_RX_BUFFER_SIZE) {
+    if(session->pointer < session->size) {
         session->buffer[session->pointer] = duration;
 
         session->pointer++;
@@ -25,7 +23,7 @@ static void infrared_receive_callback(void* ctx, bool level, uint32_t duration)
 }
 
 static void infrared_timeout_callback(void* ctx) {
-    mp_flipper_infrared_rx_session_t* session = ctx;
+    mp_flipper_infrared_rx_t* session = ctx;
 
     session->running = false;
 
@@ -33,9 +31,10 @@ static void infrared_timeout_callback(void* ctx) {
 }
 
 inline uint32_t* mp_flipper_infrared_receive(uint32_t timeout, size_t* length) {
-    mp_flipper_infrared_rx_session_t* session = malloc(sizeof(mp_flipper_infrared_rx_session_t));
+    const mp_flipper_context_t* ctx = mp_flipper_context;
+
+    mp_flipper_infrared_rx_t* session = ctx->infrared_rx;
 
-    session->buffer = calloc(MP_FLIPPER_INFRARED_RX_BUFFER_SIZE, sizeof(uint32_t));
     session->pointer = 0;
     session->running = true;
 
@@ -47,14 +46,10 @@ inline uint32_t* mp_flipper_infrared_receive(uint32_t timeout, size_t* length) {
     furi_hal_infrared_async_rx_set_timeout(timeout);
 
     while(session->running) {
-        furi_delay_us(10);
+        furi_delay_tick(10);
     }
 
     *length = session->pointer;
 
-    void* pointer = session->buffer;
-
-    free(session);
-
-    return pointer;
+    return session->buffer;
 }

+ 13 - 0
lib/micropython-port/mp_flipper_runtime.c

@@ -86,12 +86,21 @@ void* mp_flipper_context_alloc() {
 
     ctx->adc_handle = NULL;
 
+    // GPIO
     ctx->gpio_pins = malloc(MP_FLIPPER_GPIO_PINS * sizeof(mp_flipper_gpio_pin_t));
 
     for(uint8_t pin = 0; pin < MP_FLIPPER_GPIO_PINS; pin++) {
         ctx->gpio_pins[pin] = MP_FLIPPER_GPIO_PIN_OFF;
     }
 
+    // infrared
+    ctx->infrared_rx = malloc(sizeof(mp_flipper_infrared_rx_t));
+
+    ctx->infrared_rx->size = MP_FLIPPER_INFRARED_RX_BUFFER_SIZE;
+    ctx->infrared_rx->buffer = calloc(ctx->infrared_rx->size, sizeof(uint16_t));
+    ctx->infrared_rx->pointer = 0;
+    ctx->infrared_rx->running = true;
+
     return ctx;
 }
 
@@ -127,5 +136,9 @@ void mp_flipper_context_free(void* context) {
 
     free(ctx->gpio_pins);
 
+    // stop infrared
+    free(ctx->infrared_rx->buffer);
+    free(ctx->infrared_rx);
+
     free(ctx);
 }