diff options
Diffstat (limited to 'src/Platform')
-rw-r--r-- | src/Platform/Event.cpp | 192 | ||||
-rw-r--r-- | src/Platform/Event.h | 64 | ||||
-rw-r--r-- | src/Platform/EventManager.cpp | 14 | ||||
-rw-r--r-- | src/Platform/EventManager.h | 23 | ||||
-rw-r--r-- | src/Platform/Logger.h | 2 | ||||
-rw-r--r-- | src/Platform/MessageType.h | 3 | ||||
-rw-r--r-- | src/Platform/Platform.h | 2 |
7 files changed, 203 insertions, 97 deletions
diff --git a/src/Platform/Event.cpp b/src/Platform/Event.cpp index ddc17eb6..5cf95a67 100644 --- a/src/Platform/Event.cpp +++ b/src/Platform/Event.cpp @@ -7,78 +7,176 @@ #include <Platform/Event.h> #include <RepRapFirmware.h> +#include <ObjectModel/ObjectModel.h> +#include <ObjectModel/Variable.h> -Event::Event(Event *p_next, EventType et, EventParameter p_param, CanAddress p_ba, uint8_t devNum) noexcept - : next(p_next), param(p_param), type(et), boardAddress(p_ba), deviceNumber(devNum) +Event *_ecv_null Event::eventsPending = nullptr; + +inline Event::Event(Event *_ecv_null pnext, EventType et, uint16_t p_param, uint8_t devNum, CanAddress p_ba, const char *_ecv_array format, va_list vargs) noexcept + : next(pnext), param(p_param), type(et), boardAddress(p_ba), deviceNumber(devNum), isBeingProcessed(false) { + text.vprintf(format, vargs); } -void Event::AppendText(const StringRef &str) const noexcept +// Queue an event unless we have a similar event pending already. Returns true if the event was added. +// The event list is held in priority order, lowest numbered (highest priority) events first. +/*static*/ bool Event::AddEvent(EventType et, uint16_t p_param, uint8_t devNum, CanAddress p_ba, const char *_ecv_array format, va_list vargs) noexcept { - // First append the event type with underscores changed to spaces - const char *p = type.ToString(); - while (*p != 0) - { - str.cat((*p == '_') ? ' ' : *p); - ++p; - } + // Search for similar events already pending or being processed. + // An event is 'similar' if it has the same type, device number and parameter even if the text is different. + TaskCriticalSectionLocker lock; - // Now append further details of the event - switch (type.ToBaseType()) + Event** pe = &eventsPending; + while (*pe != nullptr && (et >= (*pe)->type || (*pe)->isBeingProcessed)) // while the next event in the list has same or higher priority than the new one { - case EventType::Heater_fault: - str.catf("on heater %u: %s", deviceNumber, HeaterFaultType(param.heaterFaultStatus).ToString()); - break; - - case EventType::Driver_error: - case EventType::Driver_warning: + if (et == (*pe)->type && devNum == (*pe)->deviceNumber #if SUPPORT_CAN_EXPANSION - str.catf(" on %u.%u", boardAddress, deviceNumber); -#else - str.catf(" on %u", deviceNumber); + && p_ba == (*pe)->boardAddress #endif - param.driverStatus.AppendText(str, (type == EventType::Driver_error) ? 2 : 1); - break; + ) + { + return false; // there is a similar event already in the queue + } + pe = &((*pe)->next); + } + + // We didn't find a similar event, so add the new one + *pe = new Event(*pe, et, p_param, p_ba, devNum, format, vargs); + return true; +} - case EventType::Filament_error: - str.catf(" on extruder %u: %s", deviceNumber, FilamentSensorStatus(param.filamentStatus).ToString()); - break; +// Get the highest priority event and mark it as being serviced +/*static*/ bool Event::StartProcessing() noexcept +{ + TaskCriticalSectionLocker lock; - case EventType::Main_board_power_failure: - break; + Event * const ev = eventsPending; + if (ev == nullptr) + { + return false; + } + ev->isBeingProcessed = true; + return true; +} - case EventType::Trigger: - str.catf(" %u activated", deviceNumber); - break; +// Get the name of the macro that we run when this event occurs +/*static*/ void Event::GetMacroFileName(const StringRef& fname) noexcept +{ + const Event * const ep = eventsPending; + if (ep != nullptr && ep->isBeingProcessed) + { + fname.copy(ep->type.ToString()); + fname.cat(".g"); + } +} - case EventType::Mcu_temperature_warning: +// Get the macro parameters for the current event, excluding the S parameter which the caller will add +/*static*/ void Event::GetParameters(VariableSet& vars) noexcept +{ + const Event * const ep = eventsPending; + if (ep != nullptr && ep->isBeingProcessed) + { + vars.InsertNewParameter("D", ExpressionValue((int32_t)(ep->deviceNumber))); #if SUPPORT_CAN_EXPANSION - str.catf("on board %u: temperature %.1fC", boardAddress, (double)param.fVal); -#else - str.catf(": temperature %.1fC", (double)param.fVal); + vars.InsertNewParameter("B", ExpressionValue((int32_t)(ep->boardAddress))); #endif - break; + vars.InsertNewParameter("P", ExpressionValue((int32_t)(ep->param))); } } -// Append the name of the macro that we run when this event occurs -void Event::GetMacroFileName(const StringRef& fname) const noexcept +// Get the default action for the current event +/*static*/ Event::DefaultAction Event::GetDefaultAction() noexcept { - const char *p = type.ToString(); - fname.cat((char)tolower(*p++)); - while (*p != 0) + const Event * const ep = eventsPending; + if (ep != nullptr && ep->isBeingProcessed) { - if (*p == '_' && p[1] != 0) + switch (ep->type.RawValue()) { - fname.cat(toupper(p[1])); - p += 2; + case EventType::heater_fault: + case EventType::filament_error: + return DefaultAction::pauseWithMacro; + + case EventType::driver_error: + return DefaultAction::pauseNoMacro; + + default: + break; } - else + } + return DefaultAction::none; +} + +// Mark the highest priority event as completed +/*static*/ void Event::FinishedProcessing() noexcept +{ + TaskCriticalSectionLocker lock; + + const Event *ev = eventsPending; + if (ev != nullptr && ev->isBeingProcessed) + { + eventsPending = ev->next; + delete ev; + } +} + +// Get a description of the current event +/*static*/ MessageType Event::GetTextDescription(const StringRef& str) noexcept +{ + const Event * const ep = eventsPending; + if (ep != nullptr && ep->isBeingProcessed) + { + switch (ep->type.RawValue()) { - fname.cat(*p++); + case EventType::heater_fault: + { + const char *_ecv_array heaterFaultText = HeaterFaultText[max<size_t>(ep->param, ARRAY_SIZE(HeaterFaultText) - 1)]; + str.printf("Heater %u fault: %s%s", ep->deviceNumber, heaterFaultText, ep->text.c_str()); + } + return ErrorMessage; + + case EventType::filament_error: + str.printf("Filament error on extruder %u: %s", ep->deviceNumber, FilamentSensorStatus(ep->param).ToString()); + return ErrorMessage; + + case EventType::driver_error: +#if SUPPORT_CAN_EXPANSION + str.printf("Driver %u.%u error: %s", ep->boardAddress, ep->deviceNumber, ep->text.c_str()); +#else + str.printf("Driver %u error: %s", deviceNumber, ep->text.c_str()); +#endif + return ErrorMessage; + + case EventType::driver_warning: +#if SUPPORT_CAN_EXPANSION + str.printf("Driver %u.%u warning: %s", ep->boardAddress, ep->deviceNumber, ep->text.c_str()); +#else + str.printf("Driver %u warning: %s", deviceNumber, ep->text.c_str()); +#endif + return WarningMessage; + + case EventType::driver_stall: +#if SUPPORT_CAN_EXPANSION + str.printf("Driver %u.%u stall", ep->boardAddress, ep->deviceNumber); +#else + str.printf("Driver %u stall", ep->deviceNumber); +#endif + return WarningMessage; + + case EventType::main_board_power_fail: + // This does not currently generate an event, so no text + return ErrorMessage; + + case EventType::mcu_temperature_warning: +#if SUPPORT_CAN_EXPANSION + str.printf("MCU temperature warning from board %u: temperature %.1fC", ep->boardAddress, (double)((float)ep->param/10)); +#else + str.printf("MCU temperature warning: temperature %.1fC", (double)((float)ep->param/10)); +#endif + return WarningMessage; } } - fname.cat(".g"); + str.copy("Internal error in Event"); + return ErrorMessage; } // End diff --git a/src/Platform/Event.h b/src/Platform/Event.h index 46e26107..7fc78e14 100644 --- a/src/Platform/Event.h +++ b/src/Platform/Event.h @@ -3,6 +3,19 @@ * * Created on: 18 Oct 2021 * Author: David + * + * This class manages events. An event is an occurrence reported by a machine sensor that may need to be reported or may require action to be taken. + * The various event types are listed in file CANlib/RRF3Common.h. + * When an event on a main board occurs, a corresponding Event object is created and added to the event queue, unless there is a similar event already in the queue. + * When an event on an expansion board occurs, it is transmitted to the main board over CAN and then treated in the same way as a main board event. + * The event queue is kept in priority order, with the highest priority event at the head of the queue; except that if the event at the head of the queue is + * being processed, it remains at the head of the queue until processing is complete. Leaving it in the queue while it is being processed allows other similar + * events to be ignored. + * + * The event queue is emptied by the AutoPause GCode channel. It flags the entry at the head of the queue as being processed, takes whatever action is needed, + * and removes it from the queue. + * + * A main board power failure bypasses the event mechanism. Triggers do not use the event mechanism. */ #ifndef SRC_PLATFORM_EVENT_H_ @@ -13,28 +26,59 @@ #include <CoreTypes.h> #include <RRF3Common.h> #include <General/FreelistManager.h> -#include <General/StringRef.h> +#include <General/String.h> +#include <General/SafeVsnprintf.h> +#include <Platform/MessageType.h> + +class VariableSet; class Event { public: + // Type of default action for when there is no macro file to process the event + enum class DefaultAction + { + none, // do nothing other than logging ir + pauseNoMacro, // pause, but don't run pause.g + pauseWithMacro // pause, running pause.g + }; + void* operator new(size_t sz) noexcept { return FreelistManager::Allocate<Event>(); } void operator delete(void* p) noexcept { FreelistManager::Release<Event>(p); } - Event(Event *p_next, EventType et, EventParameter p_param, CanAddress p_ba, uint8_t devNum) noexcept; + // Get a description of the current event and return the appropriate message type + static MessageType GetTextDescription(const StringRef& str) noexcept; + + // Queue an event, or release it if we have a similar event pending already. Returns true if the event was added, false if it was released. + static bool AddEvent(EventType et, uint16_t p_param, CanAddress p_ba, uint8_t devNum, const char *_ecv_array format, va_list vargs) noexcept; - // Append a description of the event to a string - void AppendText(const StringRef& str) const noexcept; + // Get the highest priority event if there is one start processing it + static bool StartProcessing() noexcept; // Get the name of the macro that we run when this event occurs - void GetMacroFileName(const StringRef& fname) const noexcept; + static void GetMacroFileName(const StringRef& fname) noexcept; + + // Get the parameters for invoking the macro file the current event + static void GetParameters(VariableSet& vars) noexcept; + + // Get the default action for the current event + static DefaultAction GetDefaultAction() noexcept; + + // Mark the highest priority event as completed + static void FinishedProcessing() noexcept; private: - Event *next; // next event in a linked list - EventParameter param; // details about the event - EventType type; // what type of event it is - CanAddress boardAddress; // which board it came from - uint8_t deviceNumber; // which device raised it + Event(Event *_ecv_null pnext, EventType et, uint16_t p_param, uint8_t devNum, CanAddress p_ba, const char *_ecv_array format, va_list vargs) noexcept; + + Event *_ecv_null next; // next event in a linked list + uint16_t param; // details about the event, e.g. for a heater fault it is the type of the fault + EventType type; // what type of event it is + CanAddress boardAddress; // which board it came from + uint8_t deviceNumber; // which device raised it, e.g. heater number, driver number, trigger number + volatile bool isBeingProcessed; // true if this event is being processed, so it must remain at the head of the queue + String<50> text; // additional info to display to the user + + static Event * _ecv_null eventsPending; // linked list of events waiting to be processed }; #endif /* SRC_PLATFORM_EVENT_H_ */ diff --git a/src/Platform/EventManager.cpp b/src/Platform/EventManager.cpp deleted file mode 100644 index e0d4c3f5..00000000 --- a/src/Platform/EventManager.cpp +++ /dev/null @@ -1,14 +0,0 @@ -/* - * EventManager.cpp - * - * Created on: 19 Oct 2021 - * Author: David - */ - -#include "EventManager.h" - -EventManager::EventManager() : eventsPending(nullptr), lastWarningMillis(0) -{ -} - -// End diff --git a/src/Platform/EventManager.h b/src/Platform/EventManager.h deleted file mode 100644 index 8f3d23c9..00000000 --- a/src/Platform/EventManager.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * EventManager.h - * - * Created on: 19 Oct 2021 - * Author: David - */ - -#ifndef SRC_PLATFORM_EVENTMANAGER_H_ -#define SRC_PLATFORM_EVENTMANAGER_H_ - -#include "Event.h" - -class EventManager -{ -public: - EventManager(); - -private: - Event *eventsPending; // linked list of pending events - uint32_t lastWarningMillis; // when we last sent a warning message -}; - -#endif /* SRC_PLATFORM_EVENTMANAGER_H_ */ diff --git a/src/Platform/Logger.h b/src/Platform/Logger.h index b2eee38b..b612bb7d 100644 --- a/src/Platform/Logger.h +++ b/src/Platform/Logger.h @@ -40,7 +40,7 @@ public: private: NamedEnum(MessageLogLevel, uint8_t, debug, info, warn, off); - MessageLogLevel GetMessageLogLevel(MessageType mt) const noexcept { return (MessageLogLevel) ((mt & MessageType::LogOff)>>30); } + MessageLogLevel GetMessageLogLevel(MessageType mt) const noexcept { return (MessageLogLevel) ((mt & MessageType::LogLevelMask) >> MessageType::LogLevelShift); } static const uint8_t LogEnabledThreshold = 3; diff --git a/src/Platform/MessageType.h b/src/Platform/MessageType.h index 482732bb..4da31ab6 100644 --- a/src/Platform/MessageType.h +++ b/src/Platform/MessageType.h @@ -41,8 +41,11 @@ enum MessageType : uint32_t RawMessageFlag = 0x8000000u, // Do not encapsulate this message BinaryCodeReplyFlag = 0x10000000u, // This message comes from a binary G-Code buffer PushFlag = 0x20000000u, // There is more to come; the message has been truncated + LogMessageLowBit = 0x40000000u, // Log level consists of two bits this is the low bit LogMessageHighBit = 0x80000000u, // Log level consists of two bits this is the high bit + LogLevelMask = 0xC0000000u, // Mask for all the log level bits + LogLevelShift = 30, // How many bits we have to shift a MessageType right by to get the logging level // Common combinations NoDestinationMessage = 0u, // A message that is going nowhere diff --git a/src/Platform/Platform.h b/src/Platform/Platform.h index 201e10f2..33d748da 100644 --- a/src/Platform/Platform.h +++ b/src/Platform/Platform.h @@ -31,7 +31,6 @@ Licence: GPL #include <Heating/TemperatureError.h> #include "OutputMemory.h" #include "UniqueId.h" -#include "EventManager.h" #include <Storage/FileStore.h> #include <Storage/FileData.h> #include <Storage/MassStorage.h> // must be after Pins.h because it needs NumSdCards defined @@ -873,7 +872,6 @@ private: #endif // Event handling - EventManager eventManager; uint32_t lastDriverPollMillis; // when we last checked the drivers and voltage monitoring #ifdef DUET3MINI |