Browse Source

sns: update events sensor

updated structure, overrides. strong time type
testing timesource changes. this should work, but might need to tweak
things if it starts to fail b/c of flash code loads
pull/2516/head
Maxim Prokhorov 2 years ago
parent
commit
d670e85b10
2 changed files with 70 additions and 81 deletions
  1. +35
    -46
      code/espurna/sensor.cpp
  2. +35
    -35
      code/espurna/sensors/EventSensor.h

+ 35
- 46
code/espurna/sensor.cpp View File

@ -2746,60 +2746,49 @@ void _sensorLoad() {
#if EVENTS_SUPPORT
{
auto getPin = [](unsigned char index) -> int {
switch (index) {
case 0: return EVENTS1_PIN;
case 1: return EVENTS2_PIN;
case 2: return EVENTS3_PIN;
case 3: return EVENTS4_PIN;
case 4: return EVENTS5_PIN;
case 5: return EVENTS6_PIN;
case 6: return EVENTS7_PIN;
case 7: return EVENTS8_PIN;
default: return GPIO_NONE;
}
auto getPin = [](unsigned char index) -> unsigned char {
return (index == 0) ? EVENTS1_PIN :
(index == 1) ? EVENTS2_PIN :
(index == 2) ? EVENTS3_PIN :
(index == 3) ? EVENTS4_PIN :
(index == 4) ? EVENTS5_PIN :
(index == 5) ? EVENTS6_PIN :
(index == 6) ? EVENTS7_PIN :
(index == 7) ? EVENTS8_PIN : GPIO_NONE;
};
auto getMode = [](unsigned char index) -> int {
switch (index) {
case 0: return EVENTS1_PIN_MODE;
case 1: return EVENTS2_PIN_MODE;
case 2: return EVENTS3_PIN_MODE;
case 3: return EVENTS4_PIN_MODE;
case 4: return EVENTS5_PIN_MODE;
case 5: return EVENTS6_PIN_MODE;
case 6: return EVENTS7_PIN_MODE;
case 7: return EVENTS8_PIN_MODE;
default: return INPUT;
}
return (index == 0) ? EVENTS1_PIN_MODE :
(index == 1) ? EVENTS2_PIN_MODE :
(index == 2) ? EVENTS3_PIN_MODE :
(index == 3) ? EVENTS4_PIN_MODE :
(index == 4) ? EVENTS5_PIN_MODE :
(index == 5) ? EVENTS6_PIN_MODE :
(index == 6) ? EVENTS7_PIN_MODE :
(index == 7) ? EVENTS8_PIN_MODE : INPUT;
};
auto getDebounce = [](unsigned char index) -> unsigned long {
switch (index) {
case 0: return EVENTS1_DEBOUNCE;
case 1: return EVENTS2_DEBOUNCE;
case 2: return EVENTS3_DEBOUNCE;
case 3: return EVENTS4_DEBOUNCE;
case 4: return EVENTS5_DEBOUNCE;
case 5: return EVENTS6_DEBOUNCE;
case 6: return EVENTS7_DEBOUNCE;
case 7: return EVENTS8_DEBOUNCE;
default: return 50;
}
auto getDebounce = [](unsigned char index) -> espurna::duration::Milliseconds {
return espurna::duration::Milliseconds(
(index == 0) ? EVENTS1_DEBOUNCE :
(index == 1) ? EVENTS2_DEBOUNCE :
(index == 2) ? EVENTS3_DEBOUNCE :
(index == 3) ? EVENTS4_DEBOUNCE :
(index == 4) ? EVENTS5_DEBOUNCE :
(index == 5) ? EVENTS6_DEBOUNCE :
(index == 6) ? EVENTS7_DEBOUNCE :
(index == 7) ? EVENTS8_DEBOUNCE : 50);
};
auto getIsrMode = [](unsigned char index) -> int {
switch (index) {
case 0: return EVENTS1_INTERRUPT_MODE;
case 1: return EVENTS2_INTERRUPT_MODE;
case 2: return EVENTS3_INTERRUPT_MODE;
case 3: return EVENTS4_INTERRUPT_MODE;
case 4: return EVENTS5_INTERRUPT_MODE;
case 5: return EVENTS6_INTERRUPT_MODE;
case 6: return EVENTS7_INTERRUPT_MODE;
case 7: return EVENTS8_INTERRUPT_MODE;
default: return RISING;
}
return (index == 0) ? EVENTS1_INTERRUPT_MODE :
(index == 1) ? EVENTS2_INTERRUPT_MODE :
(index == 2) ? EVENTS3_INTERRUPT_MODE :
(index == 3) ? EVENTS4_INTERRUPT_MODE :
(index == 4) ? EVENTS5_INTERRUPT_MODE :
(index == 5) ? EVENTS6_INTERRUPT_MODE :
(index == 6) ? EVENTS7_INTERRUPT_MODE :
(index == 7) ? EVENTS8_INTERRUPT_MODE : RISING;
};
auto pins = gpioPins();


+ 35
- 35
code/espurna/sensors/EventSensor.h View File

@ -16,14 +16,7 @@ class EventSensor : public BaseSensor {
public:
// ---------------------------------------------------------------------
// Public
// ---------------------------------------------------------------------
EventSensor() {
_count = 2;
_sensor_id = SENSOR_EVENTS_ID;
}
using TimeSource = espurna::time::CpuClock;
// ---------------------------------------------------------------------
@ -39,59 +32,63 @@ class EventSensor : public BaseSensor {
_interrupt_mode = interrupt_mode;
}
void setDebounceTime(unsigned long ms) {
_isr_debounce = microsecondsToClockCycles(ms * 1000);
template <typename T>
void setDebounceTime(T value) {
_interrupt_debounce = std::chrono::duration_cast<TimeSource::duration>(value);
}
// ---------------------------------------------------------------------
unsigned char getGPIO() {
unsigned char getGPIO() const {
return _pin.pin();
}
unsigned char getPinMode() {
unsigned char getPinMode() const {
return _pin_mode;
}
unsigned char getInterruptMode() {
unsigned char getInterruptMode() const {
return _interrupt_mode;
}
unsigned long getDebounceTime() {
return _isr_debounce;
TimeSource::duration getDebounceTime() const {
return _interrupt_debounce;
}
// ---------------------------------------------------------------------
// Sensors API
// ---------------------------------------------------------------------
unsigned char id() const override {
return SENSOR_EVENTS_ID;
}
unsigned char count() const override {
return 2;
}
// Initialization method, must be idempotent
// Defined outside the class body
void begin() {
void begin() override {
pinMode(_pin.pin(), _pin_mode);
_enableInterrupts();
_ready = true;
}
// Descriptive name of the sensor
String description() {
String description() const override {
char buffer[20];
snprintf(buffer, sizeof(buffer), "INTERRUPT @ GPIO%hhu", _pin.pin());
snprintf_P(buffer, sizeof(buffer),
PSTR("INTERRUPT @ GPIO%hhu"), _pin.pin());
return String(buffer);
}
// Descriptive name of the slot # index
String description(unsigned char) {
return description();
};
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char) {
return String(_pin);
String address(unsigned char) const override {
return String(_pin.pin(), 10);
}
// Type for slot # index
unsigned char type(unsigned char index) {
unsigned char type(unsigned char index) const override {
if (index == 0) return MAGNITUDE_COUNT;
if (index == 1) return MAGNITUDE_EVENT;
return MAGNITUDE_NONE;
@ -100,14 +97,15 @@ class EventSensor : public BaseSensor {
void pre() override {
_last = _current;
_current = _counter;
_difference = _current - _last;
}
double value(unsigned char index) override {
switch (index) {
case 0:
return _current - _last;
return _difference;
case 1:
return (_current - _last > 0) ? 1.0 : 0.0;
return (_difference > 0) ? 1.0 : 0.0;
default:
return 0.0;
}
@ -141,15 +139,16 @@ class EventSensor : public BaseSensor {
// - clockCyclesToMicroseconds(cycles)
// Since the division operation on this chip is pretty slow,
// avoid doing the conversion here and instead do that at initialization
auto cycles = ESP.getCycleCount();
if (cycles - _isr_last > _isr_debounce) {
_isr_last = cycles;
const auto now = TimeSource::now();
if (now - _interrupt_last > _interrupt_debounce) {
_interrupt_last = now;
++_counter;
}
}
void _enableInterrupts() {
if (_isr_debounce) {
if (_interrupt_debounce.count()) {
_interrupt_last = TimeSource::now();
_pin.attach(this, handleDebouncedInterrupt, _interrupt_mode);
} else {
_pin.attach(this, handleInterrupt, _interrupt_mode);
@ -164,9 +163,10 @@ class EventSensor : public BaseSensor {
unsigned long _current { 0ul };
unsigned long _last { 0ul };
unsigned long _difference { 0ul };
unsigned long _isr_last { 0ul };
unsigned long _isr_debounce { microsecondsToClockCycles(EVENTS1_DEBOUNCE * 1000) };
TimeSource::duration _interrupt_debounce;
TimeSource::time_point _interrupt_last;
InterruptablePin _pin{};
uint8_t _pin_mode { INPUT };


Loading…
Cancel
Save