数码之家

 找回密码
 立即注册
搜索
查看: 1601|回复: 0

[Arduino] [esp8266_micropython]oled ssd1306的使用

[复制链接]
发表于 2020-7-14 20:57:48 | 显示全部楼层 |阅读模式
前置贴
[esp8266_micropython] oled ssd1306的简单使用  https://www.mydigit.cn/forum.php?mod=viewthread&tid=175019&extra=
[micropython_python]黑白BMP图取模转为十六进制数组 ...  https://www.mydigit.cn/forum.php?mod=viewthread&tid=177574
[esp8266_micropython] ssd1306绘制直线:https://www.mydigit.cn/forum.php?mod=viewthread&tid=221691

代码

main.py

import network
from machine import Pin, I2C
import time
import urequests as requests
from ssd1306 import SSD1306_I2C
#from logo import wifi_logo
#from logo import ff_arr
import logo


#print ("ligth it")
#for i in range(0,64):
#  for j in range(0,128):
#    oled.pixel(j,i,1)
#    oled.show()


def connectionWifi(ssid, password):
  wlan = network.WLAN(network.STA_IF)
  wlan.active(True)
  wlan.connect(ssid, password)
  while True:
    if not wlan.isconnected():
      time.sleep_ms(2000)
    else:
      print('connected to network')
      return wlan.ifconfig()
      break

def getJsonInfoFromURL(url):
  response = requests.get(url)
  return response.json()
  
def light_dot(x,y):
  oled.pixel(x,y,1)
  

def dark_dot(x,y):
  oled.pixel(x,y,0)

def my_str(num):
  if num < 10:
    return "0"+str(num)
  
  return str(num)

def time_str_to_num(time_str, i):
  if time_str == ":":
    return 10
  else:
    return int(time_str)

if __name__ == '__main__':
  
  i2c = I2C(scl=Pin(5), sda=Pin(4))
  oled = SSD1306_I2C(128, 64, i2c)
  
  
  steps = 128 * 64
  direction_mode = 0
  step = 0
  x=0
  y=0
  
  flag  = 1
  while ( step <= 256 ):
    light_dot(x,y)
   
    if (flag==1):
      if ( x >= 127):
        direction_mode = 1
      if ( x>= 127 and y >= 63):
        direction_mode = 2
      if ( x <=0 and y>=63):
        direction_mode = 3
      if ( x == 0 and y == 1 ):
        print ('flag 2')
        flag = 2
        direction_mode = 0
        break

    if(direction_mode == 0):
      x = x+1
    elif (direction_mode == 1):
      y= y+1
    elif (direction_mode == 2):
      x = x-1
    elif (direction_mode == 3):
      y = y-1
  
  oled.show()
  
  line_y = 40
  for x in range(0,128):
    light_dot(x, line_y)
   
  oled.show()
  
  line_x=90
  for y in range (0,line_y):
    light_dot(line_x, line_y-y)
  
  oled.show()
  
  print ("done")

  ssid='FAST_20CC'
  password='409409409'
  
  oled.text("wifi module loading..",5,50)

  print ("connectionWifi")
  ifconfig = connectionWifi(ssid, password)
  
  for i in range(1,127):
    for j in range(line_y+1,63):
      dark_dot(i,j)
  oled.show()  
  
  print ("print ip")
  
  oled.text(""+ifconfig[0],5,50)

#  network logo  
#  for y in range(8, 35,3):
#    for x in range(98, 120,2):
#      if ( (x+y) < 129):
#        light_dot(x,y)
#        oled.show()

# wifi logo
  for i in range (len(logo.wifi_logo)):
    for j in range (len(logo.wifi_logo)):
      oled.pixel(j+line_x+5,i+2,logo.wifi_logo[j])
    oled.show()
   
  while 1:  
    try:
      result = getJsonInfoFromURL("http://ip:port/bills/ESP8266TimeAPI")
      print (result)

      for x in range(1,89):
        for y in range(1,39):
          dark_dot(x,y)
      oled.show()

      #oled.text(my_str(result['hours'])+":"+my_str(result['minutes']),5,10)

      time_str = my_str(result['hours'])+":"+my_str(result['minutes'])
      time_x = 9
      time_y = 5
      for i in range(len(time_str)):
        current_x = time_x+i*8*2
        #current_x = time_x+i*10
        current_y = time_y

        #print (time_x+i*8, time_y, time_str)
        current_num = time_str_to_num(time_str, i)
        num_arr= logo.ff_arr[current_num*16:current_num*16+16]
        #print (num_arr)

        for j in range(len(num_arr)):
          now_x = current_x
          now_y = current_y+j*2
          #now_y = current_y+j
         
          #binary_str = bin(int(num_arr[j], 16))
          binary_str = '{:08b}'.format(int(num_arr[j],16))
          #print (binary_str)
          for k in range(0,len(binary_str)):
            if ( binary_str[k] == "0" ):
              oled.pixel(now_x+k, now_y, 0)
              oled.pixel(now_x+k+k, now_y, 0)  
            else:
              oled.pixel(now_x+k, now_y, 1)
      oled.show()  


      time.sleep_ms(15000)
      gc.collect()
    except:
      print ("except")

效果图如下




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

打赏

参与人数 1家元 +30 收起 理由
家睦 + 30

查看全部打赏

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2024-4-26 15:27 , Processed in 0.109200 second(s), 11 queries , Redis On.

Powered by Discuz!

© 2006-2023 smzj.net

快速回复 返回顶部 返回列表