-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add receive_bytes_into function to radio module. Allows allocation-fr…
…ee radio reception.
- Loading branch information
1 parent
b35c045
commit 09b34f3
Showing
6 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}~���������~}~}}~~}|}~������������~~|}~}}~||~����������~{{~~}~|�~����~}�~~��������~���~~�}~������~}�~�����}���~~�������~~�}����~�����~��~�~}~~������~�~~~�����~~~~�����}~�~~~~�����~�}}~���~��~}~���������~���������~����~~����~�����~�����~~��~����~~���������������������~~����~~����������~~~��������~����������~~�������~~������������������~~���������������������������������������������~�������������������������������������������������������������������������������������������������������������������������������������������������~��������������������~����������������~~~�����~��~�~�����������������~�������������~~����������~~~��~��~������~~�~~����~�������������������������~~���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� |