123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- #pragma once
- #include <inttypes.h>
- #include "cmdqueue.h"
- #include "pins.h"
- #include "fastio.h"
- #include "adc.h"
- #include "pat9125.h"
- #define FSENSOR_IR 1
- #define FSENSOR_IR_ANALOG 2
- #define FSENSOR_PAT9125 3
- class FSensorBlockRunout {
- public:
- FSensorBlockRunout();
- ~FSensorBlockRunout();
- };
- #ifdef FILAMENT_SENSOR
- class Filament_sensor {
- public:
- virtual void init() = 0;
- virtual void deinit() = 0;
- virtual bool update() = 0;
- virtual bool getFilamentPresent() = 0;
- #ifdef FSENSOR_PROBING
- virtual bool probeOtherType() = 0;
- #endif
-
- enum class State : uint8_t {
- disabled = 0,
- initializing,
- ready,
- error,
- };
-
- enum class SensorActionOnError : uint8_t {
- _Continue = 0,
- _Pause = 1,
- _Undef = EEPROM_EMPTY_VALUE
- };
-
- void setEnabled(bool enabled);
-
- void setAutoLoadEnabled(bool state, bool updateEEPROM = false);
-
- bool getAutoLoadEnabled() {
- return autoLoadEnabled;
- }
-
- void setRunoutEnabled(bool state, bool updateEEPROM = false);
-
- bool getRunoutEnabled() {
- return runoutEnabled;
- }
-
- void setActionOnError(SensorActionOnError state, bool updateEEPROM = false);
-
- SensorActionOnError getActionOnError() {
- return sensorActionOnError;
- }
-
- bool getFilamentLoadEvent() {
- return postponedLoadEvent;
- }
-
- bool isError() {
- return state == State::error;
- }
-
- bool isReady() {
- return state == State::ready;
- }
-
- bool isEnabled() {
- return state != State::disabled;
- }
-
- protected:
- void settings_init_common();
-
- bool checkFilamentEvents();
-
- void triggerFilamentInserted();
-
- void triggerFilamentRemoved();
-
- void filAutoLoad();
-
- void filRunout();
-
- void triggerError();
-
- State state;
- bool autoLoadEnabled;
- bool runoutEnabled;
- bool oldFilamentPresent;
- bool postponedLoadEvent;
- ShortTimer eventBlankingTimer;
- SensorActionOnError sensorActionOnError;
- };
- #if (FILAMENT_SENSOR_TYPE == FSENSOR_IR) || (FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG)
- class IR_sensor: public Filament_sensor {
- public:
- void init() override;
- void deinit() override;
- bool update()override ;
- bool getFilamentPresent()override;
- #ifdef FSENSOR_PROBING
- bool probeOtherType()override;
- #endif
- void settings_init();
- };
- #if (FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG)
- constexpr static uint16_t Voltage2Raw(float V) {
- return (V * 1023 * OVERSAMPLENR / VOLT_DIV_REF ) + 0.5F;
- }
- constexpr static float Raw2Voltage(uint16_t raw) {
- return VOLT_DIV_REF * (raw / (1023.F * OVERSAMPLENR));
- }
- class IR_sensor_analog: public IR_sensor {
- public:
- void init()override;
- void deinit()override;
- bool update()override;
- void voltUpdate(uint16_t raw);
-
- uint16_t getVoltRaw();
-
- void settings_init();
-
- enum class SensorRevision : uint8_t {
- _Old = 0,
- _Rev04 = 1,
- _Undef = EEPROM_EMPTY_VALUE
- };
-
- SensorRevision getSensorRevision() {
- return sensorRevision;
- }
-
- const char* getIRVersionText();
-
- void setSensorRevision(SensorRevision rev, bool updateEEPROM = false);
-
- bool checkVoltage(uint16_t raw);
-
-
- constexpr static uint16_t IRsensor_Ldiode_TRESHOLD = Voltage2Raw(0.3F);
- constexpr static uint16_t IRsensor_Lmax_TRESHOLD = Voltage2Raw(1.5F);
- constexpr static uint16_t IRsensor_Hmin_TRESHOLD = Voltage2Raw(3.0F);
- constexpr static uint16_t IRsensor_Hopen_TRESHOLD = Voltage2Raw(4.6F);
- constexpr static uint16_t IRsensor_VMax_TRESHOLD = Voltage2Raw(5.F);
-
- private:
- SensorRevision sensorRevision;
- volatile bool voltReady;
- volatile uint16_t voltRaw;
- uint16_t minVolt = Voltage2Raw(6.F);
- uint16_t maxVolt = 0;
- uint16_t nFSCheckCount;
- uint8_t voltageErrorCnt;
- static constexpr uint16_t FS_CHECK_COUNT = 4;
-
-
-
-
- void IR_ANALOG_Check(SensorRevision isVersion, SensorRevision switchTo);
- };
- #endif
- #endif
- #if (FILAMENT_SENSOR_TYPE == FSENSOR_PAT9125)
- class PAT9125_sensor: public Filament_sensor {
- public:
- void init()override;
- void deinit()override;
- bool update()override;
- bool getFilamentPresent() override{
- return filterFilPresent;
- }
-
- #ifdef FSENSOR_PROBING
- bool probeOtherType() override;
- #endif
-
- void setJamDetectionEnabled(bool state, bool updateEEPROM = false);
-
- bool getJamDetectionEnabled() {
- return jamDetection;
- }
-
- void stStep(bool rev) {
- stepCount += rev ? -1 : 1;
- }
-
- void settings_init();
- private:
- static constexpr uint16_t pollingPeriod = 10;
- static constexpr uint8_t filterCnt = 5;
- ShortTimer pollingTimer;
- uint8_t filter;
- uint8_t filterFilPresent;
-
- bool jamDetection;
- int16_t oldPos;
- volatile int16_t stepCount;
- int16_t chunkSteps;
- uint8_t jamErrCnt;
-
- constexpr void calcChunkSteps(float u) {
- chunkSteps = (int16_t)(1.25 * u);
- }
-
- int16_t getStepCount();
-
- void resetStepCount();
-
- void filJam();
-
- bool updatePAT9125();
- };
- #endif
- #if FILAMENT_SENSOR_TYPE == FSENSOR_IR
- extern IR_sensor fsensor;
- #elif FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG
- extern IR_sensor_analog fsensor;
- #elif FILAMENT_SENSOR_TYPE == FSENSOR_PAT9125
- extern PAT9125_sensor fsensor;
- #endif
- #endif
|