def thrainbow(): pos = 0 np.brightness(25) while True: np.rainbow(pos, 3) notif = _thread.getnotification() if (notif > 0) and (notif <= 100): np.brightness(math.trunc(notif * 2.55)) elif notif == 1000: _thread.sendmsg(_thread.getReplID(), "[%s] Run counter = %u" % (_thread.getSelfName(), pos)) pos = pos + 1
def rgbled(n=200, led=bled): notif_exit = 4718 notif_replay = 2 notif_count = 3 x = 0 _thread.allowsuspend(True) while True: led.value(1) time.sleep_ms(n) led.value(0) x = x + 1 t = 10 while t > 0: notif = _thread.getnotification() if notif == notif_exit: _thread.sendmsg(_thread.getReplID(), "[%s] Exiting" % (_thread.getSelfName())) return elif notif == notif_replay: _thread.sendmsg(_thread.getReplID(), "[%s] I've been notified" % (_thread.getSelfName())) elif notif == notif_count: _thread.sendmsg(_thread.getReplID(), "[%s] Run counter = %u" % (_thread.getSelfName(), x)) elif notif == 777: _thread.sendmsg(_thread.getReplID(), "[%s] Forced EXCEPTION" % (_thread.getSelfName())) time.sleep_ms(1000) zz = 234 / 0 elif notif != 0: _thread.sendmsg(_thread.getReplID(), "[%s] Got unknown notification: %u" % (_thread.getSelfName(), notif)) typ, sender, msg = _thread.getmsg() if msg: _thread.sendmsg(_thread.getReplID(), "[%s] Message from '%s'\n'%s'" % (_thread.getSelfName(), _thread.getThreadName(sender), msg)) time.sleep_ms(100) t = t - 1 gc.collect()
def bmerun(interval=60): _thread.allowsuspend(True) sendmsg = True send_time = time.time() + interval while True: while time.time() < send_time: notif = _thread.getnotification() if notif == 10002: _thread.sendmsg(_thread.getReplID(), bmevalues()) elif notif == 10004: sendmsg = False elif notif == 10006: sendmsg = True elif (notif <= 3600) and (notif >= 10): interval = notif send_time = time.time() + interval _thread.sendmsg(_thread.getReplID(), "Interval set to {} seconds".format(interval)) time.sleep_ms(100) send_time = send_time + interval if sendmsg: _thread.sendmsg(_thread.getReplID(), bmevalues())
def thrainbow_py(): pos = 0 bri = 0.02 while True: for i in range(0, num_np): dHue = 360.0/num_np * (pos+i); hue = dHue % 360; np.setHSB(i, hue, 1.0, bri, 1, False) np.show() notif = _thread.getnotification() if (notif > 0) and (notif <= 100): bri = notif / 100.0 elif notif == 1000: _thread.sendmsg(_thread.getReplID(), "[%s] Run counter = %u" % (_thread.getSelfName(), pos)) pos = pos + 1
def _httpHandlerPost(httpClient, httpResponse) : # read posted form data. If data (dictionary) received, convert to string and send threadMessage to mainThread formData = httpClient.ReadRequestPostedFormData() if formData: print(formData) message = str(formData) _thread.sendmsg(_thread.getReplID(), message) # get system time and save it t datetime_dict datetime = utime.localtime() #print(datetime) datetime_dict = { "year" : datetime[0], "month" : datetime[1], "mday" : datetime[2], "hour": datetime[3], "minute" : datetime[4], "second" : datetime[5], "weekday" : datetime[6], "yearday" : datetime[7]} htmlSite = htmlSite.format(**datetime_dict) # send htmlSite as response to client httpResponse.WriteResponseOk( headers = ({'Cache-Control': 'no-cache'}), contentType = 'text/html', contentCharset = 'UTF-8', content = htmlSite)
def test(): i = 0 while i<10: i=i+1 print("Hello from thread") print(_thread.getSelfName()) mainID = _thread.getReplID() print(mainID) print(_thread.getmsg()) print(_thread.wait()) while _thread.wait() : #_thread.sendmsg(mainID, i) time.sleep(2) thrID = _thread.start_new_thread("test",test,()) _thread.sendmsg(thrID, 5) _thread.getmsg() _thread.list() _thread.notify(thrID, 1) #k=_thread.start_new_thread("testThr",testThread,(1,)) _thread.start_new_thread(testThread()) k=0 while k<15: k=k+1 time.sleep(1) print("Hello from main") pass