Skip to content

Commit

Permalink
Add receive_bytes_into function to radio module. Allows allocation-fr…
Browse files Browse the repository at this point in the history
…ee radio reception.
  • Loading branch information
markshannon authored and dpgeorge committed Nov 12, 2016
1 parent b35c045 commit 09b34f3
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 4 deletions.
7 changes: 7 additions & 0 deletions docs/radio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ Functions
Receive the next incoming message on the message queue. Returns ``None`` if
there are no pending messages. Messages are returned as bytes.

.. py:function:: receive_bytes_into(buffer)
Receive the next incoming message on the message queue. Copies the message
into ``buffer``, trimming the end of the message if necessary.
Returns ``None`` if there are no pending messages, otherwise it returns the length
of the message (which might be more than the length of the buffer).

.. py:function:: send(message)
Sends a message string. This is the equivalent of
Expand Down
1 change: 1 addition & 0 deletions inc/genhdr/qstrdefs.generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ QDEF(MP_QSTR_config, (const byte*)"\x4f\x06" "config")
QDEF(MP_QSTR_send_bytes, (const byte*)"\xbf\x0a" "send_bytes")
QDEF(MP_QSTR_receive_bytes, (const byte*)"\x88\x0d" "receive_bytes")
QDEF(MP_QSTR_receive, (const byte*)"\x4e\x07" "receive")
QDEF(MP_QSTR_receive_bytes_into, (const byte*)"\x6b\x12" "receive_bytes_into")
QDEF(MP_QSTR_length, (const byte*)"\x59\x06" "length")
QDEF(MP_QSTR_queue, (const byte*)"\x94\x05" "queue")
QDEF(MP_QSTR_channel, (const byte*)"\x26\x07" "channel")
Expand Down
1 change: 1 addition & 0 deletions inc/microbit/qstrdefsport.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ Q(send_bytes)
Q(receive_bytes)
Q(send)
Q(receive)
Q(receive_bytes_into)
Q(length)
Q(queue)
Q(channel)
Expand Down
21 changes: 17 additions & 4 deletions source/microbit/modradio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ void radio_send(const void *buf, size_t len, const void *buf2, size_t len2) {
NVIC_EnableIRQ(RADIO_IRQn);
}

static mp_obj_t radio_receive(bool typed_packet) {
static mp_obj_t radio_receive(bool typed_packet, mp_buffer_info_t *bufinfo) {
ensure_enabled();

// disable the radio irq while we receive the packet
Expand All @@ -253,7 +253,12 @@ static mp_obj_t radio_receive(bool typed_packet) {
size_t len = buf[0];
mp_obj_t ret;
if (!typed_packet) {
ret = mp_obj_new_bytes(buf + 1, len); // if it raises the radio irq remains disabled...
if (bufinfo == NULL) {
ret = mp_obj_new_bytes(buf + 1, len); // if it raises the radio irq remains disabled...
} else {
memmove(bufinfo->buf, buf+1, len < bufinfo->len ? len : bufinfo->len);
ret = MP_OBJ_NEW_SMALL_INT(len);
}
} else if (len >= 3 && buf[1] == 1 && buf[2] == 0 && buf[3] == 1) {
ret = mp_obj_new_str((char*)buf + 4, len - 3, false); // if it raises the radio irq remains disabled...
} else {
Expand Down Expand Up @@ -431,7 +436,7 @@ STATIC mp_obj_t mod_radio_send_bytes(mp_obj_t buf_in) {
MP_DEFINE_CONST_FUN_OBJ_1(mod_radio_send_bytes_obj, mod_radio_send_bytes);

STATIC mp_obj_t mod_radio_receive_bytes(void) {
return radio_receive(false);
return radio_receive(false, NULL);
}
MP_DEFINE_CONST_FUN_OBJ_0(mod_radio_receive_bytes_obj, mod_radio_receive_bytes);

Expand All @@ -444,10 +449,17 @@ STATIC mp_obj_t mod_radio_send(mp_obj_t buf_in) {
MP_DEFINE_CONST_FUN_OBJ_1(mod_radio_send_obj, mod_radio_send);

STATIC mp_obj_t mod_radio_receive(void) {
return radio_receive(true);
return radio_receive(true, NULL);
}
MP_DEFINE_CONST_FUN_OBJ_0(mod_radio_receive_obj, mod_radio_receive);

STATIC mp_obj_t mod_radio_receive_bytes_into(mp_obj_t buf_in) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);
return radio_receive(false, &bufinfo);
}
MP_DEFINE_CONST_FUN_OBJ_1(mod_radio_receive_bytes_into_obj, mod_radio_receive_bytes_into);

STATIC const mp_map_elem_t radio_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_radio) },
{ MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&mod_radio_reset_obj },
Expand All @@ -460,6 +472,7 @@ STATIC const mp_map_elem_t radio_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_receive_bytes), (mp_obj_t)&mod_radio_receive_bytes_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&mod_radio_send_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_receive), (mp_obj_t)&mod_radio_receive_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_receive_bytes_into), (mp_obj_t)&mod_radio_receive_bytes_into_obj },

