<commit_before>#pragma once

#include "wled.h"

/*
 * This usermod handles PIR sensor states.
 * The strip will be switched on and the off timer will be resetted when the sensor goes HIGH. 
 * When the sensor state goes LOW, the off timer is started and when it expires, the strip is switched off. 
 * 
 * 
 * Usermods allow you to add own functionality to WLED more easily
 * See: https://github.com/Aircoookie/WLED/wiki/Add-own-functionality
 * 
 * v2 usermods are class inheritance based and can (but don't have to) implement more functions, each of them is shown in this example.
 * Multiple v2 usermods can be added to one compilation easily.
 * 
 * Creating a usermod:
 * This file serves as an example. If you want to create a usermod, it is recommended to use usermod_v2_empty.h from the usermods folder as a template.
 * Please remember to rename the class and file to a descriptive name.
 * You may also use multiple .h and .cpp files.
 * 
 * Using a usermod:
 * 1. Copy the usermod into the sketch folder (same folder as wled00.ino)
 * 2. Register the usermod by adding #include "usermod_filename.h" in the top and registerUsermod(new MyUsermodClass()) in the bottom of usermods_list.cpp
 */

class PIRsensorSwitch : public Usermod {
  private:
    // PIR sensor pin
    const uint8_t PIRsensorPin = 13; // D7 on D1 mini
    // notification mode for colorUpdated()
    const byte NotifyUpdateMode = NOTIFIER_CALL_MODE_NO_NOTIFY; // NOTIFIER_CALL_MODE_DIRECT_CHANGE
    // delay before switch off after the sensor state goes LOW
    uint32_t m_switchOffDelay = 600000;
    // off timer start time
    uint32_t m_offTimerStart = 0;
    // current PIR sensor pin state
    byte m_PIRsensorPinState = LOW;
    // PIR sensor enabled - ISR attached
    bool m_PIRenabled = true;

    /*
     * return or change if new PIR sensor state is available
     */
    static volatile bool newPIRsensorState(bool changeState = false, bool newState = false) {
      static volatile bool s_PIRsensorState = false;
      if (changeState) {
        s_PIRsensorState = newState;
      }
      return s_PIRsensorState;
    }

    /*
     * PIR sensor state has changed
     */
    static void IRAM_ATTR ISR_PIRstateChange() {
      newPIRsensorState(true, true);
    }

    /*
     * switch strip on/off
     */
    void switchStrip(bool switchOn) {
      if (switchOn && bri == 0) {
        bri = briLast;
        colorUpdated(NotifyUpdateMode);
      }    
      else if (!switchOn && bri != 0) {
        briLast = bri;
        bri = 0;
        colorUpdated(NotifyUpdateMode);
      }
    }

    /*
     * Read and update PIR sensor state.
     * Initilize/reset switch off timer
     */
    bool updatePIRsensorState() {
      if (newPIRsensorState()) {
        m_PIRsensorPinState = digitalRead(PIRsensorPin);
        
        if (m_PIRsensorPinState == HIGH) {
          m_offTimerStart = 0;
          switchStrip(true);
        }
        else if (bri != 0) {
          // start switch off timer
          m_offTimerStart = millis();
        }
        newPIRsensorState(true, false);
        return true;
      }
      return false;
    }

    /* 
     * switch off the strip if the delay has elapsed 
     */
    bool handleOffTimer() {
      if (m_offTimerStart > 0 && millis() - m_offTimerStart > m_switchOffDelay) {
        switchStrip(false);
        m_offTimerStart = 0;        
        return true;
      }
      return false;
    }<commit_msg>Update usermod_PIR_sensor_switch.h<commit_after>#pragma once

#include "wled.h"

/*
 * This usermod handles PIR sensor states.
 * The strip will be switched on and the off timer will be resetted when the sensor goes HIGH. 
 * When the sensor state goes LOW, the off timer is started and when it expires, the strip is switched off. 
 * 
 * 
 * Usermods allow you to add own functionality to WLED more easily
 * See: https://github.com/Aircoookie/WLED/wiki/Add-own-functionality
 * 
 * v2 usermods are class inheritance based and can (but don't have to) implement more functions, each of them is shown in this example.
 * Multiple v2 usermods can be added to one compilation easily.
 * 
 * Creating a usermod:
 * This file serves as an example. If you want to create a usermod, it is recommended to use usermod_v2_empty.h from the usermods folder as a template.
 * Please remember to rename the class and file to a descriptive name.
 * You may also use multiple .h and .cpp files.
 * 
 * Using a usermod:
 * 1. Copy the usermod into the sketch folder (same folder as wled00.ino)
 * 2. Register the usermod by adding #include "usermod_filename.h" in the top and registerUsermod(new MyUsermodClass()) in the bottom of usermods_list.cpp
 */

class PIRsensorSwitch : public Usermod {
  private:
    // PIR sensor pin
    const uint8_t PIRsensorPin = 13; // D7 on D1 mini
    // notification mode for colorUpdated()
    const byte NotifyUpdateMode = NOTIFIER_CALL_MODE_NO_NOTIFY; // NOTIFIER_CALL_MODE_DIRECT_CHANGE
    // delay before switch off after the sensor state goes LOW
    uint32_t m_switchOffDelay = 600000;
    // off timer start time
    uint32_t m_offTimerStart = 0;
    // current PIR sensor pin state
    byte m_PIRsensorPinState = LOW;
    // PIR sensor enabled - ISR attached
    bool m_PIRenabled = true;

    /*
     * return or change if new PIR sensor state is available
     */
    static volatile bool newPIRsensorState(bool changeState = false, bool newState = false) {
      static volatile bool s_PIRsensorState = false;
      if (changeState) {
        s_PIRsensorState = newState;
      }
      return s_PIRsensorState;
    }

    /*
     * PIR sensor state has changed
     */
    static void IRAM_ATTR ISR_PIRstateChange() {
      newPIRsensorState(true, true);
    }

    /*
     * switch strip on/off
     */
    void switchStrip(bool switchOn) {
      if (switchOn && bri == 0) {
        bri = briLast;
        colorUpdated(NotifyUpdateMode);
      }    
      else if (!switchOn && bri != 0) {
        briLast = bri;
        bri = 0;
        colorUpdated(NotifyUpdateMode);
      }
    }

    /*
     * Read and update PIR sensor state.
     * Initilize/reset switch off timer
     */
    bool updatePIRsensorState() {
      if (newPIRsensorState()) {
        m_PIRsensorPinState = digitalRead(PIRsensorPin);
        
        if (m_PIRsensorPinState == HIGH) {
          m_offTimerStart = 0;
          switchStrip(true);
        }
        else if (bri != 0) {
          // start switch off timer
          m_offTimerStart = millis();
        }
        newPIRsensorState(true, false);
        return true;
      }
      return false;
    }

    /* 
     * switch off the strip if the delay has elapsed 
     */
    bool handleOffTimer() {
      if (m_offTimerStart > 0 && millis() - m_offTimerStart > m_switchOffDelay) {
        if (m_PIRenabled == true){
          switchStrip(false);
        }
        m_offTimerStart = 0;        
        return true;
      }
      return false;
    }
{'fraction_non_alphanumeric': 0.07629310344827586, 'fraction_numerical': 0.008620689655172414, 'mean_word_length': 2.412254901960784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3745890', 'n_tokens_mistral': 2157, 'n_tokens_neox': 2090, 'n_words': 734}