From d6e77f04f76b177585718c50de39148fdb00f2d9 Mon Sep 17 00:00:00 2001 From: Federico Terraneo <fede.tft@miosix.org> Date: Sat, 13 May 2017 23:30:17 +0200 Subject: [PATCH] Improved Leds class: removed the need for init(), added comments, used array instead of vector --- src/entrypoints/anakin-demo-board.cpp | 1 - src/entrypoints/anakin-test-dma.cpp | 1 - src/shared/Leds.cpp | 10 ++++++-- src/shared/Leds.h | 37 +++++++++++---------------- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/entrypoints/anakin-demo-board.cpp b/src/entrypoints/anakin-demo-board.cpp index 6333b1fa1..7ee1603c4 100644 --- a/src/entrypoints/anakin-demo-board.cpp +++ b/src/entrypoints/anakin-demo-board.cpp @@ -352,7 +352,6 @@ int main() { int ignore_ctr = 100; int led_status = 0x200; - Leds::init(); while(1) { sDemoBoard->update(); Thread::sleep(20); diff --git a/src/entrypoints/anakin-test-dma.cpp b/src/entrypoints/anakin-test-dma.cpp index 8242bfaee..820023085 100644 --- a/src/entrypoints/anakin-test-dma.cpp +++ b/src/entrypoints/anakin-test-dma.cpp @@ -60,7 +60,6 @@ void fifoQueueSz(void *arg) int main() { printf("\n"); - Leds::init(); Leds::set(0); Log::getInstance(); sBoard->init(); diff --git a/src/shared/Leds.cpp b/src/shared/Leds.cpp index c05ea860b..7141a4f69 100644 --- a/src/shared/Leds.cpp +++ b/src/shared/Leds.cpp @@ -1,5 +1,5 @@ /* Copyright (c) 2016-2017 Skyward Experimental Rocketry - * Authors: Alain Carlucci + * Authors: Alain Carlucci, Federico Terraneo * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,4 +22,10 @@ #include "Leds.h" -std::vector<GpioPin> Leds::pins; +//Relying on the BSP to provide leds and configure them as output +#define LED(x) miosix::leds::led##x::getPin() +std::array<miosix::GpioPin,Leds::numLeds> Leds::pins{{ + LED(0), LED(1), LED(2), LED(3), LED(4), + LED(5), LED(6), LED(7), LED(8), LED(9) +}}; +#undef LED diff --git a/src/shared/Leds.h b/src/shared/Leds.h index e2d841c25..fc69af165 100644 --- a/src/shared/Leds.h +++ b/src/shared/Leds.h @@ -1,5 +1,5 @@ /* Copyright (c) 2016 Skyward Experimental Rocketry - * Authors: Alain Carlucci, Matteo Michele Piazzolla + * Authors: Alain Carlucci, Matteo Michele Piazzolla, Federico Terraneo * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -24,22 +24,23 @@ #define LEDS_H #include <Common.h> +#include <array> -using namespace std; -using namespace miosix; class Leds { public: - - /** Every bit of the param leds is the value for the n-th led. - * For example leds = 0x201 means (only) first and last led on. + /** + * Turn on/off all leds using a bitmask + * \param leds bitmask specifing which led has to be turned on */ static void set(uint16_t leds) { for(int i=0;i<10;i++) set(i, (leds & (1 << i)) != 0); } - /** Param id should be in range [0,10). */ - static void set(uint8_t id, bool enable) { + /** + * \param id should be in range [0,10). + */ + static void set(uint8_t id, bool enable) { if(id >= pins.size()) return; @@ -49,21 +50,13 @@ public: pins[id].low(); } - /** Call this function on board startup, before the first set() call. */ - inline static void init() { - #define PIN(x,y) Gpio<GPIO##x##_BASE, y>::getPin() - pins = { - PIN(B, 0), PIN(A,15), PIN(B, 4), PIN(C, 4), PIN(C, 5), - PIN(F, 6), PIN(D, 3), PIN(D, 4), PIN(G, 2), PIN(G, 3) - }; - #undef PIN - for(auto& pin : pins) - pin.mode(Mode::OUTPUT); - } - private: - static vector<GpioPin> pins; - Leds() {} + static const int numLeds=10; + static std::array<miosix::GpioPin,numLeds> pins; + + Leds()=delete; + Leds(const Leds&)=delete; + Leds& operator=(const Leds&)=delete; }; #endif -- GitLab