回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

【PinPong Board】像素风格网络时钟 简单

头像 豆爸 2021.05.09 498 0

材料清单

  • PinPong Board X1

步骤1 灵感来源

project-image

面对1000+的LaMetric Time的智能时钟,让很多爱好者望而却步。

同时各种DIY的像素网络时钟也一度疯迷网络。

今天,让我们来给PinPong Board也来一个像素风。

步骤2 程序流程

1、连接Wifi。

2、通过NTP服务器获取时间。

3、OLED输出像素风格时钟。

步骤3 像素风时钟效果

project-image

步骤4 程序代码

代码
from pinpong.board import Board,Pin,WIFI
from dfrobot_ssd1306 import SSD1306,SSD1306_I2C #导入ssd1306库
import math
import time
import ntplib
char0 = ['111','101','101','101','111']
char1 = ['010','110','010','010','111']
char2 = ['111','001','111','100','111']
char3 = ['111','001','111','001','111']
char4 = ['101','101','111','001','001']
char5 = ['111','100','111','001','111']
char6 = ['111','100','111','101','111']
char7 = ['111','001','001','001','001']
char8 = ['111','101','111','101','111']
char9 = ['111','101','111','001','111']
colon = ['000','010','000','010','000']
Board("uno").begin()
oled=SSD1306_I2C(width=128, height=64)
oled.fill(0)
ssid = "ssid"
pwd = "password"
def connect_network(ssid, pwd):
    obj = WIFI()
    obj.set_ssid(ssid)
    obj.set_password(pwd)
    obj.connect_wifi()
    print("Waiting for WIFI connection...")
    time.sleep(5)
    while True:
      ip,port = obj.get_ip_port()
      if ip != None:
        print("ip: {} port: {}".format(ip, port))
        break
      time.sleep(1)
    ip = "ip:" + str(ip)
    port = "port:"+str(port)
    oled.text(ip,0,10)
    oled.text(port,0,30)
    oled.show()
    time.sleep(1)

def pixel(pixel_x,pixel_y):
    for y2 in range(4):
        for x2 in range(4):
            oled.pixel(pixel_x + x2, pixel_y + y2,1)


def pixel_char(x,y,char): 
    y1=0
    for y1 in range(5):
        x1=0
        for x1 in range(3):
            if str(char) == '0':
                text=str(char0[y1])
            elif str(char) == '1':
                text=str(char1[y1])
            elif str(char) == '2':
                text=str(char2[y1])
            elif str(char) == '3':
                text=str(char3[y1])
            elif str(char) == '4':
                text=str(char4[y1])
            elif str(char) == '5':
                text=str(char5[y1])
            elif str(char) == '6':
                text=str(char6[y1])
            elif str(char) == '7':
                text=str(char7[y1])
            elif str(char) == '8':
                text=str(char8[y1])
            elif str(char) == '9':
                text=str(char9[y1])
            elif str(char) == ':':
                text=str(colon[y1])
            elif str(char) =='sunny':
                text=str(sunny[y1])
            elif str(char) == 'degree':
                text=str(degree[y1]) 
            if text[x1] == "1":
                pixel(x + x1*5, y + y1*5)
                
connect_network(ssid,pwd)
while True:
    c = ntplib.NTPClient()
    response = c.request('ntp1.aliyun.com', version=3)     
    ts = response.tx_time
    _time = time.strftime('%X',time.localtime(ts))
    print(_time)        
    oled.fill(0)
    pixel_char(20,30,_time[0:1])
    pixel_char(40,30,_time[1:2])
    pixel_char(60,30,':')
    pixel_char(80,30,_time[3:4])
    pixel_char(100,30,_time[4:5]) 
    oled.show()

评论

user-avatar