'''A demonstration program for GCBASIC.
// ''--------------------------------------------------------------------------------
// ''This program Spectral_Sensor_AS726X
// ''
// ''
// ''@author    EvanV   
// ''@license   GPL
// ''@version   1.00   
// ''@date      2024-04-24
// ''********************************************************************************

#chip 18F26K22
#option Explicit

#include <as726x.h>

// ' -------------------PORTA----------------
// ' Bit#:  -7---6---5---4---3---2---1---0---
// ' IO:    ---------------------------------
// '-----------------------------------------
// '

// ' -------------------PORTB----------------
// ' Bit#:  -7---6---5---4---3---2---1---0---
// ' IO:    ---------------------------------
// '-----------------------------------------
// '

// ' ------------------PORTC-----------------
// ' Bit#:  -7---6---5---4---3---2---1---0---
// ' IO:    ---| TX|---|SDA|CLK|-------------
// '-----------------------------------------
// '



// *****************************************************************************************************
// Main program commences here.. everything before this is setup for the board.


// Setup Serial port
#DEFINE USART_BAUD_RATE 9600
#DEFINE USART_TX_BLOCKING


//  Define I2C settings
#DEFINE HI2C_BAUD_RATE 100
#DEFINE HI2C_DATA PORTC.4
#DEFINE HI2C_CLOCK PORTC.3
// Initialise I2C
// I2C pins need to be input for SSP module - this is not an option! However, your specific chip ( if a new PIC ) will need these as outputs
Dir HI2C_DATA In
Dir HI2C_CLOCK In
// MASTER
HI2CMode Master

//This is not required as this is the default.  Here for clarity only.
#DEFINE AS726X_ADDRESS 0x92  

// ************************************************* Main body of program commences here.****************************************************

    // #DEFINE SHOWSINGLENUMBERS
    HserPrintCRLF
    HSerPrintStringCRLF "AS7262 Spectral Sensor"
    HserPrintCRLF


    HserPrint "AS726XSensorVersion : "
    HSerPrintStringCRLF ByteToHex(AS726XSensorVersion)
    
    HserPrint "AS726XSensorState   : "
    HSerPrintStringCRLF ByteToHex(AS726XSensorState)

    HserPrint "AS726xError         : "
    HSerPrintStringCRLF ByteToHex(AS726xError)

    AS726XSensorVersion = AS726x_VirtualReadRegister( AS726X_HW_VERSION )

    Do

        HSerSend 1
        HserPrintCRLF

        AS726X_TakeMeasurementsWithBulb

        Dim ColorChannel as Byte
        HSerPrintStringCRLF "16Bit color reads"
        HSerPrintStringCRLF "V	B	G	Y	O	R"
        For ColorChannel = AS7262_V to AS7262_R Step 2
            HserPrint AS726x_GetChannel(ColorChannel)
            HserSend 9
        Next
        HserPrintCRLF 2


        HserPrint "AS726XSensor : 0x"
            HserPrint ByteToHex(AS726XSensorVersion)

        #IFDEF SHOWSINGLENUMBERS
            Select Case AS726XSensorVersion
            
            Dim myAS7262Value as Single
            Case SENSORTYPE_AS7262
                HSerPrintStringCRLF " AS7262"
                HserPrint "Violet "
                    myAS7262Value = AS726X_GetCalibrated(AS7262_V_CAL)
                    HSerPrintStringCRLF SingleToString( myAS7262Value )
                HserPrint "Blue   "
                    HSerPrintStringCRLF SingleToString( AS726X_GetCalibrated(AS7262_B_CAL) )
                HserPrint "Green  "
                    HSerPrintStringCRLF SingleToString( AS726X_GetCalibrated(AS7262_G_CAL) )
                HserPrint "Yellow "
                    HSerPrintStringCRLF SingleToString( AS726X_GetCalibrated(AS7262_Y_CAL) )
                HserPrint "Orange "
                    HSerPrintStringCRLF SingleToString( AS726X_GetCalibrated(AS7262_O_CAL) )
                HserPrint "Red    "
                    HSerPrintStringCRLF SingleToString( AS726X_GetCalibrated(AS7262_R_CAL) )

            Case SENSORTYPE_AS7263
                HSerPrintStringCRLF " AS7263"

                HserPrint "AS7263_W_CAL "
                    HSerPrintStringCRLF SingleToString( AS726X_GetCalibrated(AS7263_W_CAL) )
                HserPrint "AS7263_V_CAL   "
                    HSerPrintStringCRLF SingleToString( AS726X_GetCalibrated(AS7263_V_CAL) )
                HserPrint "AS7263_U_CAL  "
                    HSerPrintStringCRLF SingleToString( AS726X_GetCalibrated(AS7263_U_CAL) )
                HserPrint "AS7263_T_CAL"
                    HSerPrintStringCRLF SingleToString( AS726X_GetCalibrated(AS7263_T_CAL) )
                HserPrint "AS7263_S_CAL"
                    HSerPrintStringCRLF SingleToString( AS726X_GetCalibrated(AS7263_S_CAL) )
                HserPrint "AS7263_R_CAL"
                    HSerPrintStringCRLF SingleToString( AS726X_GetCalibrated(AS7263_R_CAL) )            
            Case Else
                HSerPrintStringCRLF " Unknown Sensor"
            End Select
        #ENDIF

        Hserprint "Temperature "
            Dim temperatureC, decpoint as Byte
            Dim temperatureFString as String * 24
            
            temperatureC = AS726X_GetTemperature
            Hserprint temperatureC  // this is byte 
            HserPrint "c: "

            #IFDEF SHOWSINGLENUMBERS
                Dim temperatureF as Single

                // Now convert to F to formatted string
                temperatureFString = ""
                // This is a ByteToSingle conversion then a conversion`
                temperatureF = ( [Single]temperatureC * 1.8 ) + 32

                temperatureFString = SingleToString(temperatureF)
                // temperatureFString = ltrim(temperatureFString)
                // Search for decimal point and set the string length, saves ~200 words, and ~50 bytes of RAM over instr()        
                    for decpoint = 1 to 16
                        if  temperatureFString(decpoint) = "." then 
                            temperatureFString(0) = decpoint - 1        
                            exit for
                        end if
                    next
                // Optional...
                    // temperatureFString = mid( temperatureFString, 1, instr( temperatureFString, ".") -1 )
                HserPrint temperatureFString
                HserPrint "f "
            #ENDIF

            HserPrintCRLF
        
        wait 1 s
        HserPrintCRLF
    Loop
    HSerPrintStringCRLF "Done"