#ifndef _PORTCONTROL

#define _PORTCONTROL

#include <arduino.h>
#include "Config.h"
#include "CommsExpander.h"
#include "PortExpander.h"

class PortControl {

    public:

        PortControl() {

            adcSelectList[0] = ADC_SELECT_0;
            adcSelectList[1] = ADC_SELECT_1;
            adcSelectList[2] = ADC_SELECT_2;

        }

        float readPortCurrent(uint32_t port) {
            // Select the ADC channel to read
            for (int i = 0; i < 3; i++) {
                uint8_t bit = bitRead(port, i);
                if (bit > 0) {
                    digitalWrite(adcSelectList[i],HIGH);
                }
                else {
                    digitalWrite(adcSelectList[i],LOW);
                }
            }
            
            // Let the channel settle
            delay(10); 

            // Read the port
            float current;
            int analogReading = analogRead(PORT_ISNS_AIN_0);
            if (port == 7) {
                current = (float)analogReading * (3.3 / 1023) * (500.0 / 309.5);
            }
            else {
                current = (float)analogReading * (3.3 / 1023) * (500.0 / 619);
            }
            return current;
        }

    protected:

        uint32_t currentPort;
        uint32_t adcSelectList[3];

};

#endif