# Authored by Jonathan Scott James the great On WOKWI
# LCD 1602 8 bit bitbang For general purpose interface as car tachometer voltage meter chronometer frequency counter waveform monitor for generator
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(26, machine.Pin.IN) #ADC
machine.Pin(27, machine.Pin.IN, machine.Pin.PULL_DOWN) #key 2 bottom key
machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN) #key 1 top key
ttt=1;t=3600*24*366+28;t=3600*24*22-9 # python sets the precisio level when you first assign the number according to what will fit in the value you assign it to.
def tickjsj():
    global t
    t=t+1
machine.Timer().init(period=1000, callback=lambda t:tickjsj())
# LCD1602 initialization sequence
# rs rw d0 d1 d2 d3 d4 d5 d6 d7
# 0  0  0  0  1  1  x  x  x  x
#
# 0  0  0  0  1  0  x  x  x  x
# 0  0  n  f  x  x  x  x  x  x 
#
# 0  0  0  0  1  0  x  x  x  x
# 0  0  n  f  x  x  x  x  x  x (#N=1:2lines;N=0:1lines  .F=1:5x11dots;F=0:5x8dots.)
#
# 0  0  0  0  0  0  x  x  x  x
# 0  1  d  c  b  x  x  x  x  x ((D)Display, (C)Cursor, (B)Blink
#
# 0  0  0  0  0  0  x  x  x  x
# 0  0  0  0  0  1  x  x  x  x (clear display instruction)
#
# 0  0  0  0  0  0  x  x  x  x
# 0  0  1 I/D SH X  X  X  X  x


#instructions abd their formats
#***************************
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  1  DL N  F  x  x
#(DL).  DL=1:8bits;0:4bits.
#(N) N=1:2lines;N=0:1lines.
#(F). F=1:5x11dots ; F=0:5x8dots.
# 000010xxxx
#
# machine.Pin(8).value(0)  # Enable
# machine.Pin(9).value(0)  # R/W 1=R 0=W
# machine.Pin(10).value(0) # RS
# machine.Pin(0).value(0)
# machine.Pin(1).value(0)
# machine.Pin(2).value(1)
# machine.Pin(3).value(1)
# machine.Pin(4).value(0)
# machine.Pin(5).value(0)
# machine.Pin(6).value(0)
# machine.Pin(7).value(0)
#*****************************
#****************************
# This is Bad because it is the 4bit mode.Need to rewrite In 8 bit mode.
# https://hades.mech.northwestern.edu/images/f/f7/LCD16x2_HJ1602A.pdf
# SEARCH FOR "15.24-bitoperation(4-bit,1line)""
# *********************************************
# LCD1602 initialization sequence

# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  1  1  x  x  x  x
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(0)
machine.Pin(1).value(0)
machine.Pin(2).value(1)
machine.Pin(3).value(1)
machine.Pin(8).value(1)  # Enable 1=Enable
machine.Pin(8).value(0)  # Enable 1=Enable

#
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  1  0  x  x  x  x
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(0)
machine.Pin(1).value(0)
machine.Pin(2).value(1)
machine.Pin(3).value(0)
machine.Pin(8).value(1)  # Enable 1=Enable
machine.Pin(8).value(0)  # Enable 1=Enable

# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  n  f  x  x  x  x  x  x 
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(1)
machine.Pin(1).value(1)
machine.Pin(8).value(1)  # Enable 1=Enable
machine.Pin(8).value(0)  # Enable 1=Enable
#
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  1  0  x  x  x  x
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(0)
machine.Pin(1).value(0)
machine.Pin(2).value(1)
machine.Pin(3).value(0)
machine.Pin(8).value(1)  # Enable 1=Enable
machine.Pin(8).value(0)  # Enable 1=Enable

# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  n  f  x  x  x  x  x  x (#N=1:2lines;N=0:1lines  .F=1:5x11dots;F=0:5x8dots.)
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(1)
machine.Pin(1).value(1)
machine.Pin(8).value(1)  # Enable 1=Enable
machine.Pin(8).value(0)  # Enable 1=Enable
#
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  0  0  x  x  x  x
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(0)
machine.Pin(1).value(0)
machine.Pin(2).value(0)
machine.Pin(3).value(0)
machine.Pin(8).value(1)  # Enable 1=Enable
machine.Pin(8).value(0)  # Enable 1=Enable
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  1  d  c  b  x  x  x  x  x ((D)Display, (C)Cursor, (B)Blink
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(1) # RS
machine.Pin(0).value(1)
machine.Pin(1).value(1)
machine.Pin(2).value(8)
machine.Pin(8).value(1)  # Enable
machine.Pin(8).value(0)  # Enable
#
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  0  0  x  x  x  x
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(0)
machine.Pin(1).value(0)
machine.Pin(2).value(0)
machine.Pin(8).value(1)  # Enable
machine.Pin(8).value(0)  # Enable
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  0  1  x  x  x  x (clear display instruction)
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(0)
machine.Pin(1).value(0)
machine.Pin(2).value(1)
machine.Pin(8).value(1)  # Enable
machine.Pin(8).value(0)  # Enable
#
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  0  0  0  0  x  x  x  x
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(0)
machine.Pin(1).value(0)
machine.Pin(2).value(0)
machine.Pin(8).value(1)  # Enable
machine.Pin(8).value(0)  # Enable
# rs rw d0 d1 d2 d3 d4 d5 d6 d7   # R/W 1=R 0=W
# 0  0  1 I/D SH X  X  X  X  x
machine.Pin(0, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit0
machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit1 
machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit2
machine.Pin(3, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit3
machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit4
machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit5
machine.Pin(6, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit6
machine.Pin(7, machine.Pin.OUT, machine.Pin.PULL_DOWN) #data bit7
machine.Pin(8, machine.Pin.OUT, machine.Pin.PULL_DOWN) #enable
machine.Pin(9, machine.Pin.OUT, machine.Pin.PULL_DOWN) #read/weite 
machine.Pin(10, machine.Pin.OUT, machine.Pin.PULL_DOWN) #register select
machine.Pin(9).value(0)  # R/W 1=R 0=W
machine.Pin(10).value(0) # RS
machine.Pin(0).value(0)
machine.Pin(1).value(0)
machine.Pin(2).value(0)
machine.Pin(8).value(1)  # Enable
machine.Pin(8).value(0)  # Enable
#******************************************
# LCD1602 initialization sequence
# rs rw d0 d1 d2 d3 d4 d5 d6 d7
# 0  0  0  0  1  1  x  x  x  x
#
# 0  0  0  0  1  0  x  x  x  x
# 0  0  n  f  x  x  x  x  x  x 
#
# 0  0  0  0  1  0  x  x  x  x
# 0  0  n  f  x  x  x  x  x  x (#N=1:2lines;N=0:1lines  .F=1:5x11dots;F=0:5x8dots.)
#
# 0  0  0  0  0  0  x  x  x  x
# 0  1  d  c  b  x  x  x  x  x ((D)Display, (C)Cursor, (B)Blink
#
# 0  0  0  0  0  0  x  x  x  x
# 0  0  0  0  0  1  x  x  x  x (clear display instruction)
#
# 0  0  0  0  0  0  x  x  x  x
# 0  0  1 I/D SH X  X  X  X  x

# nn=3600*24*366*28;m=nn;d=nn;s=nn;nn=1;b2b=0;d=12;k=0;b=0;kk=0;bb=0;sevenSeg="1111110011000011011011111001011001110110111011111111000011111111111011";
tt=" "
eightbitsevensegreversed="00111111000001100101101101001111011001100110110101111101000001110111111101101111"
ssebr=eightbitsevensegreversed
k=0;b=0;k2=0;b2=0
puter=bytearray(1)
while 1:
  k=machine.Pin(28).value()
  if (k>b):
    k1=k1+1-4*(k1>3)
  b=k
  k2=machine.Pin(27).value()
  if (k2>b2):
    k1=k1+1-4*(k1>3)
  b2=k2
  rvdc=machine.ADC(26).read_u16()
  if ttt==t:
    print(end="")
    #machine.lightsleep(100)
  if ttt!=t:
    ttt=t
    s=str(int((t/60-t//60)*60));m=str(int((t/3600-t//3600)*60));h=str(int((t/86400-t//86400)*24));d=str(t//86400);w=str((t//86400*7))    # Extrapolate seconds hours minutes and days from raw uptime seconds
    tt=("00"+d)[-2:]+("00"+h)[-2:]+("00"+m)[-2:]+("00"+s)[-2:]+"77" # compile the output buffer 
    for nn in range(8):
      n=int(tt[nn])
      machine.SPI(0,baudrate=1024,sck=machine.Pin(2),mosi=machine.Pin(3),miso=machine.Pin(4)).readinto(puter,int(ssebr[n*8:n*8+8],2))
      chrout=str(puter)[12]
      if chrout==chr(92):
        print(ord(chr(int("0x"+(str(puter)[14:16])))),"$",end="")
      if chrout!=chr(92):
        print(ord(str(puter)[12]),"@",end="")
    machine.Pin(0).value(0)    # low latched the data into the display
    machine.Pin(0).value(1)    # high latched the data into the display
    print("")
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT