Commit f79c182b authored by MagoKimbra's avatar MagoKimbra

Add SDSETTINGS

parent 7e2bd53c
......@@ -432,6 +432,14 @@
//#define DISABLE_M503
//===========================================================================
//========================== EXTRA SETTINGS ON SD ===========================
// Uncomment SD SETTINGS to enable the firmware to write some configuration, that require frequent update, on the SD card.
//#define SD_SETTINGS
#define SD_CFG_SECONDS 300 //seconds between update
#define CFG_SD_FILE "INFO.CFG" //name of the configuration file
#define CFG_SD_MAX_KEY_LEN 3+1 //icrease this if you add key name longer than the actual value.
#define CFG_SD_MAX_VALUE_LEN 12+1 //this should be enought for int, long and float if you need to retrive strings increase this carefully
//===========================================================================
//==================== Bowden Filament management ===========================
//#define EASY_LOAD
......
......@@ -259,7 +259,6 @@ extern float zprobe_zoffset;
// Lifetime stats
extern unsigned long printer_usage_seconds; //this can old about 136 year before go overflow. If you belive that you can live more than this please contact me.
extern millis_t config_last_update;
#ifdef PREVENT_DANGEROUS_EXTRUDE
extern float extrude_min_temp;
......@@ -312,6 +311,11 @@ extern int fanSpeed;
extern int laser_ttl_modulation;
#endif
#if defined(SDSUPPORT) && defined(SD_SETTINGS)
extern millis_t config_last_update;
extern bool config_readed;
#endif
extern millis_t print_job_start_ms;
extern millis_t print_job_stop_ms;
......
......@@ -282,6 +282,8 @@ static uint8_t target_extruder;
bool no_wait_for_cooling = true;
bool target_direction;
unsigned long printer_usage_seconds;
#ifndef DELTA
int xy_travel_speed = XY_TRAVEL_SPEED;
float zprobe_zoffset = 0;
......@@ -421,6 +423,10 @@ bool target_direction;
#ifdef SDSUPPORT
static bool fromsd[BUFSIZE];
#ifdef SD_SETTINGS
millis_t config_last_update = 0;
bool config_readed = false;
#endif
#endif
#ifdef FILAMENTCHANGEENABLE
......@@ -499,7 +505,7 @@ bool setTargetedHotend(int code);
return free_memory;
}
}
#endif //!SDSUPPORT
#endif // !SDSUPPORT
#endif
/**
......@@ -705,6 +711,9 @@ void setup() {
for (int8_t i = 0; i < BUFSIZE; i++) fromsd[i] = false;
#endif
// loads custom configuration from SDCARD if available else uses defaults
ConfigSD_RetrieveSettings();
// loads data from EEPROM if available else uses defaults (and resets step acceleration rate)
Config_RetrieveSettings();
......@@ -7047,6 +7056,18 @@ void manage_inactivity(bool ignore_stepper_queue/*=false*/) {
}
#endif
#if defined(SDSUPPORT) && defined(SD_SETTINGS)
if(IS_SD_INSERTED && !IS_SD_PRINTING) {
if (!config_readed) {
ConfigSD_RetrieveSettings(true);
ConfigSD_StoreSettings();
}
else if((millis() - config_last_update) > SD_CFG_SECONDS * 1000UL) {
ConfigSD_StoreSettings();
}
}
#endif
#ifdef TEMP_STAT_LEDS
handle_status_leds();
#endif
......
......@@ -150,7 +150,7 @@ void CardReader::initsd() {
}
else {
cardOK = true;
ECHO_LM(DB, MSG_SD_CARD_OK);
ECHO_LM(OK, MSG_SD_CARD_OK);
}
workDir = root;
curDir = &root;
......
......@@ -102,6 +102,10 @@
#include "ultralcd.h"
#include "configuration_store.h"
#ifdef SDSUPPORT
#include "cardreader.h"
#endif
void _EEPROM_writeData(int &pos, uint8_t* value, uint8_t size) {
uint8_t c;
while(size--) {
......@@ -565,10 +569,10 @@ void Config_ResetDefault() {
#ifndef DISABLE_M503
/**
/**
* Print Configuration Settings - M502
*/
void Config_PrintSettings(bool forReplay) {
void Config_PrintSettings(bool forReplay) {
// Always have this function, even with EEPROM_SETTINGS disabled, the current values will be shown
if (!forReplay) {
......@@ -797,14 +801,103 @@ void Config_PrintSettings(bool forReplay) {
}
}
// Print Lifetime stats
ConfigSD_PrintSettings(forReplay);
}
void ConfigSD_PrintSettings(bool forReplay) {
// Always have this function, even with SD_SETTINGS disabled, the current values will be shown
#ifdef POWER_CONSUMPTION
if (!forReplay) {
ECHO_LM(DB, "Watt/h consumed:");
}
ECHO_LVM(DB, power_consumption_hour," W/h");
ECHO_LVM(OK, power_consumption_hour," W/h");
#endif
if (!forReplay) {
ECHO_LM(DB, "Power on time:");
}
char time[30];
int day = printer_usage_seconds / 60 / 60 / 24, hours = (printer_usage_seconds / 60 / 60) % 24, minutes = (printer_usage_seconds / 60) % 60;
sprintf_P(time, PSTR("%i " MSG_END_DAY " %i " MSG_END_HOUR " %i " MSG_END_MINUTE), day, hours, minutes);
ECHO_LV(DB, time);
}
#endif //!DISABLE_M503
/**
* Configuration on SD card
*
* Author: Simone Primarosa
*
*/
void ConfigSD_ResetDefault() {
#ifdef POWER_CONSUMPTION
power_consumption_hour = 0;
#endif
printer_usage_seconds = 0;
ECHO_LM(OK, "Hardcoded SD Default Settings Loaded");
}
#endif //!DISABLE_M503
#if defined(SDSUPPORT) && defined(SD_SETTINGS)
void ConfigSD_StoreSettings() {
if(!IS_SD_INSERTED || card.isFileOpen() || card.sdprinting) return;
card.openFile(CFG_SD_FILE, false, true, false);
char buff[CFG_SD_MAX_VALUE_LEN];
#ifdef POWER_CONSUMPTION
ltoa(power_consumption_hour,buff,10);
card.unparseKeyLine(cfgSD_KEY[SD_CFG_PWR], buff);
#endif
ltoa(printer_usage_seconds,buff,10);
card.unparseKeyLine(cfgSD_KEY[SD_CFG_TME], buff);
card.closeFile(false);
config_last_update = millis();
}
void ConfigSD_RetrieveSettings(bool addValue) {
if(!IS_SD_INSERTED || card.isFileOpen() || card.sdprinting || !card.cardOK) return;
char key[CFG_SD_MAX_KEY_LEN], value[CFG_SD_MAX_VALUE_LEN];
int k_idx;
int k_len, v_len;
card.openFile(CFG_SD_FILE, true, true, false);
while(true) {
k_len = CFG_SD_MAX_KEY_LEN;
v_len = CFG_SD_MAX_VALUE_LEN;
card.parseKeyLine(key, value, k_len, v_len);
if(k_len == 0 || v_len == 0) break; //no valid key or value founded
k_idx = ConfigSD_KeyIndex(key);
if(k_idx == -1) continue; //unknow key ignore it
switch(k_idx) {
#ifdef POWER_CONSUMPTION
case SD_CFG_PWR: {
if(addValue) power_consumption_hour += (unsigned long)atol(value);
else power_consumption_hour = (unsigned long)atol(value);
}
break;
#endif
case SD_CFG_TME: {
if(addValue) printer_usage_seconds += (unsigned long)atol(value);
else printer_usage_seconds = (unsigned long)atol(value);
}
break;
}
}
card.closeFile(false);
config_readed = true;
}
int ConfigSD_KeyIndex(char *key) { //At the moment a binary search algorithm is used for simplicity, if it will be necessary (Eg. tons of key), an hash search algorithm will be implemented.
int begin = 0, end = SD_CFG_END - 1, middle, cond;
while(begin <= end) {
middle = (begin + end) / 2;
cond = strcmp(cfgSD_KEY[middle], key);
if(!cond) return middle;
else if(cond < 0) begin = middle + 1;
else end = middle - 1;
}
return -1;
}
#endif
......@@ -4,11 +4,14 @@
#include "Configuration.h"
void Config_ResetDefault();
void ConfigSD_ResetDefault();
#ifndef DISABLE_M503
void Config_PrintSettings(bool forReplay=false);
void Config_PrintSettings(bool forReplay = false);
void ConfigSD_PrintSettings(bool forReplay = false);
#else
FORCE_INLINE void Config_PrintSettings(bool forReplay=false) {}
FORCE_INLINE void ConfigSD_PrintSettings(bool forReplay = false) {}
#endif
#ifdef EEPROM_SETTINGS
......@@ -19,4 +22,27 @@ void Config_ResetDefault();
FORCE_INLINE void Config_RetrieveSettings() { Config_ResetDefault(); Config_PrintSettings(); }
#endif
#if defined(SDSUPPORT) && defined(SD_SETTINGS)
static const char *cfgSD_KEY[] = { //Keep this in lexicographical order for better search performance(O(Nlog2(N)) insted of O(N*N)) (if you don't keep this sorted, the algorithm for find the key index won't work, keep attention.)
#ifdef POWER_CONSUMPTION
"PWR",
#endif
"TME",
};
enum cfgSD_ENUM { //This need to be in the same order as cfgSD_KEY
#ifdef POWER_CONSUMPTION
SD_CFG_PWR,
#endif
SD_CFG_TME,
SD_CFG_END //Leave this always as the last
};
void ConfigSD_StoreSettings();
void ConfigSD_RetrieveSettings(bool addValue = false);
int ConfigSD_KeyIndex(char *key);
#else
FORCE_INLINE void ConfigSD_RetrieveSettings() { ConfigSD_ResetDefault(); }
#endif
#endif //CONFIGURATION_STORE_H
......@@ -48,4 +48,5 @@
//============================================================================
//============================================================================
......@@ -779,6 +779,7 @@ static float analog2tempBed(int raw) {
static void updateTemperaturesFromRawValues() {
static millis_t last_update = millis();
millis_t temp_last_update = millis();
millis_t from_last_update = temp_last_update - last_update;
#ifdef HEATER_0_USES_MAX6675
current_temperature_raw[0] = read_max6675();
#endif
......@@ -793,7 +794,6 @@ static void updateTemperaturesFromRawValues() {
filament_width_meas = analog2widthFil();
#endif
#if HAS_POWER_CONSUMPTION_SENSOR
millis_t from_last_update = temp_last_update - last_update;
static float watt_overflow = 0.0;
power_consumption_meas = analog2power();
//MYSERIAL.println(analog2current(),3);
......@@ -804,7 +804,16 @@ static void updateTemperaturesFromRawValues() {
}
#endif
//Reset the watchdog after we know we have a temperature measurement.
// Update printer usage
static unsigned int second_overflow = 0;
second_overflow += from_last_update;
if (second_overflow >= 1000) {
printer_usage_seconds++;
second_overflow -= 1000;
}
last_update = temp_last_update;
// Reset the watchdog after we know we have a temperature measurement.
watchdog_reset();
CRITICAL_SECTION_START;
......@@ -818,7 +827,7 @@ static void updateTemperaturesFromRawValues() {
// Convert raw Filament Width to millimeters
float analog2widthFil() {
return current_raw_filwidth / 16383.0 * 5.0;
//return current_raw_filwidth;
// return current_raw_filwidth;
}
// Convert raw Filament Width to a ratio
......@@ -849,7 +858,7 @@ static void updateTemperaturesFromRawValues() {
*/
void tp_init() {
#if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
//disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
// disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
MCUCR=BIT(JTD);
MCUCR=BIT(JTD);
#endif
......@@ -865,7 +874,7 @@ void tp_init() {
#ifdef PIDTEMPBED
temp_iState_min_bed = 0.0;
temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
#endif //PIDTEMPBED
#endif // PIDTEMPBED
}
#if HAS_HEATER_0
......@@ -906,7 +915,7 @@ void tp_init() {
OUT_WRITE(MAX6675_SS,HIGH);
#endif //HEATER_0_USES_MAX6675
#endif // HEATER_0_USES_MAX6675
#ifdef __SAM3X8E__
// Use timer0 for temperature measurement
......@@ -1025,7 +1034,7 @@ void tp_init() {
bed_maxttemp_raw += OVERSAMPLENR;
#endif
}
#endif //BED_MAXTEMP
#endif // BED_MAXTEMP
}
#ifdef THERMAL_PROTECTION_HOTENDS
......@@ -1169,8 +1178,8 @@ void disable_all_heaters() {
WRITE(MAX6675_SS, 0);
// ensure 100ns delay - a bit extra is fine
asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
asm("nop");// 50ns on 20Mhz, 62.5ns on 16Mhz
asm("nop");// 50ns on 20Mhz, 62.5ns on 16Mhz
// read MSB
SPDR = 0;
......@@ -1197,7 +1206,7 @@ void disable_all_heaters() {
return max6675_temp;
}
#endif //HEATER_0_USES_MAX6675
#endif // HEATER_0_USES_MAX6675
/**
* Stages in the ISR loop
......@@ -1429,7 +1438,7 @@ ISR(TIMER0_COMPB_vect) {
WRITE_FAN(soft_pwm_fan > 0 ? 1 : 0);
}
if (soft_pwm_fan < pwm_count) WRITE_FAN(0);
#endif //FAN_SOFT_PWM
#endif // FAN_SOFT_PWM
pwm_count += BIT(SOFT_PWM_SCALE);
pwm_count &= 0x7f;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment