Ver Fonte

fsensor add deinit

Alex Voinea há 3 anos atrás
pai
commit
52b44ad178
2 ficheiros alterados com 23 adições e 6 exclusões
  1. 1 1
      Firmware/Filament_sensor.cpp
  2. 22 5
      Firmware/Filament_sensor.h

+ 1 - 1
Firmware/Filament_sensor.cpp

@@ -6,4 +6,4 @@ IR_sensor fsensor;
 #elif FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG
 IR_sensor_analog fsensor;
 #endif
-#endif //FILAMENT_SENSOR
+#endif //FILAMENT_SENSOR

+ 22 - 5
Firmware/Filament_sensor.h

@@ -24,6 +24,7 @@
 class Filament_sensor {
 public:
     virtual void init() = 0;
+    virtual void deinit() = 0;
     virtual bool update() = 0;
     virtual bool getFilamentPresent() = 0;
     
@@ -41,8 +42,14 @@ public:
     };
     
     void setEnabled(bool enabled) {
-        state = enabled ? State::initializing : State::disabled;
         eeprom_update_byte((uint8_t *)EEPROM_FSENSOR, enabled);
+        if (enabled) {
+            init();
+        }
+        else {
+            deinit();
+        }
+        state = enabled ? State::initializing : State::disabled;
     }
     
     void setAutoLoadEnabled(bool state, bool updateEEPROM = false) {
@@ -156,9 +163,8 @@ protected:
     }
     
     void triggerError() {
+        // deinit(); //not sure if I should call this here.
         state = State::error;
-        autoLoadEnabled = false;
-        runoutEnabled = false;
         
         /// some message, idk
         ;//
@@ -176,10 +182,17 @@ protected:
 class IR_sensor: public Filament_sensor {
 public:
     void init() {
+        puts_P(PSTR("fsensor::init()"));
         SET_INPUT(IR_SENSOR_PIN); //input mode
         WRITE(IR_SENSOR_PIN, 1); //pullup
-        settings_init();
-        state = State::initializing;
+        settings_init(); //also sets the state to State::initializing
+    }
+    
+    void deinit() {
+        puts_P(PSTR("fsensor::deinit()"));
+        SET_INPUT(IR_SENSOR_PIN); //input mode
+        WRITE(IR_SENSOR_PIN, 0); //no pullup
+        state = State::disabled;
     }
     
     bool update() {
@@ -221,6 +234,10 @@ public:
         settings_init();
     }
     
+    void deinit() {
+        IR_sensor::deinit();
+    }
+    
     bool update() {
         bool event = IR_sensor::update();
         if (state == State::ready) {