matrixSelector.cpp 6.1 KB
#include "matrixSelector.h"
#include "config.h"
#include <arduino.h>
#include <Streaming.h>
#include <EEPROM.h>

matrixSelector::matrixSelector(int _threshold)
{
    // Serial.begin(115200);
    // threshold = 60;
}

void matrixSelector::initial()
{
    for (int i = 0; i < 10; i++)
    {
        pinMode(select_pin[i], OUTPUT);
    }
    pinMode(35, OUTPUT);
    pinMode(34, OUTPUT);
    // digitalWrite(35 , HIGH );
    //  digitalWrite(34 , HIGH );

    EEPROM.begin(4000);
    //  EEPROM.setMaxAllowedWrites(maxAllowedWrites);
    // start reading from position memBase (address 0) of the EEPROM. Set maximumSize to EEPROMSizeUno
    // Writes before membase or beyond EEPROMSizeUno will only give errors when _EEPROMEX_DEBUG is set
    //  EEPROM.setMemPool(memBase, EEPROMSizeMega);
    // addressInt  = EEPROM.getAddress(sizeof(int));
    // Serial.println(addressInt);
    //
    int x = readIntFromEEPROM(3000);
    if (x > 0)
        threshold = x;
    else
        threshold = 20;

    Serial.println("matrix scanner starts ");
    // Serial.print (threshold);
}

long matrixSelector::getadc(int pin, boolean txEn = true)
{
    pin--;
    int a, b;
    a = (pin / 10);
    b = (pin % 10);
    if (txEn)
    {
        if (b % 2 == 1)
        {
            digitalWrite(35, LOW);
            digitalWrite(34, HIGH);
        }
        else
        {
            digitalWrite(35, HIGH);
            digitalWrite(34, LOW);
        }
    }

    for (int i = 0; i < 10; i++)
    {
        // delay(1);
        if (i == a)//||i-1==a)
        {
            pinMode(select_pin[i], OUTPUT);
            digitalWrite(select_pin[i], HIGH); // Serial.print("h");Serial.print(select_pin[i]);
            delayMicroseconds(40*1000/ledcount);
        }
        else
        {
            pinMode(select_pin[i], OUTPUT);
            digitalWrite(select_pin[i], LOW); // Serial.print("L");Serial.print(select_pin[i]);
        }
    }
    // Serial.println("  ");

    long results = 0;
    for (int j = 0; j < 10; j++)
    {
        results += analogRead(sense_pin[b]);
        //  delayMicroseconds(100);
    }
    results = results / 10;
    digitalWrite(select_pin[a], LOW);
    digitalWrite(35, LOW);
    digitalWrite(34, LOW);
    //if (results == 0 && txEn)
    //    results = 9999;

    return results;
}

bool matrixSelector::getstatus(int pin)
{

    float max = getmax(pin);
    float min = getmin(pin);
    float sample = getadc(pin);

    float result = ((max - sample) / (max - min)) * 100;

    if (max <100)
        result = 0;

    if (diagnosMode)
        Serial << "\r\npin:" << pin << " max:" << max << " min: " << min << " sample : " << sample << " sen% : " << result << "";

    //if (factoryMode && sample<2)
    //    return false;

    if (result > getthreshold(pin))
        return true;
    else
        return false;
}

void matrixSelector::calibrate(int pin, int mode)
{
    // mode  1 is max
    // mode -1 is min

    long sample = 0;

    int maxadr = addressInt + (pin * 2);
    int minadr = addressInt + (pin * 2) + 300;
    // while (!EEPROM.isReady()){}

    if (mode == 1)
    {
        sample = getadc(pin, true);
        // if (sample<9999)
        writeIntIntoEEPROM(maxadr, sample, true);
    }
    if (mode == -1)
    {
        sample = getadc(pin, false);
        //if (sample < 9999)
        writeIntIntoEEPROM(minadr, sample, true);
    }
}

int matrixSelector::getmax(int pin)
{

    int adr = addressInt + (pin * 2);
    //    while (!EEPROM.isReady()){}
    int max = readIntFromEEPROM(adr);
    return max;
}
int matrixSelector::getmin(int pin)
{

    int adr = addressInt + (pin * 2) + 300;
    // while (!EEPROM.isReady()){}
    int min = readIntFromEEPROM(adr);
    return min;
}

int matrixSelector::getthreshold(int pin)
{
    int adr = addressInt + (pin * 2) + 600;
    int threshold = readIntFromEEPROM(adr);
    if (threshold == 0 || threshold > 100)
        threshold = 20;
    return threshold;
}

void matrixSelector::setcalnum(HardwareSerial *sl, int startpin, int endpin, int max, int min, int threshold)
{
    //  0 mean no change
    for (int pin = startpin; pin <= endpin; pin++)
    {
        int maxadr = addressInt + (pin * 2);
        int minadr = addressInt + (pin * 2) + 300;
        int thresholdadr = addressInt + (pin * 2) + 600;
        // while (!EEPROM.isReady()){}
        *sl << pin <<","<<max<<","<<min<<","<<threshold<<"\r\n";
        if (max > 0)
            writeIntIntoEEPROM(maxadr, max, true);

        if (min >= 0)
            writeIntIntoEEPROM(minadr, min, true);

        if (threshold > 0)
            writeIntIntoEEPROM(thresholdadr, threshold, true);
    }
    EEPROMcommit();
}

void matrixSelector::getcalnum(HardwareSerial *sl, int startpin, int endpin, int max, int min, int threshold)
{
    //  0 mean no change
    for (int pin = startpin; pin <= endpin; pin++)
    {
        int maxadr = addressInt + (pin * 2);
        int minadr = addressInt + (pin * 2) + 300;
        int thresholdadr = addressInt + (pin * 2) + 600;

        sl->print("(");
        sl->print(pin);
        sl->print(",");
        sl->print(readIntFromEEPROM(maxadr));
        sl->print(",");
        sl->print(readIntFromEEPROM(minadr));
        sl->print(",");
        sl->print(readIntFromEEPROM(thresholdadr));
        sl->print(",");
        sl->print(getadc(pin));
        sl->print(") ");
    }
}
void matrixSelector::writeIntIntoEEPROM(int address, int number, bool nocommit)
{
    EEPROM.write(address, number >> 8);
    EEPROM.write(address + 1, number & 0xFF);
    if (!nocommit)
        EEPROM.commit();
}
void matrixSelector::writeShortIntoEEPROM(int address, short number, bool nocommit)
{
    EEPROM.write(address, number);
    if (!nocommit)
        EEPROM.commit();
}
void matrixSelector::writeIntIntoEEPROM(int address, int number)
{
    writeIntIntoEEPROM(address, number, false);
}
void matrixSelector::writeShortIntoEEPROM(int address, short number)
{
    writeShortIntoEEPROM(address, number, false);
}
void matrixSelector::EEPROMcommit()
{
    EEPROM.commit();
}
int matrixSelector::readIntFromEEPROM(int address)
{
    return (EEPROM.read(address) << 8) + EEPROM.read(address + 1);
}
short matrixSelector::readShortFromEEPROM(int address)
{
    return EEPROM.read(address);
}