{ MP_OBJ_NEW_QSTR(MP_QSTR_RATE_250KBIT), MP_OBJ_NEW_SMALL_INT(RADIO_MODE_MODE_Nrf_250Kbit) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_RATE_1MBIT), MP_OBJ_NEW_SMALL_INT(RADIO_MODE_MODE_Nrf_1Mbit) },
Expand Down
74 changes: 74 additions & 0 deletions tests/radio_audio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

import audio
import radio
from microbit import button_a, button_b, display, running_time, sleep
import os

def sample_generator(filename):
buf = audio.AudioFrame()
with open(filename, "rb") as file:
ln = -1
while ln:
ln = file.readinto(buf)
yield buf

# 1 second of 128Hz sawtooth wave.
def sawtooth_generator():
sawtooth = audio.AudioFrame()
for i in range(32):
sawtooth[i] = i*8+4
for i in range(256):
yield sawtooth


def send():
display.clear()
radio.on()
radio.config(channel=90, power=4)
if "sample.raw" in os.listdir():
gen = sample_generator("sample.raw")
else:
gen = sawtooth_generator()
start = running_time()
sent = 0
for f in gen:
# One frame every 4ms = 8kHz
while sent > ((running_time() - start) >> 2) + 3:
sleep(1)
radio.send_bytes(f)
sent += 1
print(sent)

def play():
display.clear()
radio.on()
radio.config(channel=90, queue=12)
count = -1
def gen():
recvd = audio.AudioFrame()
empty = audio.AudioFrame()
while True:
if radio.receive_bytes_into(recvd) == 32:
yield recvd
else:
yield empty
if button_a.is_pressed() and button_b.is_pressed():
return
audio.play(gen())

