Avoid exposing internal variables of fsensor

makes the code a bit shorter as well, because the compiler can be sure
that the variables are not accessed from the outside of the
fsensor.cpp compilation unit.
This commit is contained in:
D.R.racer 2022-04-20 17:32:22 +02:00
parent db0374896f
commit 4732859a98
3 changed files with 59 additions and 34 deletions

View File

@ -44,19 +44,23 @@ const char ERRMSG_PAT9125_NOT_RESP[] PROGMEM = "PAT9125 not responding (%d)!\n";
#define FSENSOR_INT_PIN_PCICR_BIT PCIE1 // PinChange Interrupt Enable / Flag @ PJ4 #define FSENSOR_INT_PIN_PCICR_BIT PCIE1 // PinChange Interrupt Enable / Flag @ PJ4
//! enabled = initialized and sampled every chunk event //! enabled = initialized and sampled every chunk event
bool fsensor_enabled = true; static bool enabled = true;
bool fsensor_enabled() { return enabled; }
//! runout watching is done in fsensor_update (called from main loop) //! runout watching is done in fsensor_update (called from main loop)
bool fsensor_watch_runout = true; static bool fsensor_watch_runout = true;
//! not responding - is set if any communication error occurred during initialization or readout //! not responding - is set if any communication error occurred during initialization or readout
bool fsensor_not_responding = false; static bool not_responding = false;
bool fsensor_not_responding(){ return not_responding; }
void fsensor_set_responding_ok() { not_responding = false; }
/// This flag was originally located in mmu.cpp. Not sure what it was supposed to do, but it looks like /// This flag was originally located in mmu.cpp. Not sure what it was supposed to do, but it looks like
/// it was holding "true" all the time on MK3S. /// it was holding "true" all the time on MK3S.
#ifndef IR_SENSOR #ifndef IR_SENSOR
bool ir_sensor_detected = false; static bool ir_sensor_detected = false;
bool check_for_ir_sensor(); ///< detects IR sensor and updates ir_sensor_detected bool check_for_ir_sensor(); ///< detects IR sensor and updates ir_sensor_detected
bool IRSensorDetected() { bool fsensor_IR_detected() {
return ir_sensor_detected; return ir_sensor_detected;
} }
#endif #endif
@ -89,9 +93,11 @@ uint8_t fsensor_log = 1;
//! @{ //! @{
//! autoload feature enabled //! autoload feature enabled
bool fsensor_autoload_enabled = true; static bool autoload_enabled = true;
bool fsensor_autoload_enabled() { return autoload_enabled; }
//! autoload watching enable/disable flag //! autoload watching enable/disable flag
bool fsensor_watch_autoload = false; static bool watch_autoload = false;
#ifdef PAT9125 #ifdef PAT9125
// //
@ -202,8 +208,8 @@ void fsensor_init(void)
printf_P(PSTR("PAT9125_init:%u\n"), pat9125); printf_P(PSTR("PAT9125_init:%u\n"), pat9125);
#endif //PAT9125 #endif //PAT9125
uint8_t fsensor_enabled = eeprom_read_byte((uint8_t*)EEPROM_FSENSOR); uint8_t fsensor_enabled = eeprom_read_byte((uint8_t*)EEPROM_FSENSOR);
fsensor_autoload_enabled=eeprom_read_byte((uint8_t*)EEPROM_FSENS_AUTOLOAD_ENABLED); autoload_enabled=eeprom_read_byte((uint8_t*)EEPROM_FSENS_AUTOLOAD_ENABLED);
fsensor_not_responding = false; not_responding = false;
#ifdef PAT9125 #ifdef PAT9125
uint8_t oq_meassure_enabled = eeprom_read_byte((uint8_t*)EEPROM_FSENS_OQ_MEASS_ENABLED); uint8_t oq_meassure_enabled = eeprom_read_byte((uint8_t*)EEPROM_FSENS_OQ_MEASS_ENABLED);
fsensor_oq_meassure_enabled = (oq_meassure_enabled == 1)?true:false; fsensor_oq_meassure_enabled = (oq_meassure_enabled == 1)?true:false;
@ -223,7 +229,7 @@ void fsensor_init(void)
// set this flag accordingly to show N/A in Settings->Filament sensor. // set this flag accordingly to show N/A in Settings->Filament sensor.
// This is even valid for both fsensor board revisions (0.3 or older and 0.4). // This is even valid for both fsensor board revisions (0.3 or older and 0.4).
// Must be done after reading what type of fsensor board we have // Must be done after reading what type of fsensor board we have
fsensor_not_responding = ! fsensor_IR_check(); not_responding = ! fsensor_IR_check();
#endif //IR_SENSOR_ANALOG #endif //IR_SENSOR_ANALOG
if (fsensor_enabled){ if (fsensor_enabled){
fsensor_enable(false); // (in this case) EEPROM update is not necessary fsensor_enable(false); // (in this case) EEPROM update is not necessary
@ -272,14 +278,14 @@ bool fsensor_enable(bool bUpdateEEPROM)
if(!fsensor_IR_check()) if(!fsensor_IR_check())
{ {
bUpdateEEPROM=true; bUpdateEEPROM=true;
fsensor_enabled=false; enabled=false;
fsensor_not_responding=true; not_responding=true;
FSensorStateMenu=0; FSensorStateMenu=0;
} }
else { else {
#endif //IR_SENSOR_ANALOG #endif //IR_SENSOR_ANALOG
fsensor_enabled=true; enabled=true;
fsensor_not_responding=false; not_responding=false;
FSensorStateMenu=1; FSensorStateMenu=1;
#ifdef IR_SENSOR_ANALOG #ifdef IR_SENSOR_ANALOG
} }
@ -287,12 +293,12 @@ bool fsensor_enable(bool bUpdateEEPROM)
if(bUpdateEEPROM) if(bUpdateEEPROM)
eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, FSensorStateMenu); eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, FSensorStateMenu);
#endif //PAT9125 #endif //PAT9125
return fsensor_enabled; return enabled;
} }
void fsensor_disable(bool bUpdateEEPROM) void fsensor_disable(bool bUpdateEEPROM)
{ {
fsensor_enabled = false; enabled = false;
FSensorStateMenu = 0; FSensorStateMenu = 0;
if(bUpdateEEPROM) if(bUpdateEEPROM)
eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, 0x00); eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, 0x00);
@ -303,8 +309,8 @@ void fsensor_autoload_set(bool State)
#ifdef PAT9125 #ifdef PAT9125
if (!State) fsensor_autoload_check_stop(); if (!State) fsensor_autoload_check_stop();
#endif //PAT9125 #endif //PAT9125
fsensor_autoload_enabled = State; autoload_enabled = State;
eeprom_update_byte((unsigned char *)EEPROM_FSENS_AUTOLOAD_ENABLED, fsensor_autoload_enabled); eeprom_update_byte((unsigned char *)EEPROM_FSENS_AUTOLOAD_ENABLED, autoload_enabled);
} }
void pciSetup(byte pin) void pciSetup(byte pin)
@ -358,14 +364,14 @@ void fsensor_autoload_check_stop(void)
bool fsensor_check_autoload(void) bool fsensor_check_autoload(void)
{ {
if (!fsensor_enabled) return false; if (!enabled) return false;
if (!fsensor_autoload_enabled) return false; if (!autoload_enabled) return false;
if (IRSensorDetected()) { if (fsensor_IR_detected()) {
if (READ(IR_SENSOR_PIN)) { if (READ(IR_SENSOR_PIN)) {
fsensor_watch_autoload = true; watch_autoload = true;
} }
else if (fsensor_watch_autoload == true) { else if (watch_autoload == true) {
fsensor_watch_autoload = false; watch_autoload = false;
return true; return true;
} }
} }
@ -581,7 +587,7 @@ FORCE_INLINE static void fsensor_isr(int st_cnt)
ISR(FSENSOR_INT_PIN_VECT) ISR(FSENSOR_INT_PIN_VECT)
{ {
if (mmu_enabled || IRSensorDetected()) return; if (mmu_enabled || fsensor_IR_detected()) return;
if (!((fsensor_int_pin_old ^ FSENSOR_INT_PIN_PIN_REG) & FSENSOR_INT_PIN_MASK)) return; if (!((fsensor_int_pin_old ^ FSENSOR_INT_PIN_PIN_REG) & FSENSOR_INT_PIN_MASK)) return;
fsensor_int_pin_old = FSENSOR_INT_PIN_PIN_REG; fsensor_int_pin_old = FSENSOR_INT_PIN_PIN_REG;
@ -696,7 +702,7 @@ void fsensor_update(void)
} }
} }
#else //PAT9125 #else //PAT9125
if (CHECK_FSENSOR && IRSensorDetected()) if (CHECK_FSENSOR && fsensor_IR_detected())
{ {
if (READ(IR_SENSOR_PIN)) if (READ(IR_SENSOR_PIN))
{ // IR_SENSOR_PIN ~ H { // IR_SENSOR_PIN ~ H
@ -734,7 +740,7 @@ void fsensor_update(void)
if( (oFsensorPCB == ClFsensorPCB::_Rev04) && ( (nADC*OVERSAMPLENR) > IRsensor_Hopen_TRESHOLD ) ) if( (oFsensorPCB == ClFsensorPCB::_Rev04) && ( (nADC*OVERSAMPLENR) > IRsensor_Hopen_TRESHOLD ) )
{ {
fsensor_disable(); fsensor_disable();
fsensor_not_responding = true; not_responding = true;
printf_P(PSTR("IR sensor not responding (%d)!\n"),1); printf_P(PSTR("IR sensor not responding (%d)!\n"),1);
if((ClFsensorActionNA)eeprom_read_byte((uint8_t*)EEPROM_FSENSOR_ACTION_NA)==ClFsensorActionNA::_Pause) if((ClFsensorActionNA)eeprom_read_byte((uint8_t*)EEPROM_FSENSOR_ACTION_NA)==ClFsensorActionNA::_Pause)

View File

@ -6,10 +6,17 @@
#include "config.h" #include "config.h"
// enable/disable flag //! @name filament sensor enable/disable flag
extern bool fsensor_enabled; //! @{
// not responding flag bool fsensor_enabled();
extern bool fsensor_not_responding; //! @}
//! @name filament sensor not responding flag
//! @{
bool fsensor_not_responding();
void fsensor_set_responding_ok();
//! @}
#ifdef PAT9125 #ifdef PAT9125
// optical checking "chunk lenght" (already in steps) // optical checking "chunk lenght" (already in steps)
extern int16_t fsensor_chunk_len; extern int16_t fsensor_chunk_len;
@ -32,9 +39,9 @@ extern void fsensor_init(void);
/// IR sensor detection originally for MMU? /// IR sensor detection originally for MMU?
/// Note: the signature of this function intentionally differs upon IR_SENSOR macro to allow for best optimization. /// Note: the signature of this function intentionally differs upon IR_SENSOR macro to allow for best optimization.
#ifdef IR_SENSOR #ifdef IR_SENSOR
constexpr bool IRSensorDetected() { return true; } constexpr bool fsensor_IR_detected() { return true; }
#else #else
bool IRSensorDetected(); bool fsensor_IR_detected();
#endif #endif
@ -50,8 +57,8 @@ extern void fsensor_disable(bool bUpdateEEPROM=true);
//! @} //! @}
//autoload feature enabled //autoload feature enabled
extern bool fsensor_autoload_enabled;
extern void fsensor_autoload_set(bool State); extern void fsensor_autoload_set(bool State);
bool fsensor_autoload_enabled();
extern void fsensor_update(void); extern void fsensor_update(void);
#ifdef PAT9125 #ifdef PAT9125

View File

@ -1791,6 +1791,18 @@ void lcd_cutter_enabled()
} }
#endif //MMU_HAS_CUTTER #endif //MMU_HAS_CUTTER
void lcd_set_filament_autoload() {
fsensor_autoload_set(!fsensor_autoload_enabled());
}
#if defined(FILAMENT_SENSOR) && defined(PAT9125)
void lcd_set_filament_oq_meass()
{
fsensor_oq_meassure_set(!fsensor_oq_meassure_enabled);
}
#endif
FilamentAction eFilamentAction=FilamentAction::None; // must be initialized as 'non-autoLoad' FilamentAction eFilamentAction=FilamentAction::None; // must be initialized as 'non-autoLoad'
bool bFilamentPreheatState; bool bFilamentPreheatState;
bool bFilamentAction=false; bool bFilamentAction=false;