# Scale Calibration
# Load Cell 5kg
# HX711 amplifier
# RP2040 lcd 0.96
# MicroPython v1.15 on 2021-04-18

import os
import sys
from machine import Pin
import time
import math

# HX711 setup
from hx711 import HX711

#LCD setup
#color is BGR
RED = 0x00F8
GREEN = 0xE007
BLUE = 0x1F00
WHITE = 0xFFFF
BLACK = 0x0000
YELLOW = 0x00FF

from lcd096 import LCD_0inch96
lcd = LCD_0inch96()

# button assignments
Button_D = Pin(21, Pin.IN, Pin.PULL_DOWN) # Scale Mode 0=Wait, 1=Cal

calfac = 0 # calibration factor

def Weigh():
    global hx
    m=hx.masse(10)
    print(m)

    state=(test.value() == 0)
    time.sleep(0.5)
    
    if m <= 1:
        m = 0
    
    grams = m
    return grams

def Results():
    global calfac
    NetWeight = float(Weigh())

    if calfac == 0:
        calfac, NetWeight = bsa(812) # numeric passing parameter is the weight of a verified reference
        print (calfac, NetWeight)
        
    lcd.fill(BLACK)
    lcd.text("Calibrated",24,20,GREEN)
    lcd.text(str(NetWeight)[0:7]+" g",26,50,GREEN)
    lcd.text(str(calfac)[0:7]+" cal factor",26,60,GREEN)
    lcd.display()
    return

def bsa(target, tolerance=0.5):
    # binary_search_approximation
    low, high = 0, target
    while (high - low) > tolerance:
        caloff = (low + high) / 2
        mid = hx.massraw(10)/caloff
        if mid >= target:
            low = caloff
        else:
            high = caloff
        # print ((high - low), ((low + high) / 2), mid )   
    return int((low + high) / 2), mid

lcd.fill(BLACK)
lcd.display()

lcd.text("Remove weight now!",5,20,GREEN)
# countdown delay
for i in range(10, -1, -1):
    lcd.fill_rect(5,40,160,8,BLACK) # x,y,width,height,colour
    lcd.text(str(i)+" Sec",60,40,GREEN)
    lcd.display()
    time.sleep(1)
    
lcd.fill(BLACK)

# hx711 initialization
dout=Pin(0)
dpclk=Pin(1)
delta = 0
test=Pin(0,Pin.IN,Pin.PULL_UP)

try:
    lcd.text("Initializing",24,40,GREEN)
    lcd.display()
   
    hx = HX711(dout,dpclk,405) # starting point to search for calibration reference
    hx.wakeUp()
    hx.kanal(1)
    hx.tara(25)
    hx.calFaktor(hx.calFaktor()+delta)
    # print("Start")
    
    lcd.fill(BLACK)    
    lcd.text("Set D to Weigh",24,40,GREEN)
    lcd.display()

except:  
    # print("HX711 failed")
    lcd.fill(BLACK)
    lcd.text("HX711 failed",24,42,RED)
    lcd.display()
    sys.exit() 

if __name__=='__main__':   
    
    lcd.fill(BLACK)
    while (calfac == 0):
        if Button_D.value() == 1:
            lcd.fill(BLACK)    
            lcd.text("Calibrating",24,40,GREEN)
            lcd.display() 
            Results()
          