def app_start(): connectWifi() oled.fill(0) oled.fill_rect(0, 0, 128, 10, 1) oled.hctext('SGP Weather', 1, 0) if not wlan.isconnected(): oled.hctext('No WiFi :(', 24, 1) oled.hctext('Connection', 32, 1) oled.hctext('[B] to Quit', 56, 1) oled.show() while (btn.B.value()): sleep_ms(100) return 1 oled.hctext('Connecting to', 24, 1) oled.hctext('data.gov.sg', 32, 1) oled.show() r1 = urequests.get( 'https://api.data.gov.sg/v1/environment/24-hour-weather-forecast') r2 = urequests.get( 'https://api.data.gov.sg/v1/environment/2-hour-weather-forecast') d1 = r1.json() d2 = r2.json() forecast = d1['items'][0]['general']['forecast'] rhumid = d1['items'][0]['general']['relative_humidity'] temp = d1['items'][0]['general']['temperature'] wind = d1['items'][0]['general']['wind'] oled.fill(0) oled.fill_rect(0, 0, 128, 10, 1) oled.hctext('24 Hour Forecast', 1, 0) oled.text(forecast, 0, 17, 1) oled.text('RH ' + str(rhumid['low']) + '-' + str(rhumid['high']) + ' %', 0, 32, 1) oled.text('Temp ' + str(temp['low']) + '-' + str(temp['high']) + ' C', 0, 40, 1) oled.text('Wind Dir ' + str(wind['direction']), 0, 48, 1) oled.text( ' @ ' + str(wind['speed']['low']) + '-' + str(wind['speed']['high']) + ' km/h', 0, 56, 1) oled.show() while btn.A.value() and btn.B.value(): sleep_ms(100) sleep_ms(200) forecasts = [] for area in d2['items'][0]['forecasts']: forecasts.extend([area['area'][:16], area['forecast'][:16], '']) sid = 0 while True: sid = selectVList('2 Hour Nowcast', forecasts, sid, 1) if sid == -1: return 0
def msgBox(header, lines): oled.fill(0) oled.fill_rect(0,0,128,10,1) oled.text(header,0,1,0) if type(lines) != list: lines = [lines[i:i+16] for i in range(0, len(lines), 16)] for y, line in enumerate(lines): oled.text(line,0,11+y*8,1) oled.show() while btn.B.value(): sleep_ms(100) sleep_ms(300)
def app_start(): oled.fill(0) oled.text('Hello', 0, 0, 1) oled.text('World', 0, 8, 1) oled.show() for y in range(2 * 8, -1, -1): oled.invert(y % 2) for x in range(5 * 8, -1, -1): oled.fill_rect(x * 3, y * 3, 3, 3, oled.pixel(x, y)) oled.show() oled.hctext('[B] to Quit', 56, 1) oled.show() while btn.B.value(): sleep_ms(20)
def drawDualButton(lefttext, righttext, leftsel, rightsel): btnWidth = 54 btnHeight = 14 topTextOffset = int((btnHeight-8)/2) leftTextOffset = int((btnWidth - len(lefttext)*8)/2) rightTextOffset = int((btnWidth - len(righttext)*8)/2) if leftsel: oled.fill_rect(5, int(64/2-btnHeight/2), btnWidth, btnHeight, 1) else: oled.rect(5, int(64/2-btnHeight/2), btnWidth, btnHeight, 1) oled.text(lefttext, 5+leftTextOffset, int(64/2-btnHeight/2) + topTextOffset, 1-leftsel) if rightsel: oled.fill_rect(128-5-btnWidth, int(64/2-btnHeight/2), btnWidth, btnHeight, 1) else: oled.rect(128-5-btnWidth, int(64/2-btnHeight/2), btnWidth, btnHeight, 1) oled.text(righttext, 128-5-btnWidth+rightTextOffset, int(64/2-btnHeight/2) + topTextOffset, 1-rightsel)
def inputDrawing(width=8, height=8, _S=8, buffer=-1): import framebuf if buffer == -1: buffer = bytearray(width*height//8) fbuf = framebuf.FrameBuffer(buffer, width, height, framebuf.MONO_HLSB) cx,cy=0,0 rdw=1 while btn.B.value(): if not btn.U.value(): while not btn.U.value(): sleep_ms(10) cy=(cy-1)%height rdw=1 if not btn.D.value(): while not btn.D.value(): sleep_ms(10) cy=(cy+1)%height rdw=1 if not btn.L.value(): while not btn.L.value(): sleep_ms(10) cx=(cx-1)%width rdw=1 if not btn.R.value(): while not btn.R.value(): sleep_ms(10) cx=(cx+1)%width rdw=1 if not btn.A.value(): while not btn.A.value(): sleep_ms(10) fbuf.pixel(cx,cy,not fbuf.pixel(cx,cy)) rdw=1 if rdw: rdw=0 oled.fill(0) for x in range(width): for y in range(width): if fbuf.pixel(x,y): oled.fill_rect(x*_S,y*_S,_S,_S,1) oled.rect(cx*_S,cy*_S,_S,_S,not fbuf.pixel(cx,cy)) oled.text('Draw',80,0,1) oled.text('Cursor:',68,16,1) curstat='(%s,%s)'%(cx,cy) oled.text(curstat,96-len(curstat)*4,26,1) oled.text("[B] to",72,47,1) oled.text("Finish",72,56,1) oled.show() sleep_ms(20) return (width,height,buffer)
def inputAlphanumeric(): selRow = 0 selCol = 0 ans = "" heldDown = False while True: oled.fill(0) oled.hctext(ans[-15:]+('_' if (ticks_ms()>>9)%2 else ' '),0,1) oled.hline(0,9,128,1) for row in range(4): for col in range(16): color = (row == selRow and col == selCol) oled.fill_rect(1+col*8,20+row*10-1,8,10,color) oled.text(keyboard[row][col],1+col*8,20+row*10,not color) oled.show() if not btn.U.value(): selRow = (selRow + 4 - 1)%4 if not btn.D.value(): selRow = (selRow + 4 + 1)%4 if not btn.L.value(): selCol = (selCol + 16 - 1)%16 if not btn.R.value(): selCol = (selCol + 16 + 1)%16 if not btn.A.value(): if selRow == 3 and selCol == 15: return ans ans += keyboard[selRow][selCol] if not btn.B.value(): ans = ans[:-1] holdCnt = 0 while((not btn.U.value()) or (not btn.D.value()) or (not btn.L.value()) or (not btn.R.value()) or (not btn.A.value()) or (not btn.B.value())): sleep_ms(10) holdCnt += 1 if heldDown: if holdCnt >= 5: break else: if holdCnt >= 50: heldDown = True if holdCnt < 5: heldDown = False
def installApp(REPO_URL, manifest, APP_ROOT='/apps'): import upip_utarfile as tarfile import uzlib, os, gc from upip import url_open gc.collect() oled.fill(0) oled.text('Installing...', 0, 0, 1) oled.text(manifest['name'], 0, 8, 1) oled.text(manifest['version'], 0, 16, 1) oled.show() count = 0 try: s1 = url_open(REPO_URL + manifest['url']) f2 = uzlib.DecompIO(s1, 30) t3 = tarfile.TarFile(fileobj=f2) for x in t3: print(x) count += 1 oled.fill_rect(0, 32, 128, 16, 0) oled.hctext('File #%d' % count, 32, 1) oled.hctext(x.name[-16:], 40, 1) oled.show() if x.type == tarfile.DIRTYPE: # a dir FOLDER_PATH = APP_ROOT + '/' + x.name[:-1] print(FOLDER_PATH) if x.name[:-1] in os.listdir(APP_ROOT): deleteFolder(FOLDER_PATH) # delete if exists os.mkdir(FOLDER_PATH) else: # a file f4 = open(APP_ROOT + '/' + x.name, 'wb') f4.write(t3.extractfile(x).read()) f4.close() finally: s1.close() rebuildAppsIndex(APP_ROOT) oled.text('Done :) Reboot!', 0, 56, 1) oled.show() sleep_ms(500) import machine machine.reset()
def viewAppDetail(manifest): oled.fill(0) oled.fill_rect(0, 0, 128, 10, 1) oled.text(manifest['name'], 0, 1, 0) oled.hctext('V:%s' % manifest['version'], 21, 1) oled.hctext(manifest['author'], 31, 1) oled.text('[R] to Install', 0, 48, 1) oled.text('[B] to Cancel', 0, 56, 1) desc = manifest['desc'] + ' ' while True: ticknow = ticks_ms() oled.fill_rect(0, 11, 128, 9, 0) for i in range(16): oled.text(desc[(i + ticknow // 150) % len(desc)], 8 * i, 11, 1) oled.show() if not btn.R.value(): sleep_ms(300) return True if not btn.B.value(): sleep_ms(300) return False sleep_ms(20)
def app_start(): connectWifi() oled.fill(0) oled.fill_rect(0, 0, 128, 10, 1) oled.hctext('SGP Environment', 1, 0) if not wlan.isconnected(): oled.hctext('No WiFi :(', 24, 1) oled.hctext('Connection', 32, 1) oled.hctext('[B] to Quit', 56, 1) oled.show() while (btn.B.value()): sleep_ms(100) return 1 oled.hctext('Connecting to', 24, 1) oled.hctext('data.gov.sg', 32, 1) oled.show() psi = urequests.get('https://api.data.gov.sg/v1/environment/psi') psi = psi.json() psi_24 = psi['items'][0]['readings']['psi_twenty_four_hourly'] pm10_24 = psi['items'][0]['readings']['pm10_twenty_four_hourly'] pm25_24 = psi['items'][0]['readings']['pm25_twenty_four_hourly'] #psi3 = psi['items'][0]['readings']['psi_three_hourly'] oled.fill(0) oled.fill_rect(0, 0, 128, 10, 1) oled.hctext('SGP Environment', 1, 0) oled.hctext('24 hour values:', 17, 1) oled.text('PSI ' + str(psi_24['national']), 0, 32, 1) oled.hctext('(%s)' % psidescriptor(psi_24['national']), 40, 1) oled.text('PM10 %d ug/m^3' % pm10_24['national'], 0, 48, 1) oled.text('PM2.5 %d ug/m^3' % pm25_24['national'], 0, 56, 1) #oled.text(' @ '+str(wind['speed']['low'])+'-'+str(wind['speed']['high'])+' km/h',0,56,1) oled.show() while btn.A.value() and btn.B.value(): sleep_ms(100) sleep_ms(200) return
def startSession(isClient): import uselect poller = uselect.poll() poller.register(gameSocket, uselect.POLLIN) heldDown = False turn = 2 if isClient: board = [[0 for x in range(8)] for y in range(8)] selx = 0 sely = 0 else: board = initBoard selx = 0 sely = 7 found = False pieceSelected = (-1, -1) if not isClient: for i in range(10): gameSocket.sendto(numToBin(1) + boardToBinary(board), clientAddr) while True: while poller.poll(1): data, _ = gameSocket.recvfrom(200) cmd = binToNum(data[:1]) data = data[1:] if cmd == 1: board = binaryToBoard(data) elif cmd == 2: turn = binToNum(data) ''' for i in range(8): for j in range(8): if board[i][j] == turn: selx = i sely = j found = True break if found: break ''' #board = binaryToBoard(boardData[1:]) #find the first one if nothing is selected if isClient: boardDraw = flipBoard(board) else: boardDraw = board oled.fill(0) if isCurrentTurn(turn, isClient): if isClient: drawBoard(boardDraw, 7-selx, 7-sely) else: drawBoard(boardDraw, selx, sely) oled.text("GO", 102, 52, 1) else: drawBoard(boardDraw, -1, -1) oled.show() if isClient: if not btn.U.value(): sely = min(7, sely + 1) if not btn.D.value(): sely = max(0, sely - 1) if not btn.L.value(): selx = min(7, selx + 1) if not btn.R.value(): selx = max(0, selx - 1) else: if not btn.U.value(): sely = max(0, sely - 1) if not btn.D.value(): sely = min(7, sely + 1) if not btn.L.value(): selx = max(0, selx - 1) if not btn.R.value(): selx = min(7, selx + 1) if (not btn.A.value()) and isCurrentTurn(turn, isClient): #turn locked #selecting a piece if isFriendly(turn, board[sely][selx]): for i in range(8): for j in range(8): if board[i][j] == 5: board[i][j] = 0 moves = getValidMoves(board, selx, sely) if len(moves) == 0: continue for move in moves: x = move[0] y = move[1] board[y][x] = 5 pieceSelected = (selx, sely) #selecting a movement if board[sely][selx] == 5: for i in range(8): for j in range(8): if board[i][j] == 5: board[i][j] = 0 x, y = pieceSelected board[sely][selx] = board[y][x] board[y][x] = 0 if canPromote(turn, selx, sely): board[sely][selx] += 2 enemyCaptured = False for move in moves: if len(move) > 2 and move[0] == selx and move[1] == sely: enemyx = move[2] enemyy = move[3] board[enemyy][enemyx] = 0 enemyCaptured = True break if enemyCaptured: canCaptureMore = False moves = getValidMoves(board, selx, sely) for move in moves: if len(move) > 2: canCaptureMore = True if canCaptureMore: for move in moves: if len(move) > 2: x = move[0] y = move[1] board[y][x] = 5 pieceSelected = (selx, sely) else: turn = 3 - turn for i in range(20): if isClient: gameSocket.send(numToBin(1) + boardToBinary(board)) gameSocket.send(numToBin(2) + numToBin(turn)) else: gameSocket.sendto(numToBin(1) + boardToBinary(board), clientAddr) gameSocket.sendto(numToBin(2) + numToBin(turn), clientAddr) else: turn = 3 - turn for i in range(20): if isClient: gameSocket.send(numToBin(1) + boardToBinary(board)) gameSocket.send(numToBin(2) + numToBin(turn)) else: gameSocket.sendto(numToBin(1) + boardToBinary(board), clientAddr) gameSocket.sendto(numToBin(2) + numToBin(turn), clientAddr) if not btn.B.value(): for i in range(8): for j in range(8): if board[i][j] == 5: board[i][j] = 0 holdCnt = 0 while((not btn.U.value()) or (not btn.D.value()) or (not btn.L.value()) or (not btn.R.value()) or (not btn.A.value()) or (not btn.B.value())): sleep_ms(10) holdCnt += 1 if heldDown: if holdCnt >= 5: break else: if holdCnt >= 50: heldDown = True if holdCnt < 5: heldDown = False
def app_start(): apps = [] apps_manifest = ujson.load(open('/apps/apps.json')) # User Apps for app in apps_manifest['list']: apps.append((app['name'], '/apps/%s' % app['dir'], app['start'])) apps.extend([ # System Apps ('App Store', '/systemapps', 'appstore'), ('System Info', '/systemapps', 'sysinfo'), ('File Explorer', '/systemapps', 'fileexplorer'), ('WiFi Scan', '/systemapps', 'wifiscan'), ('C01N Config', '/systemapps', 'coinconfig'), ('Credits', '/systemapps', 'credits') ]) sidx = 0 lastbat = BAT.percentage() needredraw = 1 while True: sleep_ms(10) batnow = BAT.percentage() if abs(lastbat - batnow) > 0.1: needredraw = 1 lastbat = batnow if btn.L.value() == 0: sleep_ms(200) sidx = (sidx - 1) % len(apps) needredraw = 1 if btn.R.value() == 0: sleep_ms(200) sidx = (sidx + 1) % len(apps) needredraw = 1 if btn.A.value() == 0: sleep_ms(200) originalSysModules = set() for mod in sys.modules: originalSysModules.add(mod) # Hacky Launch Code exec( 'try:\n\tos.chdir("%s")\n\timport %s as curapp\n\tcurapp.app_start()\nexcept:\n\toled.hctext("App Launch Fail",30,1)\n\toled.show()\nfinally:\n\tos.chdir("/")' % (apps[sidx][1], apps[sidx][2])) # Efficient Cleanup of Userspace for mod in sys.modules: if mod not in originalSysModules: del sys.modules[mod] gc.collect() needredraw = 1 if needredraw: needredraw = 0 oled.fill(0) oled.text('Apps', 0, 1, 1) oled.fill_rect(108, 2, int(16 * max(min(batnow, 1), 0)), 5, 1) oled.fill_rect(126, 2, 2, 5, 1) oled.rect(106, 0, 20, 9, 1) batstatus = 'USB' if batnow > 1 else '%d%%' % (batnow * 100) oled.text(batstatus, 104 - len(batstatus) * 8, 1, 1) oled.hline(0, 10, 128, 1) oled.hctext(apps[sidx][0], 20, 1) oled.hctext('[A] to Launch', 45, 1) oled.hctext('< %d/%d >' % (sidx + 1, len(apps)), 56, 1) oled.show() np[0] = hsv_to_rgb(sidx / len(apps), 1, 7) np.write() sleep_ms(20)