Nano BLE Senseには、利用できる3つの異なるLEDがあります。RGBと内蔵LED、電源LEDです。
LEDモジュールをインポートすれば、RGBと内蔵LEDにアクセスできます。
1
2
3
4
5
6
fromboardimportLEDled_red=LED(1)# red LEDled_green=LED(2)# green LEDled_blue=LED(3)# blue LEDled_builtin=LED(4)# classic built-in LED (also accessible through pin 13)
importimage,audio,timefromulabimportnumpyasnpfromulabimportscipyasspCHANNELS=1SIZE=256//(2*CHANNELS)raw_buf=Nonefb=image.Image(SIZE+50,SIZE,image.RGB565,copy_to_fb=True)audio.init(channels=CHANNELS,frequency=16000,gain_db=80,highpass=0.9883)defaudio_callback(buf):# NOTE: do Not call any function that allocates memory.globalraw_bufif(raw_buf==None):raw_buf=buf# Start audio streamingaudio.start_streaming(audio_callback)defdraw_fft(img,fft_buf):fft_buf=(fft_buf/max(fft_buf))*SIZEfft_buf=np.log10(fft_buf+1)*20color=(0xFF,0x0F,0x00)foriinrange(0,SIZE):img.draw_line(i,SIZE,i,SIZE-int(fft_buf[i]),color,1)defdraw_audio_bar(img,level,offset):blk_size=SIZE//10color=(0xFF,0x00,0xF0)blk_space=(blk_size//4)foriinrange(0,int(round(level/10))):fb.draw_rectangle(SIZE+offset,SIZE-((i+1)*blk_size)+blk_space,20,blk_size-blk_space,color,1,True)while(True):if(raw_buf!=None):pcm_buf=np.frombuffer(raw_buf,dtype=np.int16)raw_buf=NoneifCHANNELS==1:fft_buf=sp.signal.spectrogram(pcm_buf)l_lvl=int((np.mean(abs(pcm_buf[1::2]))/32768)*100)else:fft_buf=sp.signal.spectrogram(pcm_buf[0::2])l_lvl=int((np.mean(abs(pcm_buf[1::2]))/32768)*100)r_lvl=int((np.mean(abs(pcm_buf[0::2]))/32768)*100)fb.clear()draw_fft(fb,fft_buf)draw_audio_bar(fb,l_lvl,0)ifCHANNELS==2:draw_audio_bar(fb,r_lvl,25)fb.flush()# Stop streamingaudio.stop_streaming()
# Use nRF Connect from App store, connect to the Nano and write 1/0 to control the LED.importtimefromboardimportLEDfromubluepyimportService,Characteristic,UUID,Peripheral,constantsdefevent_handler(id,handle,data):globalperiphglobalserviceifid==constants.EVT_GAP_CONNECTED:passelifid==constants.EVT_GAP_DISCONNECTED:# restart advertisementperiph.advertise(device_name="Nano 33 BLE Sense",services=[service])elifid==constants.EVT_GATTS_WRITE:LED(1).on()ifint(data[0])elseLED(1).off()# start off with LED(1) offLED(1).off()notif_enabled=Falseuuid_service=UUID("0x1523")uuid_led=UUID("0x1525")service=Service(uuid_service)char_led=Characteristic(uuid_led,props=Characteristic.PROP_WRITE)service.addCharacteristic(char_led)periph=Peripheral()periph.addService(service)periph.setConnectionHandler(event_handler)periph.advertise(device_name="Nano 33 BLE Sense",services=[service])while(True):time.sleep_ms(500)
まとめ
この記事では、OpenMV IDEでNano BLE Senseボードを制御するいくつかのスクリプトを概観しました。ArduinoボードでMicroPythonには、他のボード用のガイドや、Python®を学習するための有用なリンクもあります。