


wdt.init(3600*1000) # 1800000)
disableAlive()
IamAlive()
breakThread = True
time.sleep(4)



# changeFlashSetting('weather_shield_option')
# changeFlashSetting('wifi_enabled')
# resetFlashSettings()
# changeFlashSetting('lte_enabled')
# changeFlashSetting('debugging_messages')


# h2s 49.9kv/A    computedgain = 133.467    150.15na    root=2   U7   IC4 ain0   h2s  range 50ppm
# resp 499Kv/A   computedgain = -44.209     -45.33na   root=3   U6  IC4  ain2   resp   range 20ppm
# o3  499kv/A   computedgain = -46.143    -43.43na     root=0   U4  IC1 ain0    o3  range 5ppm



def testh2s():
    global samplingSequence # cf3/9
    if (samplingSequence & 1) == 1: # cf3/9
        h2s_offset = internalSettings['h2s_offset']
        h2s_root = internalSettings['h2s_root']
        h2s_volt = SPEC_read(h2s_root)  # 2
        print("H2S - SGas= " + str(h2s_volt) + " Off= " + str(h2s_offset))

def tsto3():
    global samplingSequence
    if (samplingSequence & 2) == 2: # cf3/9
        o3_offset = internalSettings['o3_offset']
        o3_root = internalSettings['o3_root']
        o3_volt = SPEC_read(o3_root)
        print("O3 - SGas= " + str(o3_volt) + " Off= " + str(o3_offset))


def tstresp():
    global samplingSequence
    if (samplingSequence & 4) == 4: # cf3/9
        resp_offset = internalSettings['resp_offset']
        resp_root = internalSettings['resp_root']
        resp_volt = SPEC_read(resp_root)
        print("RESP - SGas= " + str(resp_volt) + " Off= " + str(resp_offset))

def testadc(U1=255, U2=10):
    global samplingSequence
    samplingSequence = U1
    for i in range(U2):
        testh2s() # 1  2.2.24
        tsto3() # 2  2.2.24
        tstresp() # 8  2.3.1
        if samplingSequence == 255:
            pass
        else:
            samplingSequence = samplingSequence * 2
            if samplingSequence > 4:
                samplingSequence = 1



'''
readIdxfile_smpLog = 0 # 2.2.26

def testReadfile_smpLog(): # 2.2.26
    global readIdxfile_smpLog

    f = open('/sd/smpLog.txt', 'r')
    f.seek(readIdxfile_smpLog)
    packet = f.readline()
    print("Packet1 L:" + str(len(packet)) + " \\ " + packet )
    packet = f.readline()
    print("Packet2 L:" + str(len(packet)) + " \\ " + packet )
    D1 = f.tell()
    print("D1:" + str(D1))
    readIdxfile_smpLog += D1

    if f:
        print("Close file")
        f.close()
'''





''' passing function as a parameter
def toto(scratch, U1, flag=True):
    if flag:
        print(scratch + ' - U1:' + str(U1))
    pass

def titi(func, U3):
    print("titi")
    U2 = 2
    func("hello", U3)
    pass
'''

''' Time calculator
https://www.calculator.net/time-calculator.html?tcday1=16&tchour1=00&tcminute1=14&tcsecond1=56&Op=-&tcday2=0&tchour2=5&tcminute2=0&tcsecond2=0&tcday3=&tchour3=-4&tcminute3=-45&tcsecond3=-4&ctype=1&x=78&y=27
'''


def testDustSleepCycling():  # test power supply dip when PM wakes up
    for x in range(1000):
        # chrono.reset()
        # chrono.start()
        DIO.writePort(0x03) # hm3300.powerMode(1)
        time.sleep(0.1)
        # S1 = wsoIC3.get_voltage(wsoIC3.read(channel=0, gain=PGA_2_048V))
        # S1 = wsoIC1.get_voltage(wsoIC1.diff(chanPos=2, chanNeg=3, gain=PGA_2_048V)) # U5 CO2 - OK
        # S1 = wsoIC3.get_voltage(wsoIC3.read(channel=2, gain=PGA_2_048V))
        S1 = wsoIC3.get_voltage(wsoIC3.read(channel=0, gain=PGA_2_048V))  # U2 CH4 - Fails sometine
        print(" S1:" + str(S1))
        DIO.writePort(0x01) # hm3300.powerMode(0)
        # S1 = wsoIC3.get_voltage(wsoIC3.read(channel=0, gain=PGA_2_048V))
        # S1 = wsoIC1.get_voltage(wsoIC1.diff(chanPos=2, chanNeg=3, gain=PGA_2_048V)) # U5
        # S1 = wsoIC3.get_voltage(wsoIC3.read(channel=2, gain=PGA_2_048V))
        S1 = wsoIC3.get_voltage(wsoIC3.read(channel=0, gain=PGA_2_048V))  # U2
        print(" S2:" + str(S1))
        time.sleep(0.1)
        # chrono.stop()
        # S1 = chrono.read_ms()
        # print(" T1:" + str(S1))



