Commit c05d31ce 刘韬

1

0 个父辈
{
"configuration": "CDCOnBoot=default,MSCOnBoot=default,DFUOnBoot=default,UploadMode=default,PSRAM=disabled,PartitionScheme=default,CPUFreq=240,FlashMode=qio,FlashFreq=80,FlashSize=4M,UploadSpeed=921600,DebugLevel=none",
"board": "esp32:esp32:esp32s2",
"sketch": "smart_shell_esp32.ino",
"port": "COM8",
"output": "D:\\rick\\ardoino\\build"
}
\ No newline at end of file
#pragma once
//#define diagnosMode true
#define _16MM true
//#define _25MM true
//#define _54MM true
#define ColorsetAddress 1000
#define ADDREPAddress 3002
//3010~3020
#define LedTypeEPAddress 3010
#define RowWidthAddress 3020
#define FactoryModeAddress 3050
//3100~3200
#define SensorCheckAddress 3100
#define BoardIsOK 3300
//3400~3500
#define ShelfPartAddress 3400
\ No newline at end of file
#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]);
#ifdef _25MM
delay(5);
#endif
#ifdef _54MM
delay(10);
#endif
//if (ledcount<40)
//
delayMicroseconds(40*1000/ledcount);
//if(pin==20 || pin==40)
// delay(5);
//delay(10-ledcount/10);
// if (sendelay>0) delay(sendelay);
}
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(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->println(pin);
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);
}
#ifndef _matrixSelector_h_
#define _matrixSelector_h_
#include "config.h"
#include <arduino.h>
//#include <EEPROMex.h>
#include <EEPROM.h>
class matrixSelector {
public:
matrixSelector(int _threshold );
// ~matrixSelector();
void initial();
long getadc(int pin , boolean txEn);
bool getstatus(int pin);
void calibrate(int pin,int mode);
void setcalnum(HardwareSerial *sl,int startpin,int endpin,int max,int min , int threshold );
void getcalnum(HardwareSerial *sl,int startpin,int endpin,int max,int min , int threshold );
int getmax(int pin);
int getmin(int pin);
int getthreshold(int pin);
int readIntFromEEPROM(int address);
short readShortFromEEPROM(int address);
void writeIntIntoEEPROM(int address, int number,bool nocommit);
void writeShortIntoEEPROM(int address, short number,bool nocommit);
void writeIntIntoEEPROM(int address, int number);
void writeShortIntoEEPROM(int address, short number);
void EEPROMcommit();
int sendelay=0;
int ledcount=100;
bool diagnosMode=false;
bool factoryMode=false;
//char *internalBuffer;
//uint16_t internalBufferSize = 0;
private:
int select_pin[10] = { 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 };
int sense_pin[10] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
int threshold;
const int maxAllowedWrites = 1000;
const int memBase = 200;
int addressInt ;
};
#endif
此文件的差异被折叠, 点击展开。
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!