# File name: kcpu.py
# Copyright: The AustSTEM Foundation Limited
# Author: Tony Strasser
# Date created: 25 Jun 2019
# Date last modified: 25 Jun 2019
# MicroPython Version: 1.10 for the Kookaberry V4-05
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation.
# To the fullest extent permitted by law, AustSTEM absolutely disclaims 
# all warranties, expressed or implied, including, but not limited to, 
# implied warranties of merchantability and fitness for any particular purpose. 
# AustSTEM gives no warranty that this software will be free of errors, 
# or that defects in the software will be corrected,  
# See the GNU General Public License for more details.
#
# Module to measure and monitor key Kookaberry operating parameters
# kcpu.battery_volts() - measures the power supply voltage
# kcpu.battery_ok()- compares the supply voltage to a preset threshold and returns True or False
# kcpu.vref() - measures the CPU core voltage
# kcpu.vbat() - measures the CPU supply voltage (usually 0.2V below power supply)
# kcpu.temp() - measures the CPU core temperature in degrees Celsius
# Usage:
    #    import kcpu
    #    if kcpu.battery_ok(): do something
    #    else: print('Battery is low')
    
# Begin code

# Initial conditions
from _kooka import ADCAll
battery_low = 2.7    # low battery threshhold
adc = ADCAll(12, 0xf0000)  # create the analogue input for kooka cpu variables
        
def battery_volts():
    return adc.read_vref()    # Kookaberry supply voltage
        
def battery_ok():    # sets flag True if battery sufficient else False
    if adc.read_vref() >= battery_low: return True
    else: return False
        
def vref():
    return adc.read_core_vref()    # Kookaberry core voltage
     
def vbat():
    return adc.read_core_vbat()    # Kookaberry core supply voltage

def temp():
    return adc.read_core_temp()    # Kookaberry cpu temperature deg C


