Merge pull request #809 from mkbel/Use_Timer

Use timer
This commit is contained in:
XPila 2018-06-05 20:54:41 +02:00 committed by GitHub
commit 6a8425f62d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 2581 additions and 63 deletions

6
.gitignore vendored Normal file
View file

@ -0,0 +1,6 @@
.settings
.project
.cproject
Debug
Firmware/Configuration_prusa.h
Firmware/Doc

View file

@ -1,30 +1,46 @@
/* -*- c++ -*- */
/*
Reprap firmware based on Sprinter and grbl.
Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
/**
* @file
*/
/*
This firmware is a mashup between Sprinter and grbl.
(https://github.com/kliment/Sprinter)
(https://github.com/simen/grbl/tree)
It has preliminary support for Matthew Roberts advance algorithm
http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
/**
* @mainpage Reprap 3D printer firmware based on Sprinter and grbl.
*
* @section intro_sec Introduction
*
* This firmware is a mashup between Sprinter and grbl.
* https://github.com/kliment/Sprinter
* https://github.com/simen/grbl/tree
*
* It has preliminary support for Matthew Roberts advance algorithm
* http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
*
* Prusa Research s.r.o. https://www.prusa3d.cz
*
* @section copyright_sec Copyright
*
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @section notes_sec Notes
*
* * Do not create static objects in global functions.
* Otherwise constructor guard against concurrent calls is generated costing
* about 8B RAM and 14B flash.
*
*
*/
#include "Marlin.h"
@ -305,6 +321,7 @@ float pause_lastpos[4];
unsigned long pause_time = 0;
unsigned long start_pause_print = millis();
unsigned long t_fan_rising_edge = millis();
static LongTimer safetyTimer;
//unsigned long load_filament_time;
@ -2900,7 +2917,7 @@ static void gcode_PRUSA_SN()
selectedSerialPort = 0;
MSerial.write(";S");
int numbersRead = 0;
Timer timeout;
ShortTimer timeout;
timeout.start();
while (numbersRead < 19) {
@ -2911,7 +2928,7 @@ static void gcode_PRUSA_SN()
numbersRead++;
selectedSerialPort = 0;
}
if (timeout.expired(100)) break;
if (timeout.expired(100u)) break;
}
selectedSerialPort = 1;
MSerial.write('\n');
@ -7241,7 +7258,6 @@ static void handleSafetyTimer()
#if (EXTRUDERS > 1)
#error Implemented only for one extruder.
#endif //(EXTRUDERS > 1)
static Timer safetyTimer;
if (IS_SD_PRINTING || is_usb_printing || isPrintPaused || (custom_message_type == 4) || saved_printing
|| (lcd_commands_type == LCD_COMMAND_V2_CAL) || (!degTargetBed() && !degTargetHotend(0)))
{

View file

@ -12,14 +12,16 @@
* It is guaranteed, that construction is equivalent with zeroing all members.
* This property can be exploited in MenuData union.
*/
Timer::Timer() : m_isRunning(false), m_started()
template<typename T>
Timer<T>::Timer() : m_isRunning(false), m_started()
{
}
/**
* @brief Start timer
*/
void Timer::start()
template<typename T>
void Timer<T>::start()
{
m_started = millis();
m_isRunning = true;
@ -29,19 +31,21 @@ void Timer::start()
* @brief Timer has expired
*
* Timer is considered expired after msPeriod has passed from time the timer was started.
* This function must be called at least each (unsigned long maximum value - msPeriod) milliseconds to be sure to
* Timer is stopped after expiration.
* This function must be called at least each (T maximum value - msPeriod) milliseconds to be sure to
* catch first expiration.
* This function is expected to handle wrap around of time register well.
*
* @param msPeriod Time interval in milliseconds.
* @param msPeriod Time interval in milliseconds. Do not omit "ul" when using constant literal with LongTimer.
* @retval true Timer has expired
* @retval false Timer not expired yet, or is not running, or time window in which is timer considered expired passed.
*/
bool Timer::expired(unsigned long msPeriod)
template<typename T>
bool Timer<T>::expired(T msPeriod)
{
if (!m_isRunning) return false;
bool expired = false;
const unsigned long now = millis();
const T now = millis();
if (m_started <= m_started + msPeriod)
{
if ((now >= m_started + msPeriod) || (now < m_started))
@ -59,3 +63,6 @@ bool Timer::expired(unsigned long msPeriod)
if (expired) m_isRunning = false;
return expired;
}
template class Timer<unsigned long>;
template class Timer<unsigned short>;

View file

@ -1,4 +1,4 @@
/*
/**
* @file
* @author Marek Bel
*/
@ -10,10 +10,10 @@
* @brief simple timer
*
* Simple and memory saving implementation. Should handle timer register wrap around well.
* Maximum period is at least 49 days. Resolution is one millisecond. To save memory, doesn't store timer period.
* If you wish timer which is storing period, derive from this. If you need time intervals smaller than 65 seconds
* consider implementing timer with smaller underlying type.
* Resolution is one millisecond. To save memory, doesn't store timer period.
* If you wish timer which is storing period, derive from this.
*/
template <class T>
class Timer
{
public:
@ -21,10 +21,23 @@ public:
void start();
void stop(){m_isRunning = false;}
bool running(){return m_isRunning;}
bool expired(unsigned long msPeriod);
bool expired(T msPeriod);
private:
bool m_isRunning;
unsigned long m_started;
T m_started;
};
/**
* @brief Timer unsigned long specialization
*
* Maximum period is at least 49 days.
*/
using LongTimer = Timer<unsigned long>;
/**
* @brief Timer unsigned short specialization
*
* Maximum period is at least 65 seconds.
*/
using ShortTimer = Timer<unsigned short>;
#endif /* TIMER_H */

2494
Firmware/doxyfile Normal file

File diff suppressed because it is too large Load diff

View file

@ -111,7 +111,7 @@ union MenuData
struct AutoLoadFilamentMenu
{
//Timer timer;
//ShortTimer timer;
char dummy;
} autoLoadFilamentMenu;
struct _Lcd_moveMenu
@ -168,14 +168,13 @@ uint8_t farm_mode = 0;
int farm_no = 0;
int farm_timer = 8;
int farm_status = 0;
unsigned long allert_timer = millis();
bool printer_connected = true;
unsigned long display_time; //just timer for showing pid finished message on lcd;
float pid_temp = DEFAULT_PID_TEMP;
bool long_press_active = false;
long long_press_timer = millis();
static ShortTimer longPressTimer;
unsigned long button_blanking_time = millis();
bool button_pressed = false;
@ -2090,7 +2089,7 @@ static void lcd_menu_AutoLoadFilament()
}
else
{
Timer* ptimer = (Timer*)&(menuData.autoLoadFilamentMenu.dummy);
ShortTimer* ptimer = (ShortTimer*)&(menuData.autoLoadFilamentMenu.dummy);
if (!ptimer->running()) ptimer->start();
lcd.setCursor(0, 0);
lcd_printPGM(_T(MSG_ERROR));
@ -2675,7 +2674,7 @@ bool lcd_wait_for_pinda(float temp) {
lcd_set_custom_characters_degree();
setTargetHotend(0, 0);
setTargetBed(0);
Timer pinda_timeout;
LongTimer pinda_timeout;
pinda_timeout.start();
bool target_temp_reached = true;
@ -5085,20 +5084,6 @@ static void lcd_disable_farm_mode() {
}
static void lcd_ping_allert() {
if ((abs(millis() - allert_timer)*0.001) > PING_ALLERT_PERIOD) {
allert_timer = millis();
SET_OUTPUT(BEEPER);
for (int i = 0; i < 2; i++) {
WRITE(BEEPER, HIGH);
delay(50);
WRITE(BEEPER, LOW);
delay(100);
}
}
};
#ifdef SNMM
@ -7684,7 +7669,6 @@ void lcd_ping() { //chceck if printer is connected to monitoring when in farm mo
//if there are comamnds in buffer, some long gcodes can delay execution of ping command
//therefore longer period is used
printer_connected = false;
//lcd_ping_allert(); //acustic signals
}
else {
lcd_printer_connected();
@ -7775,12 +7759,11 @@ void lcd_buttons_update()
if (millis() > button_blanking_time) {
button_blanking_time = millis() + BUTTON_BLANKING_TIME;
if (button_pressed == false && long_press_active == false) {
long_press_timer = millis();
longPressTimer.start();
button_pressed = true;
}
else {
if (millis() - long_press_timer > LONG_PRESS_TIME) { //long press activated
if (longPressTimer.expired(LONG_PRESS_TIME)) {
long_press_active = true;
move_menu_scale = 1.0;
menu_action_submenu(lcd_move_z);

View file

@ -263,7 +263,6 @@ static float count_e(float layer_heigth, float extrusion_width, float extrusion_
static void lcd_babystep_z();
void stack_error();
static void lcd_ping_allert();
void lcd_printer_connected();
void lcd_ping();