0

我是编码新手。我有一个想要完成的项目,所以这令人生畏。我让别人写了这个,但我的 LCD 是 DOA,所以我想切换到 OLED。谁能帮我把这段代码从 LCD 显示器转录到 OLED?我想我取得了一些进展,但我不完全确定下一步该做什么。我已经设置了头文件以导入 OLED 的 SSD1306 python 文件,并将 I2C 的选项更改为 OLED 显示器的设置。我已经设置了引脚,并且我已经更改了 I2C 运行到我认为适合此 OLED 显示器时发生的情况,但我不确定。后来有一些代码可以控制写入显示器的内容,但这是我无法理解的地方。我不知道在 lcd_print 的东西上要改变什么才能让它做同样的事情,但是在 OLED 上......

from machine import I2C, Pin
import ssd1306
import utime
import machine

import sys


relay_pin = 6
relay = Pin(relay_pin, Pin.OUT)

i2c = I2C(0, sda=Pin(0), scl=Pin(1))
def lcd_print(keyword):
  if keyword is not str:
    keyword=str(keyword)
  I2C_ADDR     = 0x3c


  i2c = I2C(0, sda=Pin(0), scl=Pin(1))
  lcd = ssd1306.SSD1306_I2C(128, 32, i2c)
  lcd.text(keyword)
  lcd.show()


#keypad

key_map=[["D","#","0","*"],\
        ["C","9","8","7"],\
        ["B","6","5","4"],\
        ["A","3","2","1"]]

col_list=[2,3,4,5]
row_list=[8,9,10,11]

for x in range(0,4):
  row_list[x]=Pin(row_list[x], Pin.OUT)
  row_list[x].value(1)


for x in range(0,4):
  col_list[x] = Pin(col_list[x], Pin.IN, Pin.PULL_UP)


def Keypad4x4Read(cols,rows):
  for r in rows:
    r.value(0)
    result=[cols[0].value(),cols[1].value(),cols[2].value(),cols[3].value()]

    if min(result)==0:
      key=key_map[int(rows.index(r))][int(result.index(0))]
      r.value(1) # manages key keept pressed
      return(key)
    r.value(1)


def send_pulse(n:int):
  for i in range(n):
    relay(1)
    utime.sleep(0.005)
    relay(0)
    utime.sleep(0.015)

def save2file(data_:str):
  f = open("data.txt", "w")
  f.write(data_)
  f.close()

def read_from_file():
  f = open("data.txt","r")
  data_ = f.read()
  f.close()
  return data_


def check_int(string_:str): #Checks if String content is a numeric value
  if string_ is not None:
    numerics="0123456789"
    for i in string_:
      if not i in numerics:
        return False
    return True

try:
  val = read_from_file()
except:
  val = "00000"

def get_order():
  temp=""
  def fill_zeros(string_input:str, max_len:int): 
    str_len = len(string_input)
    zeros = "0" * (max_len-str_len)
    return zeros
  global val
  lcd_print(fill_zeros(val,5)+val[0:-1]+"."+val[-1]+"\n")
  while True:
    
    key = Keypad4x4Read(col_list,row_list)
    if key != None and key != '#':
      temp=temp+key
      utime.sleep(0.3)
      lcd_print("\n"+temp)

    if key == "#" and temp  != "":
      if temp[0]=='A' and temp[1:]==val and check_int(temp[1:]):
        pulse_times=100000-int(temp[1:])
        lcd_print("R"+fill_zeros(temp,5)+val[0:-1]+"."+val[-1]+"\n")
        send_pulse(pulse_times)
        val ="0000.0"
        lcd_print("0000.0")
        save2file(val)
        temp=""
        
      elif check_int(temp):
        if int(temp)>99999:
          lcd_print("\nOUT OF RANGE") #The entered value is out of range
          temp=""
          utime.sleep(1)
          lcd_print(val[0:-1]+"."+val[-1]+"\n")
          continue
        val = temp
        lcd_print(fill_zeros(temp,5)+val[0:-1]+"."+val[-1]+"\nSENDING PULSE")
        send_pulse(int(val))
        save2file(val)
        lcd_print(fill_zeros(temp,5)+val[0:-1]+"."+val[-1])
        temp=""
      
      elif temp == "B":
        save2file(val)
        lcd_print("DATA SAVED...\nSYSTEM PAUSED...")
        temp=""
      elif temp == "C":
        val = "00000"
        save2file(val)
        temp=""
        lcd_print("...RESET...")
        sys.exit()

      else:
        lcd_print("UNDEFINED \nCOMMAND...")
        temp=""
        utime.sleep(1)
        lcd_print(fill_zeros(temp,5)+val[0:-1]+"."+val[-1])

get_order()
4

0 回答 0