0

I am new to coding. I have this project that I want to get done, so this is daunting. I had someone else write this, but my LCD was DOA, so I want to switch to an OLED. Can anyone help me transcribe this code from LCD display to OLED? I made some progress, I think, but I'm not entirely sure what to do next. I've setup the header to import the SSD1306 python file for the OLED and changed the options for the I2C to the settings for the OLED display. I've set the pins and I've changed what happens when I2C runs to what I think is appropriate for this OLED display, but I'm not sure. And later on there are some codes that control what is written to the display, but this is where I am out of my depth. I don't know what to change at the lcd_print stuff to make it do the same thing, but on an 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()
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • The documentation for the `ssd1306` module is [here](https://docs.micropython.org/en/latest/esp8266/quickref.html#ssd1306-driver) - that's for the ESP8266 microcontroller board, not the Raspberry Pi Pico which you have tagged, but I expect they are the same. What LCD were you trying to use and what driver module did your code use before - e.g. what did the line starting `lcd = ` say before you changed it? – nekomatic Jan 26 '22 at 23:04
  • I think I figured it out. I had to add a position to lcd.text. I just have it lcd.text(keyword, 0, 0, 1) and it makes the screen display stuff and it seems to work now. – Rob Probasco Jan 27 '22 at 13:34

0 Answers0