Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DTMF #86

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

DTMF #86

Show file tree
Hide file tree
Changes from all 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
33 changes: 33 additions & 0 deletions ports/gprs_a9/examples/example_016_DTMF.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions ports/gprs_a9/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ====
Expand Down
26 changes: 26 additions & 0 deletions ports/gprs_a9/modcellular.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 },
Expand Down
1 change: 1 addition & 0 deletions ports/gprs_a9/modcellular.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down