# GPSFetch(debug=True)


'''
Device Configuration
======================================

FiPy ID: (1001) 0002
Saved 0002.
Air Monitor ID: (1001) 0002
Saved 0002.
PyCom Socket Version: (1 or 2 or 3=PyTrack) 3
Saved as 3.
Weather Shield Option: (0 None, 128 Std, 159 CO2-C1-PID-H2S-Wind) 159
Saved as 159.
Sample Interval: (30s)
Default set - 30
Saved 30.
PID Enabled: (VOC PID - y/n) y
Saved as True.
Light blinks each sample: (y/n) n
Saved as False.
Shutdown Battery Voltage: (3.45)
Default set - 3.45
Saved 3.45.
LoRa Enabled: (external LoRa antenna required - y/n) n
Saved as False.
LTE Enabled: (external LTE antenna required - y/n) y
Saved as True.
LTE Carrier
======================================

 1 : standard
 2 : at&t
 3 : verizon
----------------------------------------
Selection: 3
Saved verizon.
WiFi Enabled: (y/n) n
Saved as False.
WiFi Access Point Enabled: (broadcast wifi - y/n) n
Saved as False.
Debugging messages: (y/n) n
Saved as False.

Configurator task complete.
'''



'''
configurator.writeVRAM('co2_offset', 0.1)
configurator.writeVRAM('c1_offset', 0.1)
configurator.writeVRAM('co2_gain', 26315.789)
configurator.writeVRAM('c1_gain', 26315.789)


configurator.writeFlashKey('weather_shield_option', 159)
configurator.writeFlashKey('weather_shield_option', 144) # WS with Std sensors + Wind sensor only

configurator.writeFlashKey('debugging_messages', False)

MFactor_SPEC(150.15, 49.9)
configurator.writeVRAM('h2s_gain', 133.4671)


'''

# [54, 64, 65, 72, 88, 118]
#  54 = $36  fuel gauge

'''
sensorsPacket

def pushSensorValue
'''

'''
sData = wsCommSw.i2c1.readfrom(0x70, 1)
U1 = ustruct.unpack('@B', sData)[0]
print('PI4M read: 0x{0:04X}'.format(U1))


print(wsCommSw.i2c1.scan())
[56, 65, 72, 73, 74, 75, 112]

'''

"""
# main.py 169
nvsKeys = ['battery_charge', 'pid_offset', 'pid_gain', 'tvoc_base', 'co2_base']
internalSettings
configurator.writeVRAM('battery_charge', battCsum)


# configurator.py
configurator.FLASH_CONFIG
deviceSettings = FLASH_CONFIG
deviceSettings['light_blinks']

deviceSettings['debugging_messages'] = False
configurator.writeFlashKey('debugging_messages',False)

configurator.writeFlashKey('light_blinks',False)
configurator.writeFlashKey('lte_enabled', True)
configurator.writeFlashKey('wifi_enabled', False)
configurator.writeFlashKey('pycom_socket_version', 3)


configurator.writeFlashKey('lora_enabled', False)

changeFlashSetting('pycom_socket_version')
changeFlashSetting('debugging_messages')

changeFlashSetting('send_interval')

configurator.clearFlashConfig()
configurator.runFlashSetup()

configurator.writeFlashKey('wifi_pass', "Home-24att")
configurator.writeFlashKey('wifi_ssid', "acqsol2302please")
configurator.writeFlashKey('wifi_antenna', False)
configurator.writeFlashKey('ap_enabled', True)


changeFlashSetting('wifi_enabled')



"""


''' 3b
Wind speed sensor (reed switch - p14 Dig input)
Wind direction sensor (potentiometre - p13 ADC)
http://www.vleo.net/i2c-anemometer-cheap-wind-data-logger/

from machine import Timer
import time

chrono = Timer.Chrono()

for the next 2 second read input switch
exit loop when switch is closed
if switch detectected
    chrono.start()
    for the next 2 second read input switch
    exit loop when switch is closed
    lap = chrono.read() # time it took for the wheel to make a full rotation
    compute wind speed
else
    not enough wind to get measure
chrono.stop()

'''


'''
import sys
sys.stdin.read(1)

import select
select.poll([sys.stdin],1)
'''
