def test(): """Test code.""" spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(4), cs=Pin(5), rst=Pin(21)) while 1: display.draw_image('1.raw', 0, 32, 128, 96) sleep(5) display.draw_image('2.raw', 0, 32, 128, 96) sleep(5) display.cleanup()
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)) display.contrast(0) display.draw_image('images/MicroPython128x128.raw', 0, 0, 128, 128) fixed_font = XglcdFont('fonts/FixedFont5x8.c', 5, 8) contrast_range = list(range(1, 16)) + list(reversed(range(15))) for c in contrast_range: display.contrast(c) display.draw_text(30, 120, 'contrast: {0:02d}'.format(c), fixed_font, color565(255, 255, 255)) sleep(1) 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(): """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)) display.draw_image('images/Tabby128x128.raw', 0, 0, 128, 128) print("Loading fonts, please wait.") fixed_font = XglcdFont('fonts/FixedFont5x8.c', 5, 8) unispace = XglcdFont('fonts/Unispace12x24.c', 12, 24) print("Fonts loaded.") display.draw_text(0, 0, 'Not transparent', fixed_font, color565(255, 0, 255)) display.draw_text(0, 80, 'Transparent', unispace, color565(0, 128, 255), spacing=0, transparent=True) display.draw_text(0, 103, 'Background', unispace, color565(0, 128, 255), color565(255, 255, 255), spacing=0) display.draw_text(103, 20, 'Test', unispace, color565(0, 255, 128), landscape=True, spacing=2, transparent=True) display.draw_text(0, 20, 'Test', unispace, color565(128, 255, 0), landscape=True) sleep(9) display.cleanup()
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)) display.draw_image('images/RaspberryPiWB128x128.raw', 0, 0, 128, 128) sleep(5) display.draw_image('images/MicroPython128x128.raw', 0, 0, 128, 128) sleep(5) display.draw_image('images/Tabby128x128.raw', 0, 0, 128, 128) sleep(5) display.draw_image('images/Tortie128x128.raw', 0, 0, 128, 128) sleep(9) display.cleanup()
def main(): """Initialize display.""" # 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)) # use Wifiboy screen pins spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23)) display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16)) # Draw background image display.draw_image('images/Arkanoid_Border128x118.raw', 0, 10, 128, 118) # Initialize ADC on VP pin 36 adc = ADC(Pin(36)) # Set attenuation 0-2V (Will use resistor to limit pot to 2V). adc.atten(ADC.ATTN_6DB) # Seed random numbers seed(ticks_us()) # Generate bricks MAX_LEVEL = const(9) level = 1 bricks = load_level(level, display) # Initialize paddle paddle = Paddle(display) # Initialize score score = Score(display) # Initialize balls balls = [] # Add first ball balls.append(Ball(59, 111, -2, -1, display, frozen=True)) # Initialize lives lives = [] for i in range(1, 3): lives.append(Life(i, display)) # Initialize power-ups powerups = [] try: while True: timer = ticks_us() # Set paddle position to ADC spinner (scale 6 - 98) paddle.h_position(adc.read() // 44 + 5) # Handle balls score_points = 0 for ball in balls: # Position ball.set_position(paddle.x, paddle.y, paddle.x2, paddle.center) # Check for collision with bricks if not frozen if not ball.frozen: prior_collision = False ball_x = ball.x ball_y = ball.y ball_x2 = ball.x2 ball_y2 = ball.y2 ball_center_x = ball.x + ((ball.x2 + 1 - ball.x) // 2) ball_center_y = ball.y + ((ball.y2 + 1 - ball.y) // 2) # Check for hits for brick in bricks: if (ball_x2 >= brick.x and ball_x <= brick.x2 and ball_y2 >= brick.y and ball_y <= brick.y2): # Hit if not prior_collision: ball.x_speed, ball.y_speed = brick.bounce( ball.x, ball.y, ball.x2, ball.y2, ball.x_speed, ball.y_speed, ball_center_x, ball_center_y) prior_collision = True score_points += 1 brick.clear() bricks.remove(brick) # Generate random power-ups if score_points > 0 and randint(1, 20) == 7: powerups.append(Powerup(ball.x, 64, display)) # Check for missed if ball.y2 > display.height - 3: ball.clear_previous() balls.remove(ball) if not balls: # Clear powerups powerups.clear() # Lose life if last ball on screen if len(lives) == 0: score.game_over() else: # Subtract Life lives.pop().clear() # Add ball balls.append( Ball(59, 112, 2, -3, display, frozen=True)) else: # Draw ball ball.draw() # Update score if changed if score_points: score.increment(score_points) # Handle power-ups for powerup in powerups: powerup.set_position(paddle.x, paddle.y, paddle.x2, paddle.center) powerup.draw() if powerup.collected: # Power-up collected powerup.clear() # Add ball balls.append( Ball(powerup.x, 112, 2, -1, display, frozen=False)) powerups.remove(powerup) elif powerup.y2 > display.height - 3: # Power-up missed powerup.clear() powerups.remove(powerup) # Check for level completion if not bricks: for ball in balls: ball.clear() balls.clear() for powerup in powerups: powerup.clear() powerups.clear() level += 1 if level > MAX_LEVEL: level = 1 bricks = load_level(level, display) balls.append(Ball(59, 111, -2, -1, display, frozen=True)) # 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 < 20: color = color565(255, 0, 0) 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:
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()