Longan Nano
Longan Nano Demo
longan_nano_led.hpp
Go to the documentation of this file.
1 /**********************************************************************************
2 BSD 3-Clause License
3 
4 Copyright (c) 2020, Orso Eric
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice, this
11  list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its
18  contributors may be used to endorse or promote products derived from
19  this software without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 **********************************************************************************/
32 
33 /**********************************************************************************
34 ** ENVIROMENT VARIABILE
35 **********************************************************************************/
36 
37 #ifndef LONGAN_NANO_LED_H_
38  #define LONGAN_NANO_LED_H_
39 
40 /**********************************************************************************
41 ** GLOBAL INCLUDES
42 **********************************************************************************/
43 
44 #include <gd32vf103.h>
45 
46 /**********************************************************************************
47 ** DEFINES
48 **********************************************************************************/
49 
50 /**********************************************************************************
51 ** MACROS
52 **********************************************************************************/
53 
54 /**********************************************************************************
55 ** NAMESPACE
56 **********************************************************************************/
57 
59 namespace Longan_nano
60 {
61 
62 /**********************************************************************************
63 ** TYPEDEFS
64 **********************************************************************************/
65 
66 /**********************************************************************************
67 ** PROTOTYPE: STRUCTURES
68 **********************************************************************************/
69 
70 /**********************************************************************************
71 ** PROTOTYPE: GLOBAL VARIABILES
72 **********************************************************************************/
73 
74 /**********************************************************************************
75 ** PROTOTYPE: CLASS
76 **********************************************************************************/
77 
78 /************************************************************************************/
80 /************************************************************************************/
89 /************************************************************************************/
90 
91 class Leds
92 {
93  //Visible to all
94  public:
95  //--------------------------------------------------------------------------
96  // ENUM
97  //--------------------------------------------------------------------------
98  // Scope enums inside class in order to encapsulate them where they logically belong
99 
101  typedef enum _Color
102  {
107  WHITE
108  } Color;
109 
111  typedef enum _Led_gpio
112  {
113  RED_GPIO = GPIOC,
114  GREEN_GPIO = GPIOA,
115  BLUE_GPIO = GPIOA,
117 
119  typedef enum _Led_pin
120  {
121  RED_PIN = GPIO_PIN_13,
122  GREEN_PIN = GPIO_PIN_1,
123  BLUE_PIN = GPIO_PIN_2,
125 
126  //--------------------------------------------------------------------------
127  // CONSTRUCTORS
128  //--------------------------------------------------------------------------
129 
131  Leds( void );
132 
133  //--------------------------------------------------------------------------
134  // DESTRUCTORS
135  //--------------------------------------------------------------------------
136 
138  ~Leds( void );
139 
140  //--------------------------------------------------------------------------
141  // OPERATORS
142  //--------------------------------------------------------------------------
143 
144  //--------------------------------------------------------------------------
145  // SETTERS
146  //--------------------------------------------------------------------------
147 
148  //--------------------------------------------------------------------------
149  // GETTERS
150  //--------------------------------------------------------------------------
151 
152  //--------------------------------------------------------------------------
153  // TESTERS
154  //--------------------------------------------------------------------------
155 
156  //--------------------------------------------------------------------------
157  // PUBLIC METHODS
158  //--------------------------------------------------------------------------
159 
160  /***************************************************************************/
163  /***************************************************************************/
167  /***************************************************************************/
168 
169  static inline bool init( void )
170  {
171  //----------------------------------------------------------------
172  // VARS
173  //----------------------------------------------------------------
174 
175  //----------------------------------------------------------------
176  // BODY
177  //----------------------------------------------------------------
178 
179  //Clock the GPIO banks
180  rcu_periph_clock_enable(RCU_GPIOA);
181  rcu_periph_clock_enable(RCU_GPIOC);
182  //Setup the R (PC13), G (PA1) and B (PA2) LEDs
183  gpio_init(Led_gpio::RED_GPIO, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,Led_pin::RED_PIN);
184  gpio_init(Led_gpio::GREEN_GPIO, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,Led_pin::GREEN_PIN);
185  gpio_init(Led_gpio::BLUE_GPIO, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,Led_pin::BLUE_PIN);
186 
187  //----------------------------------------------------------------
188  // RETURN
189  //----------------------------------------------------------------
190 
191  return false; //OK
192  } //End public static method: init
193 
194  /***************************************************************************/
197  /***************************************************************************/
202  /***************************************************************************/
203 
204  static inline bool set_color( Leds::Color color )
205  {
206  //----------------------------------------------------------------
207  // VARS
208  //----------------------------------------------------------------
209 
210  //LED are inverted. SET turn them OFF
211  bit_status led_r = FlagStatus::SET;
212  bit_status led_g = FlagStatus::SET;
213  bit_status led_b = FlagStatus::SET;
214 
215  //----------------------------------------------------------------
216  // BODY
217  //----------------------------------------------------------------
218 
219  //Switch: Color
220  switch(color)
221  {
222  case (Leds::Color::BLACK):
223  {
224 
225  break;
226  }
227  case (Leds::Color::WHITE):
228  {
229  led_r = FlagStatus::RESET;
230  led_g = FlagStatus::RESET;
231  led_b = FlagStatus::RESET;
232 
233  break;
234  }
235  default:
236  {
237  return true; //FAIL
238  }
239  } //End Switch: Color
240 
241  //Apply the color setting
242  gpio_bit_write( Led_gpio::RED_GPIO, Led_pin::RED_PIN, (bit_status)led_r );
243  gpio_bit_write( Led_gpio::GREEN_GPIO, Led_pin::GREEN_PIN, (bit_status)led_g );
244  gpio_bit_write( Led_gpio::BLUE_GPIO, Led_pin::BLUE_PIN, (bit_status)led_b );
245 
246  //----------------------------------------------------------------
247  // RETURN
248  //----------------------------------------------------------------
249 
250  return false; //OK
251  } //End public static method: set_color
252 
253  /***************************************************************************/
256  /***************************************************************************/
260  /***************************************************************************/
261 
262  static inline bool toggle( Leds::Color color )
263  {
264  //----------------------------------------------------------------
265  // VARS
266  //----------------------------------------------------------------
267 
268  //----------------------------------------------------------------
269  // BODY
270  //----------------------------------------------------------------
271 
272  //Switch: Color
273  switch(color)
274  {
275  case (Leds::Color::RED):
276  {
277  gpio_bit_write(Led_gpio::RED_GPIO, Led_pin::RED_PIN, (bit_status)(1-gpio_input_bit_get(Led_gpio::RED_GPIO, Led_pin::RED_PIN)));
278  break;
279  }
280  case (Leds::Color::GREEN):
281  {
282  gpio_bit_write(Led_gpio::GREEN_GPIO, Led_pin::GREEN_PIN, (bit_status)(1-gpio_input_bit_get(Led_gpio::GREEN_GPIO, Led_pin::GREEN_PIN)));
283  break;
284  }
285  case (Leds::Color::BLUE):
286  {
287  gpio_bit_write(Led_gpio::BLUE_GPIO, Led_pin::BLUE_PIN, (bit_status)(1-gpio_input_bit_get(Led_gpio::BLUE_GPIO, Led_pin::BLUE_PIN)));
288  break;
289  }
290  default:
291  {
292  return true; //FAIL
293  }
294  } //End Switch: Color
295 
296  //----------------------------------------------------------------
297  // RETURN
298  //----------------------------------------------------------------
299 
300  return false; //OK
301  } //End public static method: toggle
302 
303  /***************************************************************************/
306  /***************************************************************************/
310  /***************************************************************************/
311 
312  static inline bool clear( Leds::Color color )
313  {
314  //----------------------------------------------------------------
315  // VARS
316  //----------------------------------------------------------------
317 
318  //----------------------------------------------------------------
319  // BODY
320  //----------------------------------------------------------------
321 
322  //Switch: Color
323  switch(color)
324  {
325  case (Leds::Color::RED):
326  {
327  gpio_bit_set(Led_gpio::RED_GPIO, Led_pin::RED_PIN);
328  break;
329  }
330  case (Leds::Color::GREEN):
331  {
332  gpio_bit_set(Led_gpio::GREEN_GPIO, Led_pin::GREEN_PIN);
333  break;
334  }
335  case (Leds::Color::BLUE):
336  {
337  gpio_bit_set(Led_gpio::BLUE_GPIO, Led_pin::BLUE_PIN);
338  break;
339  }
340  default:
341  {
342  return true; //FAIL
343  }
344  } //End Switch: Color
345 
346  //----------------------------------------------------------------
347  // RETURN
348  //----------------------------------------------------------------
349 
350  return false; //OK
351  } //End public static method: clear
352 
353  /***************************************************************************/
356  /***************************************************************************/
360  /***************************************************************************/
361 
362  static inline bool set( Leds::Color color )
363  {
364  //----------------------------------------------------------------
365  // VARS
366  //----------------------------------------------------------------
367 
368  //----------------------------------------------------------------
369  // BODY
370  //----------------------------------------------------------------
371 
372  //Switch: Color
373  switch(color)
374  {
375  case (Leds::Color::RED):
376  {
377  gpio_bit_reset(Led_gpio::RED_GPIO, Led_pin::RED_PIN);
378  break;
379  }
380  case (Leds::Color::GREEN):
381  {
382  gpio_bit_reset(Led_gpio::GREEN_GPIO, Led_pin::GREEN_PIN);
383  break;
384  }
385  case (Leds::Color::BLUE):
386  {
387  gpio_bit_reset(Led_gpio::BLUE_GPIO, Led_pin::BLUE_PIN);
388  break;
389  }
390  default:
391  {
392  return true; //FAIL
393  }
394  } //End Switch: Color
395 
396  //----------------------------------------------------------------
397  // RETURN
398  //----------------------------------------------------------------
399 
400  return false; //OK
401  } //End public static method: set
402 
403  //--------------------------------------------------------------------------
404  // PUBLIC STATIC METHODS
405  //--------------------------------------------------------------------------
406 
407  //--------------------------------------------------------------------------
408  // PUBLIC VARS
409  //--------------------------------------------------------------------------
410 
411  //Visible to derived classes
412  protected:
413  //--------------------------------------------------------------------------
414  // PROTECTED METHODS
415  //--------------------------------------------------------------------------
416 
417  //--------------------------------------------------------------------------
418  // PROTECTED VARS
419  //--------------------------------------------------------------------------
420 
421  //Visible only inside the class
422  private:
423  //--------------------------------------------------------------------------
424  // PRIVATE METHODS
425  //--------------------------------------------------------------------------
426 
427  //--------------------------------------------------------------------------
428  // PRIVATE VARS
429  //--------------------------------------------------------------------------
430 
431 }; //End Class: Longan_nano_led
432 
433 /**********************************************************************************
434 ** NAMESPACE
435 **********************************************************************************/
436 
437 } //End Namespace: Longan_nano
438 
439 #else
440  #warning "Multiple inclusion of hader file LONGAN_NANO_LED_H_"
441 #endif
Longan_nano::Leds::Led_gpio
enum Longan_nano::Leds::_Led_gpio Led_gpio
GPIO used by the LEDs.
Longan_nano::Leds::RED_GPIO
@ RED_GPIO
Definition: longan_nano_led.hpp:113
Longan_nano
Longan_nano::Leds::~Leds
~Leds(void)
Default destructor.
Longan_nano::Leds::WHITE
@ WHITE
Definition: longan_nano_led.hpp:107
Longan_nano::Leds::GREEN
@ GREEN
Definition: longan_nano_led.hpp:105
Longan_nano::Leds::Led_pin
enum Longan_nano::Leds::_Led_pin Led_pin
pins used by the LEDs
Longan_nano::Leds
Leds.
Definition: longan_nano_led.hpp:92
Longan_nano::Leds::GREEN_GPIO
@ GREEN_GPIO
Definition: longan_nano_led.hpp:114
Longan_nano::Leds::BLUE
@ BLUE
Definition: longan_nano_led.hpp:106
Longan_nano::Leds::BLUE_PIN
@ BLUE_PIN
Definition: longan_nano_led.hpp:123
Longan_nano::Leds::RED
@ RED
Definition: longan_nano_led.hpp:104
Longan_nano::Leds::RED_PIN
@ RED_PIN
Definition: longan_nano_led.hpp:121
Longan_nano::Leds::set
static bool set(Leds::Color color)
public static method set |
Definition: longan_nano_led.hpp:362
Longan_nano::Leds::init
static bool init(void)
public static method init |
Definition: longan_nano_led.hpp:169
Longan_nano::Leds::Leds
Leds(void)
Default constructor.
Longan_nano::Leds::GREEN_PIN
@ GREEN_PIN
Definition: longan_nano_led.hpp:122
Longan_nano::Leds::_Led_gpio
_Led_gpio
GPIO used by the LEDs.
Definition: longan_nano_led.hpp:112
Longan_nano::Leds::BLUE_GPIO
@ BLUE_GPIO
Definition: longan_nano_led.hpp:115
Longan_nano::Leds::BLACK
@ BLACK
Definition: longan_nano_led.hpp:103
Longan_nano::Leds::Color
enum Longan_nano::Leds::_Color Color
Color of the LEDs.
Longan_nano::Leds::set_color
static bool set_color(Leds::Color color)
public static method set_color | Led_color
Definition: longan_nano_led.hpp:204
Longan_nano::Leds::toggle
static bool toggle(Leds::Color color)
public static method toggle |
Definition: longan_nano_led.hpp:262
Longan_nano::Leds::_Color
_Color
Color of the LEDs.
Definition: longan_nano_led.hpp:102
Longan_nano::Leds::_Led_pin
_Led_pin
pins used by the LEDs
Definition: longan_nano_led.hpp:120
Longan_nano::Leds::clear
static bool clear(Leds::Color color)
public static method clear |
Definition: longan_nano_led.hpp:312