欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

智能互联网时钟 TeaCan2.0代码实例

程序员文章站 2023-11-19 18:22:40
有网友要我的TeaCan2.0的代码,我贴出来吧。这个代码是python3的,要下载屏幕的例程,这样才会有相关的驱动和库。不然代码会出错。还要几个库:PIL安装:(PIL是Python一个强大方便的图像处理库) pip install Pillow农历库Borax1.3安装 (Borax是一个的 Python3 开发工具集合库,不限于显示农历) pip install boraxrequests库:python3 -m pip install requests以下为全部代码:这里面可能会有...
有网友要我的TeaCan2.0的代码,我贴出来吧。这个代码是python3的,要下载屏幕的例程,这样才会有相关的驱动和库。不然代码会出错。还要几个库:
PIL安装:(PIL是Python一个强大方便的图像处理库)
 pip install Pillow
农历库Borax1.3安装 (Borax是一个的 Python3 开发工具集合库,不限于显示农历)
 pip install borax
requests库:

python3 -m pip install requests

以下为全部代码:这里面可能会有垃圾代码,太久了没研究了,最近在考试,所以停了,要到11月份可才会再深入研究。
# -*- coding:UTF-8 -*-

#--------------Driver Library-----------------#
import RPi.GPIO as GPIO
import OLED_Driver as OLED
#--------------Image Library---------------#
from PIL  import Image
from PIL import ImageDraw
from PIL import ImageFont
from PIL import ImageColor
import sys
import os
import random
import time
from borax.calendars.lunardate import LunarDate
import json
import re
import requests
#-------------Test Display Functions---------------#
def weather_update():
    global c,d,e,f,filemtime_tianqi
    #下载weather_mini文件
    filetq_city=open('/var/www/html/tianqicity.txt','r')
    citysetup1=filetq_city.readline()
    citysetup=citysetup1.strip('\n')
    filetq_city.close()
    filemtime_tianqi=os.path.getmtime('/var/www/html/tianqicity.txt')
    Download_addres='http://wthrcdn.etouch.cn/weather_mini?city='+citysetup
    #把下载地址发送给requests模块
    f=requests.get(Download_addres)
    #下载文件(原文保存)
    with open("weather_mini","wb") as code:
         code.write(f.content)
    file_open=open("weather_mini",'r')
    file=file_open.readline()
    file_open.close()
    if 'invilad' in file:
        c='城市错误'
        d="错误"
        e="又是错误"
        f="还是错误"
    else:
    #解析weather_mini文件
        dataJson=json.load(open('weather_mini',encoding='UTF-8'))

    #提取天气相关信息
        date=dataJson['data']['forecast'][0]['date']
        tianqi=dataJson['data']['forecast'][0]['type']
        low=dataJson['data']['forecast'][0]['low']
        high=dataJson['data']['forecast'][0]['high']
        fengxiang=dataJson['data']['forecast'][0]['fengxiang']
        fengli=dataJson['data']['forecast'][0]['fengli']
        c=citysetup+':'+tianqi
        d='气温:'+low[3:-1]+'到'+high[3:-1]+'℃'
        e='风力:'+re.findall('.*CDATA\[(.*)]]',fengli)[0]
        f='风向:'+fengxiang

def nianyueri():
    #年月日星期农历位置
    weeks={'Mon':'星期一','Tue':'星期二','Wed':'星期三','Thu':'星期四','Fri':'星期五','Sat':'星期六','Sun':'星期日'}
def bianliang():
    global a,b
    a=time.strftime('%Y年%m月%d日')
    weeks={'Mon':'星期一','Tue':'星期二','Wed':'星期三','Thu':'星期四','Fri':'星期五','Sat':'星期六','Sun':'星期日'}
    d=weeks[time.strftime('%a')]
    today=LunarDate.today()
    b=today.strftime('农历'+'%M月%D'+' '+d)

def Display_Picture(File_Name):
       image = Image.open(File_Name)
       OLED.Display_Image1(image)

def shijian():
    image = Image.new("RGB", (OLED.SSD1351_WIDTH, OLED.SSD1351_HEIGHT), "BLACK")
    draw = ImageDraw.Draw(image)
    #年月日
    draw.text((0,0), a, font = font18, fill = "yellow")
    #农历星期
    draw.text((0,21),b, font = font13, fill = "RED")
    #时间
    draw.text((0,33),time.strftime('%H:%M') , font = font, fill = "BLUE")
    #天气
    draw.text((40,70), c, font = font13, fill = "WHITE")
    draw.text((40,85), d, font = font13, fill = "BROWN")
    draw.text((40,100), e, font = font13, fill = "PURPLE")
    draw.text((40,115), f, font = font13, fill = "PINK")
    OLED.Display_Image(image)
#    Display_Picture("32.jpg")
#    while (True):
#        draw.line([(0, 50), (80, 50)], fill = "WHITE", width = 24)
#        OLED.Display_Image(image)
#        draw.text((0,33),time.strftime('%H:%M') , font = font, fill = "BLUE")
 #       OLED.Display_Image(image)
 #       time.sleep(1)
try:
    OLED.Device_Init()
    font = ImageFont.truetype('cambriab.ttf',30)
    font18 = ImageFont.truetype('SIMYOU.TTF',18)
    font13 = ImageFont.truetype('SIMYOU.TTF',13)
    weather_update() 
    bianliang()
    while (True):
        time.sleep(0.01)
         #更新时间
        shijian()
        if time.strftime('%H%M%S')=='000000' or\
                time.strftime('%H%M%S')=='060000' or\
                time.strftime('%H%M%S')=='120000' or\
                time.strftime('%H%M%S')=='180000':
                    weather_update()



except:
    print("\r\nEnd")
    OLED.Clear_Screen()
    GPIO.cleanup()



最后上一张现在的图,左下角的空余位置本来要上图的,暂时还没成功。

智能互联网时钟 TeaCan2.0代码实例

本文地址:https://blog.csdn.net/kim5659/article/details/107103968