diff --git a/demos/ARMCM4-STM32F407-DISCOVERY-MEMS-G++/main.cpp b/demos/ARMCM4-STM32F407-DISCOVERY-MEMS-G++/main.cpp index 5794ac798..3851e1d29 100644 --- a/demos/ARMCM4-STM32F407-DISCOVERY-MEMS-G++/main.cpp +++ b/demos/ARMCM4-STM32F407-DISCOVERY-MEMS-G++/main.cpp @@ -24,7 +24,6 @@ #include "evtimer.h" using namespace chibios_rt; -#if 0 /* * LED blink sequences. @@ -43,43 +42,45 @@ typedef struct { uint32_t value; } seqop_t; -// Flashing sequence for LED1. -static const seqop_t LED1_sequence[] = +// Flashing sequence for LED4. +static const seqop_t LED4_sequence[] = { {BITCLEAR, PAL_PORT_BIT(GPIOD_LED4)}, {SLEEP, 200}, {BITSET, PAL_PORT_BIT(GPIOD_LED4)}, - {SLEEP, 800}, - {BITCLEAR, PAL_PORT_BIT(GPIOD_LED4)}, - {SLEEP, 400}, - {BITSET, PAL_PORT_BIT(GPIOD_LED4)}, - {SLEEP, 600}, - {BITCLEAR, PAL_PORT_BIT(GPIOD_LED4)}, - {SLEEP, 600}, - {BITSET, PAL_PORT_BIT(GPIOD_LED4)}, - {SLEEP, 400}, - {BITCLEAR, PAL_PORT_BIT(GPIOD_LED4)}, - {SLEEP, 800}, - {BITSET, PAL_PORT_BIT(GPIOD_LED4)}, - {SLEEP, 200}, + {SLEEP, 1800}, {GOTO, 0} }; +// Flashing sequence for LED3. +static const seqop_t LED3_sequence[] = +{ + {SLEEP, 1000}, + {BITCLEAR, PAL_PORT_BIT(GPIOD_LED3)}, + {SLEEP, 1800}, + {BITSET, PAL_PORT_BIT(GPIOD_LED3)}, + {SLEEP, 200}, + {GOTO, 1} +}; + /* * Sequencer thread class. It can drive LEDs or other output pins. * Any sequencer is just an instance of this class, all the details are * totally encapsulated and hidden to the application level. */ -class SequencerThread : public EnhancedThread<128> { +class SequencerThread : public BaseStaticThread<128> { private: const seqop_t *base, *curr; // Thread local variables. protected: virtual msg_t Main(void) { + + setName("sequencer"); + while (true) { switch(curr->action) { case SLEEP: - Sleep(curr->value); + sleep(curr->value); break; case GOTO: curr = &base[curr->value]; @@ -98,7 +99,7 @@ protected: } public: - SequencerThread(const seqop_t *sequence) : EnhancedThread<128>("sequencer") { + SequencerThread(const seqop_t *sequence) : BaseStaticThread<128>() { base = curr = sequence; } @@ -107,41 +108,30 @@ public: /* * Tester thread class. This thread executes the test suite. */ -class TesterThread : public EnhancedThread<256> { +class TesterThread : public BaseStaticThread<256> { protected: virtual msg_t Main(void) { + setName("tester"); + return TestThread(&SD2); } public: - TesterThread(void) : EnhancedThread<256>("tester") { + TesterThread(void) : BaseStaticThread<256>() { } }; -/* - * Executed as an event handler at 500mS intervals. - */ -static void TimerHandler(eventid_t id) { - - (void)id; - if (palReadPad(GPIOA, GPIOA_BUTTON)) { - TesterThread tester; - tester.Wait(); - }; -} -#endif +/* Static threads instances.*/ +static TesterThread tester; +static SequencerThread blinker1(LED3_sequence); +static SequencerThread blinker2(LED4_sequence); /* * Application entry point. */ int main(void) { -/* static const evhandler_t evhndl[] = { - TimerHandler - }; - static EvTimer evt; - struct EventListener el0;*/ /* * System initializations. @@ -157,22 +147,23 @@ int main(void) { * Activates the serial driver 2 using the driver default configuration. */ sdStart(&SD2, NULL); -/* - evtInit(&evt, 500); // Initializes an event timer. - evtStart(&evt); // Starts the event timer. - chEvtRegister(&evt.et_es, &el0, 0); // Registers a listener on the source. -*/ + /* * Starts several instances of the SequencerThread class, each one operating * on a different LED. */ -// SequencerThread blinker1(LED1_sequence); + blinker1.start(NORMALPRIO + 10); + blinker2.start(NORMALPRIO + 10); /* * Serves timer events. */ -// while (true) -// Event::Dispatch(evhndl, Event::WaitOne(ALL_EVENTS)); + while (true) { + if (palReadPad(GPIOA, GPIOA_BUTTON)) { + tester.start(NORMALPRIO); + tester.wait(); + }; + } return 0; } diff --git a/os/various/cpp_wrappers/ch.cpp b/os/various/cpp_wrappers/ch.cpp index 99f7440c9..1ada921da 100644 --- a/os/various/cpp_wrappers/ch.cpp +++ b/os/various/cpp_wrappers/ch.cpp @@ -281,6 +281,12 @@ namespace chibios_rt { return chEvtWaitAllTimeout(ewmask, time); } #endif /* CH_USE_EVENTS_TIMEOUT */ + + void BaseThread::dispatchEvents(const evhandler_t handlers[], + eventmask_t mask) { + + chEvtDispatch(handlers, mask); + } #endif /* CH_USE_EVENTS */ #if CH_USE_SEMAPHORES @@ -384,55 +390,47 @@ namespace chibios_rt { #if CH_USE_EVENTS /*------------------------------------------------------------------------* - * chibios_rt::EventListener * + * chibios_rt::EvtListener * *------------------------------------------------------------------------*/ - flagsmask_t EventListener::getAndClearFlags(void) { + flagsmask_t EvtListener::getAndClearFlags(void) { return chEvtGetAndClearFlags(&ev_listener); } /*------------------------------------------------------------------------* - * chibios_rt::EventSource * + * chibios_rt::EvtSource * *------------------------------------------------------------------------*/ - EventSource::EventSource(void) { + EvtSource::EvtSource(void) { - ev_source.es_next = (::EventListener *)(void *)&ev_source; + chEvtInit(&ev_source); } - void EventSource::registerOne(chibios_rt::EventListener *elp, + void EvtSource::registerOne(chibios_rt::EvtListener *elp, eventid_t eid) { chEvtRegister(&ev_source, &elp->ev_listener, eid); } - void EventSource::registerMask(chibios_rt::EventListener *elp, + void EvtSource::registerMask(chibios_rt::EvtListener *elp, eventmask_t emask) { chEvtRegisterMask(&ev_source, &elp->ev_listener, emask); } - void EventSource::unregister(chibios_rt::EventListener *elp) { + void EvtSource::unregister(chibios_rt::EvtListener *elp) { chEvtUnregister(&ev_source, &elp->ev_listener); } - void EventSource::broadcastFlags(flagsmask_t flags) { + void EvtSource::broadcastFlags(flagsmask_t flags) { chEvtBroadcastFlags(&ev_source, flags); } - void EventSource::broadcastFlagsI(flagsmask_t flags) { + void EvtSource::broadcastFlagsI(flagsmask_t flags) { chEvtBroadcastFlagsI(&ev_source, flags); } - - /*------------------------------------------------------------------------* - * chibios_rt::Event * - *------------------------------------------------------------------------*/ - void Event::dispatch(const evhandler_t handlers[], eventmask_t mask) { - - chEvtDispatch(handlers, mask); - } #endif /* CH_USE_EVENTS */ } diff --git a/os/various/cpp_wrappers/ch.hpp b/os/various/cpp_wrappers/ch.hpp index 0178f346f..6d0eb66c7 100644 --- a/os/various/cpp_wrappers/ch.hpp +++ b/os/various/cpp_wrappers/ch.hpp @@ -517,6 +517,18 @@ namespace chibios_rt { static eventmask_t waitAllEventsTimeout(eventmask_t ewmask, systime_t time); #endif /* CH_USE_EVENTS_TIMEOUT */ + + /** + * @brief Invokes the event handlers associated to an event flags mask. + * + * @param[in] mask mask of the event flags to be dispatched + * @param[in] handlers an array of @p evhandler_t. The array must have size + * equal to the number of bits in eventmask_t. + * + * @api + */ + static void dispatchEvents(const evhandler_t handlers[], + eventmask_t mask); #endif /* CH_USE_EVENTS */ }; @@ -783,15 +795,15 @@ namespace chibios_rt { #if CH_USE_EVENTS || defined(__DOXYGEN__) /*------------------------------------------------------------------------* - * chibios_rt::EventListener * + * chibios_rt::EvtListener * *------------------------------------------------------------------------*/ /** * @brief Class encapsulating an event listener. */ - class EventListener { + class EvtListener { public: /** - * @brief Embedded @p ::EventListener structure. + * @brief Embedded @p ::EvtListener structure. */ struct ::EventListener ev_listener; @@ -808,48 +820,48 @@ namespace chibios_rt { }; /*------------------------------------------------------------------------* - * chibios_rt::EventSource * + * chibios_rt::EvtSource * *------------------------------------------------------------------------*/ /** * @brief Class encapsulating an event source. */ - class EventSource { + class EvtSource { public: /** - * @brief Embedded @p ::EventSource structure. + * @brief Embedded @p ::EvtSource structure. */ struct ::EventSource ev_source; /** - * @brief EventSource object constructor. - * @details The embedded @p ::EventSource structure is initialized. + * @brief EvtSource object constructor. + * @details The embedded @p ::EvtSource structure is initialized. * * @api */ - EventSource(void); + EvtSource(void); /** * @brief Registers a listener on the event source. * - * @param[in] elp pointer to the @p EventListener object + * @param[in] elp pointer to the @p EvtListener object * @param[in] eid numeric identifier assigned to the Event * Listener * * @api */ - void registerOne(chibios_rt::EventListener *elp, eventid_t eid); + void registerOne(chibios_rt::EvtListener *elp, eventid_t eid); /** * @brief Registers an Event Listener on an Event Source. * @note Multiple Event Listeners can specify the same bits to be added. * - * @param[in] elp pointer to the @p EventListener object + * @param[in] elp pointer to the @p EvtListener object * @param[in] emask the mask of event flags to be pended to the * thread when the event source is broadcasted * * @api */ - void registerMask(chibios_rt::EventListener *elp, eventmask_t emask); + void registerMask(chibios_rt::EvtListener *elp, eventmask_t emask); /** * @brief Unregisters a listener. @@ -860,7 +872,7 @@ namespace chibios_rt { * * @api */ - void unregister(chibios_rt::EventListener *elp); + void unregister(chibios_rt::EvtListener *elp); /** * @brief Broadcasts on an event source. @@ -886,25 +898,6 @@ namespace chibios_rt { */ void broadcastFlagsI(flagsmask_t flags); }; - - /** - * @brief Class encapsulating an event-related functionalities. - */ - class Event { - public: - - /** - * @brief Invokes the event handlers associated with a mask. - * - * @param[in] mask mask of the events to be dispatched - * @param[in] handlers an array of @p evhandler_t. The array must be - * have indexes from zero up the higher registered - * event identifier. - * - * @api - */ - static void dispatch(const evhandler_t handlers[], eventmask_t mask); - }; #endif /* CH_USE_EVENTS */ }