From 58212214c85773062659c2bdd04357b15d48cab3 Mon Sep 17 00:00:00 2001 From: ens4dz Date: Mon, 14 Sep 2020 16:00:53 +0200 Subject: [PATCH 1/4] Update main.c --- ports/gprs_a9/main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/gprs_a9/main.c b/ports/gprs_a9/main.c index bec8b8b79eb5..58d67a7522e5 100644 --- a/ports/gprs_a9/main.c +++ b/ports/gprs_a9/main.c @@ -354,6 +354,10 @@ void EventDispatch(API_Event_t* pEvent) case API_EVENT_ID_CALL_HANGUP: modcellular_notify_call_hangup(pEvent); break; + + case API_EVENT_ID_CALL_DTMF: + modcellular_notify_dtmf_incoming(pEvent); + break; // USSD // ==== From e0f9c9046ada223f52ec0245b9d5bf8ca6acfdc8 Mon Sep 17 00:00:00 2001 From: ens4dz Date: Mon, 14 Sep 2020 16:06:06 +0200 Subject: [PATCH 2/4] Update modcellular.c --- ports/gprs_a9/modcellular.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ports/gprs_a9/modcellular.c b/ports/gprs_a9/modcellular.c index ec99f2d1889b..63e7da115113 100644 --- a/ports/gprs_a9/modcellular.c +++ b/ports/gprs_a9/modcellular.c @@ -140,6 +140,7 @@ STATIC mp_obj_t modcellular_sms_from_raw(uint8_t* header, uint32_t header_length // Incoming call mp_obj_t call_callback = mp_const_none; +mp_obj_t dtmf_callback = mp_const_none; // ---- // Init @@ -150,6 +151,7 @@ void modcellular_init0(void) { network_status_callback = mp_const_none; sms_callback = mp_const_none; call_callback = mp_const_none; + dtmf_callback = mp_const_none; ussd_callback = mp_const_none; // Reset statuses @@ -366,6 +368,14 @@ void modcellular_notify_call_hangup(API_Event_t* event) { mp_sched_schedule(call_callback, mp_obj_new_bool(event->param1)); } +void modcellular_notify_dtmf_incoming(API_Event_t* event) { + if (dtmf_callback && dtmf_callback != mp_const_none){ + char dtmf[1]; + dtmf[0] = event->param1 ; + mp_sched_schedule(dtmf_callback, mp_obj_new_str( dtmf,1) ); + } +} + // USSD mp_obj_t decode_ussd(API_Event_t* event) { @@ -1287,6 +1297,21 @@ STATIC mp_obj_t modcellular_on_call(mp_obj_t callable) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(modcellular_on_call_obj, modcellular_on_call); +STATIC mp_obj_t modcellular_on_dtmf(mp_obj_t callable) { + // ======================================== + // Sets a callback on incoming calls. + // Args: + // callback (Callable): a callback to + // execute on incoming dtmf. + // ======================================== + dtmf_callback = callable; + return mp_const_none; +} + +STATIC MP_DEFINE_CONST_FUN_OBJ_1(modcellular_on_dtmf_obj, modcellular_on_dtmf); + + + STATIC mp_obj_t modcellular_on_ussd(mp_obj_t callable) { // ======================================== // Sets a callback on USSD. @@ -1320,6 +1345,7 @@ STATIC const mp_map_elem_t mp_module_cellular_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&modcellular_scan_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_register), (mp_obj_t)&modcellular_register_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_dial), (mp_obj_t)&modcellular_dial_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_on_dtmf), (mp_obj_t)&modcellular_on_dtmf_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_ussd), (mp_obj_t)&modcellular_ussd_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_stations), (mp_obj_t)&modcellular_stations_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_agps_station_data), (mp_obj_t)&modcellular_agps_station_data_obj }, From 9fd8f23be9da9445705f1fdac03807e05f1f40ef Mon Sep 17 00:00:00 2001 From: ens4dz Date: Mon, 14 Sep 2020 16:07:11 +0200 Subject: [PATCH 3/4] Update modcellular.h --- ports/gprs_a9/modcellular.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/gprs_a9/modcellular.h b/ports/gprs_a9/modcellular.h index 5e9c3f5e3aa1..4b64c133635e 100644 --- a/ports/gprs_a9/modcellular.h +++ b/ports/gprs_a9/modcellular.h @@ -62,6 +62,7 @@ void modcellular_notify_cell_info(API_Event_t* event); void modcellular_notify_call_incoming(API_Event_t* event); void modcellular_notify_call_hangup(API_Event_t* event); +void modcellular_notify_incoming_dtmf(API_Event_t* event); void modcellular_notify_ussd_sent(API_Event_t* event); void modcellular_notify_ussd_failed(API_Event_t* event); From 591f1d04607a4bd7f0dd142a63298d3fa6f2f4ac Mon Sep 17 00:00:00 2001 From: ens4dz Date: Mon, 14 Sep 2020 16:15:50 +0200 Subject: [PATCH 4/4] Create example_016_DTMF.py --- ports/gprs_a9/examples/example_016_DTMF.py | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 ports/gprs_a9/examples/example_016_DTMF.py diff --git a/ports/gprs_a9/examples/example_016_DTMF.py b/ports/gprs_a9/examples/example_016_DTMF.py new file mode 100644 index 000000000000..361bf7589a94 --- /dev/null +++ b/ports/gprs_a9/examples/example_016_DTMF.py @@ -0,0 +1,33 @@ +# Micropython a9g example +# Source: https://github.com/pulkin/micropython +# Demonstrates how to control device from DTMF, +# in call, press key "1" from your phone to turn ON builtin led, "0" to turn it OFF + +import cellular +import machine +import time + +led1 = machine.Pin(27, machine.Pin.OUT, 1) +led2 = machine.Pin(28, machine.Pin.OUT, 1) + +led_stat = 1 +while (not cellular.is_network_registered() ) : + print("waiting network to register..") + led1.value(led_stat) + led2.value(not led_stat) + led_stat = not led_stat + time.sleep(1) + +led1.value(0) +led2.value(0) + +cellular.dial('0xxxxxxxxx') + +def dtmf_handler(evt): + print(evt) + if evt == "1": + led1.value(1) + elif evt == "0": + led1.value(0) + +cellular.on_dtmf(dtmf_handler)