while True:
message = "Press button a to send 'sample.raw' or sawtooth wave. Press button b to play received waveform. Press both buttons to stop."
display.scroll(message, delay=100, wait=False)
message_end = running_time() + len(message)*600
if button_a.is_pressed() and button_b.is_pressed():
break
while True:
sleep(50)
if button_a.is_pressed():
send()
break
if button_b.is_pressed():
play()
break
if running_time() > message_end:
break
1 change: 1 addition & 0 deletions tests/sample.raw
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������~~}|}}~~~��������������������������������~|zz{|}~~����������������������������������~{xz||||}}~��~~���~��������������������zuuwxxz|}}~~��������������������}����~xqrtuwxz||}~�����~{}���}�������������~}����}topruvwy{{~~~}|}��~{zz���}|�������������z���wkhlpststv|�~zvuvzyrt{�������������������[X[Zbt�����|g[\[^o}������uaUVc������jlps����������xRSSTz����}]TVU_jw������lPRO\�����p]ci|�����������iHPIb�����b[RTW_Xk�����gQMJ^������w_fdr�������������WGLI}����slfaQRWY�����jYYT[u������[Xfv�����������z~[KfWy�����f\qSUxe�����oeu|Ua{����bWg}�������������L=TK�������`CH^i�����~}�iOOAf����b^toz������������;<LO��������Z=@Yl�������uYFcW^�����pabZs�����������l;;sns�����zFTAE{li�����qe|_\L?����uexmc���������Ĺb%HDJ�kx�ٺ�z]<ZH5e���ϯ��xNV4C��������]q�|�������՞85`A2Li�����NjPU==^b_�͹���c=[l`eu����~����������ÏHEjR1Fj}����Ǜt�~RIUdx�z���e{�_uulnik��i����������sWTZ9@X]\u�������xgonYXolr�qb��b~��l��t{ga��q�ƶ���}xnF;UE6[�{���������ccs`\tvw\^��R���f��q����������\Yte:Gsr^n��������}kfkuq`{�eo��r��~s��~������������wio_[bmea��|�������~|wvy}~oh�^}�yi��q���o��y������~�uu{sov�{x��}������~��������~~{zwxzzz}�������������~~~{yy{z{~~yx{|�|~}~��|}}|�|��|z~}y{{{}}���������~}}}~���~~~��}ywxz{z~�||{{�~������}~��������{xyy{{�������{y{|z}�|}}|}������{|{~{z�����~zxxxxyz~������zyy{}}}{}}~~~}}}}}}||~�����||{|||}||���������|{|{z{~~}~}|}}}~~�||}}~|}~}~~}}yzzy}|z}�����������}{}|}}}~~��}�}}~��|}~|{}}{{~}�����~�~|~�~~|~�~{}�~~�����~|}|}~}~}�~}~~~~}��������~~~�|~}}�}|�~{|}}}}}����~}}~}|~}}}~~����~|}~����������~~~}}�����������������������~~~�~��~�����}��������|~~~~}�~~��������~}~�~�����~~���������������������������~���������������������������������������������������~����~}�������~~~}~}}{xsn}��ÔIA^v���oed~����rhk|��������eBIu����}ohm}��wkht������}z}}{vz�����������|~������ury������������zh`dq��yw~�����tllr{���������wqqs{�����������}}�����������}~~��������������||������~wv����{y�������~��z{����������z|���������������}}������������xtw{������������}yxv}������|z~�������������~}||���~}�����zwz������~|}�����|wx}���|}����}}���~�������}}��|xx|����~~}����~����}~~��~}y{�����x{���}{z������{uy~~~��������|xw{���������~����������{|��}}~�������~�}}}����~|����~{}����}~������~xy���~|�����������~|{����������������������������}}~~}~~|~wwz{|~{xx{zz|{|~������������������������|n^RPVet���������qfdipw{}tnqz�����~}�����������������uR;7BVk���������ybUSYgsy�������rU<5@Tm��������������������i>58Hbu��������w]OMUdpsr}������k^_bZI=D`����������|w}�������qC356Hex����ľ�xQ:<HRau�������uaTNR^f_Ybw������~~�|���������vD/21Gp������ʧi<36CZl{����ū�\HGNSZglp�����tqsuuy|����������]5126]������ѹw>35=Viq����̬|VGGJPVb|�����xonlkhj|����������qF232Lt��������Q45ANWl����̬�dPNMDF_���ǦeKYghdab������������tR628W~�����î�V;:LYhz�������n`\XUXj�����hVahgjmo��������������lB1=Wt��������lRO[^\k���������ueXXj������dh}vhw�z�������������uA1DTc����Ǵ��j]egZYky��������}tf`juturox��������������������~Q>JSR\nw��ż����zl^QO^ov{��������wqqcB;]tp{������������������dZ^b]]dkz���������thbcimln}���������cMS[W]s{v��������������z_IJURO_ry��������whVT]_al{����������f?Ea[Ss���������������iNMOE@R`g}���������xe_ZNOXan����������gPW[MQlvq��������������gOLJ<9HQXo����������{oeVONR[jy��������uwucZge^k��������������~dXSE:?IM\x�����������ylb][]dhku������}��yuyunr|~~�����������}mbWMHEGP_oy�����������yrmjhiikouy|~|xx{{|}~|yz~}������������rjbYXXTU^iq{�����������{vrqroostsv{{z|zuwz{{~~~����������������{wvttsrstrtxy}���~||{zyxz||}��������|}}{xttvuw{}}��������������������}|{ywuttttsstuwz{}���������������{zzyxx{{{z{}~~���������������}~{yxwxxwvwz{{||}��������������������~}}|||~}~��������������������~~~}�~}zzzyyyzy{}�����������������~~||�}�������������������������}{zzyvtvxzzxy}}~~}~~~~~�~~��������~}~~|{|}~�������~|}}{wwwxyxxyz{}}|{|~|zxyyy{{~����������������||}�~~�~�~}}{z|}|}~�������~�}|~}~����������~��{{}zz{{|~~�~}�~�������������~}~|{~~||~~~~}}}|}~}~}~�������������������������~{|}||}|~|{}}|}|}~~}}�����~�xp~�}{����|}~|}zz��}zwx|}|}�������~~~��~}~~��}~����}{{|{zz|~}|{zyxxz|�����������������ufiqdYgx{~��������}qqplkqx|�������quuXG]oei��������������qH>[\:Ex����������uWP`^Wh���������}yjRIPRXan�������������������]]xb6Fqun��������y_hkZUfx����������s[HWW@Lx�~������������|���aSlqJ>f�~|�������|\]kf[b{���������yieeYT\lor�������������������[Goi28n~z���ƭ���hMZ]QTf���������n`ZPN`bc{�����������{�������rEdu<)i�r��������i?WdLOj���������}eYY[gwgg��s~��z���������������U:bb17r�����ǡ���R;P[V^u�������yshZPQfxr�������rlswpk}����������{���Z^tkTVs���������~aP_skd}�������ppvl]dw�~������vrnptnp~���������������qhihXPc|���������oZ^ecex�������}xshdgov�������xtkdcgoz����������}�������ug��EKxwn|�������b\pmddo�������}~ufez�v�������uoyvlr|�������w��wrrx�~z�����������tiqpddmz���������|qpsuux��������|zuqry}���������}yyzy{��������~|vrtwyy{���������{s�xXZluxx�������zsrql_e����������nktwv{��������}zurt{����������zvxyvvz�����������}|�����������~�|{}�~}��������~��~����~~}~�~}~��������~|{{|~�����������~}~~}~��~�����~~}}}~|~��������}}}}�����������~{{�������������~~����������~~~~����~�~}�~��}~~�����������������������~||}~�����������������������������~������������������������������~}~���������}~������������������������������������������������������������������~~������������������������������������������������~��~�����������������������~��������������~�����������������������������������������������������������������������������������~������������������������������������������~��������������������o��{~����~}��j��|}�uqzo�����wp{t�������uvyop���{��vv|�{{���~��}v��||��~}}���|��z|}~~z{���������~~~z��}����}�����|��{��������|��}����}���|~������~����~�����~�������������~~�~��������~���}���|��}~����������~}�~�������������������~������~���~�����|~����~}}�������������~���}����~��z�������y��}z��|~���}}��~|��~��~~��~�������~�������{�~~����}��~���{��~~����~�}x��}�����|�z~�������|��}���}�}��|������z}��y���~���|���~}�������~��|��|��~~����������}~���}�������z�}{���{���~���~��}���}��~��~~}��}����~|�|}��~{}}�}��������yw��yy}����������|��}z|���|���zz��~|��z���~���}}��}~��~|��~~~�}��������~y~�}}�������~}}~|z}~���������~}~}}~~}|}~������������~~|}~}}~||~����������~{{~~}~|�~����~}�~~��������~���~~�}~������~}�~�����}���~~�������~~�}����~�����~��~�~}~~������~�~~~�����~~~~�����}~�~~~~�����~�}}~���~��~}~���������~���������~����~~����~�����~�����~~��~����~~���������������������~~����~~����������~~~��������~����������~~�������~~������������������~~���������������������������������������������~�������������������������������������������������������������������������������������������������������������������������������������������������~��������������������~����������������~~~�����~��~�~�����������������~�������������~~����������~~~��~��~������~~�~~����~�������������������������~~����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

0 comments on commit 09b34f3

Please sign in to comment.