def test(): """Bouncing box.""" try: # Baud rate of 14500000 seems about the max spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) display.clear() colors = [ color565(255, 0, 0), color565(0, 255, 0), color565(0, 0, 255), color565(255, 255, 0), color565(0, 255, 255), color565(255, 0, 255) ] sizes = [12, 11, 10, 9, 8, 7] boxes = [Box(128, 128, sizes[i], display, colors[i]) for i in range(6)] while True: timer = ticks_us() for b in boxes: b.update_pos() b.draw() # Attempt to set framerate to 30 FPS timer_dif = 33333 - ticks_diff(ticks_us(), timer) if timer_dif > 0: sleep_us(timer_dif) except KeyboardInterrupt: display.cleanup()
def test(): """CircuitPython Text, Shape & Sprite""" if implementation.name != 'circuitpython': print() print('This demo is for CircuitPython only!') exit() try: # Configuratoin for CS and DC pins: cs_pin = DigitalInOut(board.P0_15) dc_pin = DigitalInOut(board.P0_17) rst_pin = DigitalInOut(board.P0_20) # Setup SPI bus using hardware SPI: spi = SPI(clock=board.P0_24, MOSI=board.P0_22) # Create the SSD1351 display: display = Display(spi, dc=dc_pin, cs=cs_pin, rst=rst_pin) display.clear() # Load Fixed Font fixed = XglcdFont('fonts/FixedFont5x8.c', 5, 8, letter_count=96) # Title WIDTH = 128 text = 'CircuitPython Demo' # Measure text and center length = fixed.measure_text(text) x = int((WIDTH / 2) - (length / 2)) display.draw_text(x, 6, text, fixed, color565(255, 255, 0)) # Draw title outline display.draw_rectangle(0, 0, 127, 20, color565(0, 255, 0)) # Load sprite logo = BouncingSprite('images/blinka45x48.raw', 45, 48, 128, 128, 1, display) while True: timer = monotonic() logo.update_pos() logo.draw() # Attempt to set framerate to 30 FPS timer_dif = .033333333 - (monotonic() - timer) if timer_dif > 0: sleep(timer_dif) except KeyboardInterrupt: display.cleanup()
def test(): """Scrolling Marquee""" try: # Implementation dependant pin and SPI configuration if implementation.name == 'circuitpython': import board from busio import SPI from digitalio import DigitalInOut cs_pin = DigitalInOut(board.P0_15) dc_pin = DigitalInOut(board.P0_17) rst_pin = DigitalInOut(board.P0_20) spi = SPI(clock=board.P0_24, MOSI=board.P0_22) else: from machine import Pin, SPI cs_pin = Pin(5) dc_pin = Pin(17) rst_pin = Pin(16) spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) # Create the SSD1351 display: display = Display(spi, dc=dc_pin, cs=cs_pin, rst=rst_pin) display.clear() # Draw non-moving circles display.fill_circle(63, 63, 63, color565(27, 72, 156)) display.fill_circle(63, 63, 53, color565(0, 0, 0)) display.fill_circle(63, 63, 43, color565(189, 0, 36)) display.fill_circle(63, 63, 33, color565(0, 0, 0)) # Load Marquee image display.draw_image('images\Rototron128x26.raw', 0, 50, 128, 26) # Set up scrolling display.set_scroll(horiz_offset=1, vert_start_row=50, vert_row_count=26, vert_offset=0, speed=1) display.scroll(True) while True: # Do nothing, scrolling handled by hardware sleep(1) except KeyboardInterrupt: display.cleanup()
def test(): """Bouncing sprite.""" try: # Baud rate of 14500000 seems about the max spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) display.clear() # Load sprite logo = BouncingSprite('images/Python41x49.raw', 41, 49, 128, 128, 1, display) while True: timer = ticks_us() logo.update_pos() logo.draw() # Attempt to set framerate to 30 FPS timer_dif = 33333 - ticks_diff(ticks_us(), timer) if timer_dif > 0: sleep_us(timer_dif) except KeyboardInterrupt: display.cleanup()
if water_level > 0: display.fill_hrect(1, 53, water_level, 18, color) display.fill_hrect(water_level + 1, 53, 121 - water_level, 18, color565(0, 0, 0)) display.draw_text(37, 40, str(water_level) + '/' + str(MAX_WATER) + ' ', bally, color565(255, 255, 255)) if spi is None: spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) display.draw_image('MicroPython128x128.raw', 0, 0, 128, 128) sleep(1) display.clear() bally = XglcdFont('Bally7x9.c', 7, 9) display.draw_text(0, 0, 'Diesel:', bally, color565(255, 255, 255)) display.draw_text(0, 40, 'Vann:', bally, color565(255, 255, 255)) display.draw_text(0, 80, 'Septik:', bally, color565(255, 255, 255)) display.draw_rectangle(0, 12, 120, 20, color565(255, 255, 255)) display.draw_rectangle(0, 52, 120, 20, color565(255, 255, 255)) display.draw_rectangle(0, 92, 120, 20, color565(255, 255, 255)) adc = ADC(Pin(35)) adc.atten(adc.ATTN_11DB) #normalized to 3.3v while True: # read_septic() read_water()
class OTV(): def __init__(self): spi = SPI(2, sck=Pin(18), mosi=Pin(23), miso=Pin(19),baudrate=14500000) self.display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) # self.display("连接MQTT服务器...") self.mqtt = network.mqtt("otv", "mqtt://t.hsxsix.com", user="******", password="******", cleansession=True, connected_cb=self.conncb, disconnected_cb=self.disconncb, subscribed_cb=self.subscb, published_cb=self.pubcb, data_cb=self.datacb) self.weather_data = {} self.weather_api = 'http://118.24.144.127/weather/v1?city={}&node=micropython_ssd1351' self.weather_tm = Timer(2) self.weather_tm.init(period=1000*60*20, mode=self.weather_tm.PERIODIC, callback=self.update_weather) def publish(self, msg): self.mqtt.publish('otv', 'Hi from Micropython') def conncb(self, task): # self.dispaly("连接MQTT成功") print("[{}] Connected".format(task)) self.mqtt.subscribe('otv') def disconncb(self, task): print("[{}] Disconnected".format(task)) def subscb(self, task): print("[{}] Subscribed".format(task)) def pubcb(self, pub): print("[{}] Published: {}".format(pub[0], pub[1])) def datacb(self, msg): ''' data format: "tv_id|image url|XX|XX|XX" ''' _, image_url, image_size, position = msg[2].split('|') print("Received a task to show image:\nimage url:{},\nimage size:{}".format(image_url,image_size)) # self.display.draw_image("收到文件,正在下载。。。") download_image = self.http_get(image_url) w,h = image_size.split(',') x,y = position.split(',') if download_image: self.display.clear() self.display.draw_image(download_image, int(x), int(y), int(w), int(h)) def update_weather(self, timer): # self.display("update weather data。。。") self.http_get(self.weather_api, types='text', file_name='weather.txt') with open(weather_file, 'r') as f: self.weather_data = json.loads(f.read()) def show_today_weather(self): if self.weather_data['code'] == 'ok': today_weather = self.weather_data['0']['weather_code'] current_temp = self.weather_data['0']['current_temp'] current_weather = self.weather_data['0']['current_weather'] date = self.weather_data['0']['date'] temp = self.weather_data['0']['temp'] today_aqi = self.weather_data['0']['aqi'] # self.display.draw_image('bg.raw',0,32,128,96) self.display.draw_image('{}.raw'.format(today_weather),5,37,50,50) i=0 for char in current_weather: start_x=50+int((78-15*len(current_weather))/2)+15*i self.display.draw_bitarray(w_char, start_x,42,15,16) i+=1 i=0 for t_char in temp: start_x =50+int((78-9*len(current_weather))/2)+9*i self.display.draw_bitarray(w_char, start_x,64,9,16) i+=1 x = 9 if len(today_aqi)==9 else 5 for char in today_aqi: if len(char) > 100: self.display.draw_bitarray(w_char, x,92,15,16) x = x+15 else: self.display.draw_bitarray(w_char, x,92,9,16) x = x+9 # for char in date: # self.display.draw_bitarray(w_char, 0,32,9,16) # for char in current_temp: # self.display.draw_bitarray(w_char, 0,32,9,16) else: pass # self.display.draw_image("weather data error!") def show_three_day_weather(self): for day in ('1','2','3'): weather = self.weather_data[day]['weather_code'] temp = self.weather_data[day]['temp'] date = self.weather_data[day]['date'] aqi = self.weather_data[day]['aqi'] def http_get(self, url, types='image', file_name=None): download = None try: proto, dummy, host, path = url.split("/", 3) except ValueError: proto, dummy, host = url.split("/", 2) path = "" if proto == "http:": port = 80 else: raise ValueError("Unsupported protocol: " + proto) if ":" in host: host, port = host.split(":", 1) port = int(port) addr = socket.getaddrinfo(host, port)[0][-1] if not file_name: file_name = path.split('/')[-1] s = socket.socket() s.connect(addr) s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8')) while True: data = s.readline() if not data or data == b"\r\n": print(data) break if types == 'image': with open (file_name, 'wb') as f: while True: data = s.recv(512) if data: f.write(data) else: download = file_name break else: with open(file_name, 'w') as f: while True: data = s.recv(512) if data: f.write(data) else: download = file_name break s.close() return download def start(self): self.mqtt.start()
def test(): """Test code.""" spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) print("Loading fonts, please wait.") arcadepix = XglcdFont('fonts/ArcadePix9x11.c', 9, 11) bally = XglcdFont('fonts/Bally7x9.c', 7, 9) broadway = XglcdFont('fonts/Broadway17x15.c', 17, 15) espresso_dolce = XglcdFont('fonts/EspressoDolce18x24.c', 18, 24) fixed_font = XglcdFont('fonts/FixedFont5x8.c', 5, 8) neato = XglcdFont('fonts/Neato5x7.c', 5, 7, letter_count=223) robotron = XglcdFont('fonts/Robotron7x11.c', 7, 11) unispace = XglcdFont('fonts/Unispace12x24.c', 12, 24) wendy = XglcdFont('fonts/Wendy7x8.c', 7, 8) print("Fonts loaded.") display.draw_text(0, 0, 'Arcade Pix 9x11', arcadepix, color565(255, 0, 0)) display.draw_text(0, 12, 'Bally 7x9', bally, color565(0, 255, 0)) display.draw_text(0, 23, 'Broadway', broadway, color565(0, 0, 255)) display.draw_text(0, 36, 'Espresso', espresso_dolce, color565(0, 255, 255)) display.draw_text(0, 64, 'Fixed Font 5x8', fixed_font, color565(255, 0, 255)) display.draw_text(0, 76, 'Neato 5x7', neato, color565(255, 255, 0)) display.draw_text(0, 85, 'Robotron 7x11', robotron, color565(255, 255, 255)) display.draw_text(0, 96, 'Unispace', unispace, color565(255, 128, 0)) display.draw_text(0, 120, 'Wendy 7x8', wendy, color565(255, 0, 128)) sleep(9) display.clear() display.draw_text(0, 0, 'Arcade Pix 9x11', arcadepix, color565(255, 0, 0), landscape=True) display.draw_text(12, 0, 'Bally 7x9', bally, color565(0, 255, 0), landscape=True) display.draw_text(23, 0, 'Broadway', broadway, color565(0, 0, 255), landscape=True) display.draw_text(36, 0, 'Espresso', espresso_dolce, color565(0, 255, 255), landscape=True) display.draw_text(64, 0, 'Fixed Font 5x8', fixed_font, color565(255, 0, 255), landscape=True) display.draw_text(76, 0, 'Neato 5x7', neato, color565(255, 255, 0), landscape=True) display.draw_text(85, 0, 'Robotron 7x11', robotron, color565(255, 255, 255), landscape=True) display.draw_text(96, 0, 'Unispace', unispace, color565(255, 128, 0), landscape=True) display.draw_text(120, 0, 'Wendy 7x8', wendy, color565(255, 0, 128), landscape=True) sleep(9) display.clear() display.draw_text(0, 0, 'Arcade Pix 9x11', arcadepix, color565(255, 0, 0), background=color565(0, 255, 255)) display.draw_text(0, 12, 'Bally 7x9', bally, color565(0, 255, 0), background=color565(0, 0, 128)) display.draw_text(0, 23, 'Broadway', broadway, color565(0, 0, 255), background=color565(255, 255, 0)) display.draw_text(0, 36, 'Espresso', espresso_dolce, color565(0, 255, 255), background=color565(255, 0, 0)) display.draw_text(0, 64, 'Fixed Font 5x8', fixed_font, color565(255, 0, 255), background=color565(0, 128, 0)) display.draw_text(0, 76, 'Neato 5x7', neato, color565(255, 255, 0), background=color565(0, 0, 255)) display.draw_text(0, 85, 'Robotron 7x11', robotron, color565(255, 255, 255), background=color565(128, 128, 128)) display.draw_text(0, 96, 'Unispace', unispace, color565(255, 128, 0), background=color565(0, 128, 255)) display.draw_text(0, 120, 'Wendy 7x8', wendy, color565(255, 0, 128), background=color565(255, 255, 255)) sleep(9) display.cleanup()
def test(): """Test code.""" # Baud rate of 14500000 seems about the max spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) print('spi started') display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) print('display started') display.clear(color565(64, 0, 255)) sleep(1) display.clear() display.draw_hline(10, 127, 63, color565(255, 0, 255)) sleep(1) display.draw_vline(10, 0, 127, color565(0, 255, 255)) sleep(1) display.fill_hrect(23, 50, 30, 75, color565(255, 255, 255)) sleep(1) display.draw_hline(0, 0, 127, color565(255, 0, 0)) sleep(1) display.draw_line(127, 0, 64, 127, color565(255, 255, 0)) sleep(2) display.clear() coords = [[0, 63], [78, 80], [122, 92], [50, 50], [78, 15], [0, 63]] display.draw_lines(coords, color565(0, 255, 255)) sleep(1) display.clear() display.fill_polygon(7, 63, 63, 50, color565(0, 255, 0)) sleep(1) display.fill_rectangle(0, 0, 15, 127, color565(255, 0, 0)) sleep(1) display.clear() display.fill_rectangle(0, 0, 63, 63, color565(128, 128, 255)) sleep(1) display.draw_rectangle(0, 64, 63, 63, color565(255, 0, 255)) sleep(1) display.fill_rectangle(64, 0, 63, 63, color565(128, 0, 255)) sleep(1) display.draw_polygon(3, 96, 96, 30, color565(0, 64, 255), rotate=15) sleep(3) display.clear() display.fill_circle(32, 32, 30, color565(0, 255, 0)) sleep(1) display.draw_circle(32, 96, 30, color565(0, 0, 255)) sleep(1) display.fill_ellipse(96, 32, 30, 16, color565(255, 0, 0)) sleep(1) display.draw_ellipse(96, 96, 16, 30, color565(255, 255, 0)) sleep(5) display.cleanup()