Last active
March 20, 2019 15:00
-
-
Save ikaruga777/d54cae58bc6da2a73f35907123d73442 to your computer and use it in GitHub Desktop.
M5Stack入門のコード
This file contains hidden or 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
from m5stack import * | |
import network | |
import time | |
import _thread | |
class WifiList: | |
def __init__(self, fontHeight): | |
self.access_points = [] | |
self.cursor = 0 | |
self.fontHeight = fontHeight | |
def scan(self, wifi): | |
self.access_points = wifi.scan() | |
if self.cursor >= len(self.access_points): | |
self.cursor = len(self.access_points) | |
def move_cursor(self): | |
color = lcd.WHITE | |
if hasattr(self, 'target') and wifi_list.target is not None and self.target[0] == self.access_points[self.cursor][0]: | |
color = lcd.RED | |
lcd.text(0, self.cursor * self.fontHeight, self._access_point_format(self.access_points[self.cursor]), color) | |
self.cursor = self.cursor + 1 | |
if self.cursor >= len(self.access_points): | |
self.cursor = 0 | |
lcd.text(0, self.cursor * self.fontHeight, self._access_point_format(self.access_points[self.cursor]), lcd.YELLOW) | |
def subscribe(self): | |
self.target = self.access_points[self.cursor] | |
return self.target | |
def cancel(self): | |
self.target = None | |
def show_list(self): | |
lcd.setCursor(0, 0) | |
lcd.clear(0xEEEEEE) | |
for index, access_point in enumerate(self.access_points): | |
color = lcd.WHITE | |
if hasattr(self, 'target') and wifi_list.target is not None and self.target[0] == access_point[0]: | |
color = lcd.RED | |
elif self.cursor == index: | |
color = lcd.YELLOW | |
lcd.text(lcd.LASTX,lcd.LASTY, self._access_point_format(access_point), color) | |
def _access_point_format(self, access_point): | |
return "{0}: {1}\r\n".format(access_point[0].decode('UTF-8'), access_point[3]) | |
def on_wasAPressed(): | |
wifi_list.move_cursor() | |
def on_wasBPressed(): | |
wifi_list.subscribe() | |
def scan_wifi(): | |
while True: | |
wifi_list.scan(wifi) | |
wifi_list.show_list() | |
time.sleep_ms(30) | |
buttonA.wasPressed(on_wasAPressed) | |
buttonB.wasPressed(on_wasBPressed) | |
lcd.setCursor(0, 0) | |
lcd.setColor(lcd.BLACK) | |
lcd.clear(lcd.BLACK) | |
fw, fh = lcd.fontSize() | |
speaker.volume(1) | |
lcd.text(0,0,'initialize wifi..') | |
wifi = network.WLAN(network.STA_IF) | |
wifi.active(True) | |
wifi_list = WifiList(fh) | |
wifi_list.scan(wifi) | |
wifi_list.show_list() | |
def main(): | |
while True: | |
if hasattr(wifi_list, 'target') and wifi_list.target is not None: | |
target_access_point = wifi_list.target | |
if buttonC.isPressed(): | |
wifi_list.cancel() | |
rssi = list(filter(lambda s: s[0] == target_access_point[0], wifi_list.access_points))[0][3] | |
if rssi in range(0,-30,-1): | |
freq = 6600 | |
elif rssi in range(-30,-40,-1): | |
freq = 5500 | |
elif rssi in range(-40,-50,-1): | |
freq = 4400 | |
elif rssi in range(-50,-67,-1): | |
freq = 3300 | |
elif rssi in range(-68,-70,-1): | |
freq = 2200 | |
elif rssi in range (-71,-80,-1): | |
freq = 1100 | |
elif rssi in range (-81,-90,-1): | |
freq = 800 | |
else: | |
freq = 100 | |
speaker.tone(freq,20) | |
time.sleep_ms(100) | |
main_id = _thread.start_new_thread('main', main,()) | |
_thread.start_new_thread('scan_wifi', scan_wifi, ()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment