this code is verz dependent on timing of incoming requests
from machine import UART, Pin, I2C
from ssd1306 import SSD1306_I2C
import _thread
import random
import framebuf
import utime
from oled import Write, GFX, SSD1306_I2C
from oled.fonts import ubuntu_mono_15, ubuntu_mono_20

uart = UART(1, timeout=100, baudrate=56000, tx=Pin(4), rx=Pin(5), bits=8, parity=None, stop=1)
led = Pin(25, Pin.OUT)

#value inc button up
butPinValueUp=14
myButtonValueUp=Pin(butPinValueUp, Pin.IN, Pin.PULL_UP)

#value dec button down
butPinValueDown=13
myButtonValueDown=Pin(butPinValueDown, Pin.IN, Pin.PULL_UP)

#value dec button down
butPinValueReset=10
myButtonValueReset=Pin(butPinValueReset, Pin.IN, Pin.PULL_UP)

i2c = I2C(0, sda = Pin(0), scl = Pin(1), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
gfx = GFX(128, 64, oled.pixel)

#initial values for data values and inc/dec values
cellValue=100000
incValue=100
randValue=9
data="awaiting input"

#fonts
write15 = Write(oled, ubuntu_mono_15)
write20 = Write(oled, ubuntu_mono_20)

oled.fill(0)
write20.text("JMS AERO AG", 7, 0)
write20.text("Data Logger", 7, 20)
write20.text("Simulator", 19, 40)
oled.show()

utime.sleep(5)

for i in range(64):
    oled.scroll(0, -2)
    oled.show()
    utime.sleep_ms(10)


#checks which button has been pressed, runs in its own thread
def checkButtons():
     
    global cellValue
    global incValue
    global oled
    global data
    
    while True:
        
        oled.show()

        oled.fill(0)
             
        oled.text(str(data),0,0)
        
        data="awaiting input"

        #write20.text(str(data),0,0)

        write20.text(str(cellValue)+" Kg",10 ,10)    
        write15.text(str(cellValue* 0.00980665)+" kN",10,26)
        write15.text(str(cellValue* 0.001)+" T",10,38)
        write15.text(str(cellValue* 0.001102317)+" US tn",10,50)

        butStateUp=myButtonValueUp.value()
    
        if butStateUp==0:
            print("Up Pressed")
            utime.sleep(.2)
            cellValue=cellValue+incValue
        
        butStateDown=myButtonValueDown.value()
    
        if butStateDown==0:
            print("Down Pressed")
            utime.sleep(.2)
            cellValue=cellValue-incValue
    
        butStateReset=myButtonValueReset.value()
    
        if butStateReset==0:
            print("Reset Pressed")
            cellValue=100000
            utime.sleep(.2)

        
_thread.start_new_thread(checkButtons,())


####################################################################################

#data transfer
while True:
    #global oled

    if uart.any() == True:

        data = uart.readline() 
        print(data)
        
        if data == b'<<INITIALIZE\r\n':
            led.toggle() 
            #uart.write("<<INITIALIZE\r\n")
        
        if data == b'<<CANCEL\r\n':
            led.toggle() 
            #uart.write("<<CANCEL\r\n")
            
        if data == b'<<START TEST\r\n':
            led.toggle() 
            #uart.write("<<START TEST\r\n")
        
        if data == b'wei.zrp\r\n':
            led.toggle() 
            uart.write("Zero Emulated\r\n")
            utime.sleep(2)

        if data == b'wei.mdf\r\n':
            led.toggle()

            gfx.fill_rect(120, 0, 8, 8, 1)

            
            from random import randrange
            randEndValue=(randrange(randValue))
            print ("wei.mdf.A:RAW:+  20617;1;ST;GS;GRO:+  " + str(cellValue) + ";NET:+  0.000; TAR:+  0.000;" + str(randEndValue) + "e\r\n")
            uart.write ("wei.mdf.A:RAW:+  20617;1;ST;GS;GRO:+  " + str(cellValue) + ";NET:+  0.000; TAR:+  0.000;" + str(randEndValue) + "e\r\n")


solutions