Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions esp32/mods/esp_espnow.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ STATIC void send_queue_handler(void *arg) {
}
}

STATIC void IRAM_ATTR send_cb(const uint8_t *macaddr, esp_now_send_status_t status)
{
STATIC void IRAM_ATTR send_cb(const uint8_t *macaddr, esp_now_send_status_t status){
if (send_cb_obj != mp_const_none) {
mp_obj_tuple_t *msg = mp_obj_new_tuple(2, NULL);
msg->items[0] = mp_obj_new_bytes(macaddr, ESP_NOW_ETH_ALEN);
Expand All @@ -119,8 +118,7 @@ STATIC void recv_queue_handler(void *arg) {
}
}

STATIC void IRAM_ATTR recv_cb(const uint8_t *macaddr, const uint8_t *data, int len)
{
STATIC void IRAM_ATTR recv_cb(const uint8_t *macaddr, const uint8_t *data, int len) {
if (recv_cb_obj != mp_const_none) {
mp_obj_tuple_t *msg = mp_obj_new_tuple(2, NULL);
msg->items[0] = mp_obj_new_bytes(macaddr, ESP_NOW_ETH_ALEN);
Expand All @@ -129,8 +127,15 @@ STATIC void IRAM_ATTR recv_cb(const uint8_t *macaddr, const uint8_t *data, int l
}
}

// A static pointer to the espnow singleton
STATIC mp_obj_espnow_t *espnow_singleton = NULL;

STATIC mp_obj_t espnow_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {


if (espnow_singleton != NULL) {
return espnow_singleton;
}

mp_obj_espnow_t *self = m_new_obj(mp_obj_espnow_t);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better if this class would be "singleton", like e.g. CoAp module. As I understand instantiating again this class would result that esp_now is re-initialized which may ruin the already ongoing former instances.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - I agree - perhaps like this??

// A static pointer to the espnow singleton
STATIC mp_obj_espnow_t *espnow_singleton = NULL;

STATIC mp_obj_t espnow_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {

    if (espnow_singleton != NULL) {
        return espnow_singleton;
    } 

    mp_obj_espnow_t *self = m_new_obj(mp_obj_espnow_t);
    self->base.type = type;
    self->number = 0;
    
    ESPNOW_EXCEPTIONS(esp_now_init());  
    ESPNOW_EXCEPTIONS(esp_now_register_recv_cb(recv_cb));
    ESPNOW_EXCEPTIONS(esp_now_register_send_cb(send_cb));

    espnow_singleton = self;

    return MP_OBJ_FROM_PTR(self);
}

l

self->base.type = type;
self->number = 0;
Expand All @@ -139,6 +144,8 @@ STATIC mp_obj_t espnow_make_new(const mp_obj_type_t *type, size_t n_args, size_t
ESPNOW_EXCEPTIONS(esp_now_register_recv_cb(recv_cb));
ESPNOW_EXCEPTIONS(esp_now_register_send_cb(send_cb));

espnow_singleton = self;

return MP_OBJ_FROM_PTR(self);
}

Expand Down