You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.9KB

  1. // Copyright 2015 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Driver for all the LEDs.
  28. #include "marbles/drivers/leds.h"
  29. #include <algorithm>
  30. #include <stm32f4xx_conf.h>
  31. namespace marbles {
  32. using namespace std;
  33. struct LedDefinition {
  34. GPIO_TypeDef* gpio;
  35. uint16_t pin[3]; // pins for R, G, B - assumed to be on the same GPIO
  36. };
  37. const LedDefinition led_definition[] = {
  38. { GPIOB, { 0, GPIO_Pin_10, 0 } }, // LED_T_DEJA_VU
  39. { GPIOB, { GPIO_Pin_13, GPIO_Pin_12, 0 } }, // LED_T_MODEL
  40. { GPIOC, { GPIO_Pin_7, GPIO_Pin_6, 0 } }, // LED_T_RANGE
  41. { GPIOB, { 0, GPIO_Pin_9, 0 } }, // LED_X_DEJA_VU
  42. { GPIOB, { GPIO_Pin_8, GPIO_Pin_7, 0 } }, // LED_X_CONTROL_MODE,
  43. { GPIOB, { GPIO_Pin_4, GPIO_Pin_5, 0 } }, // LED_X_RANGE
  44. { GPIOB, { 0, GPIO_Pin_3, 0 } } // LED_X_EXT
  45. };
  46. void Leds::Init() {
  47. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  48. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  49. GPIO_InitTypeDef gpio_init;
  50. gpio_init.GPIO_Mode = GPIO_Mode_OUT;
  51. gpio_init.GPIO_OType = GPIO_OType_PP;
  52. gpio_init.GPIO_Speed = GPIO_Speed_2MHz;
  53. gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
  54. for (int i = 0; i < LED_LAST; ++i) {
  55. LedDefinition d = led_definition[i];
  56. gpio_init.GPIO_Pin = d.pin[0] | d.pin[1] | d.pin[2];
  57. GPIO_Init(d.gpio, &gpio_init);
  58. }
  59. Clear();
  60. }
  61. void Leds::Clear() {
  62. fill(&colors_[0], &colors_[LED_LAST], LED_COLOR_OFF);
  63. }
  64. void Leds::Write() {
  65. for (int i = 0; i < LED_LAST; ++i) {
  66. LedDefinition d = led_definition[i];
  67. GPIO_WriteBit(d.gpio, d.pin[0], static_cast<BitAction>(
  68. (colors_[i] & 0x800000) >> 23));
  69. GPIO_WriteBit(d.gpio, d.pin[1], static_cast<BitAction>(
  70. (colors_[i] & 0x008000) >> 15));
  71. }
  72. }
  73. } // namespace marbles