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.

101 lines
3.1KB

  1. // Copyright 2014 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 the 4 bicolor LEDs controlled by a 595.
  28. #include "clouds/drivers/leds.h"
  29. #include <algorithm>
  30. namespace clouds {
  31. using namespace std;
  32. const uint16_t kPinClk = GPIO_Pin_5;
  33. const uint16_t kPinEnable = GPIO_Pin_4;
  34. const uint16_t kPinData = GPIO_Pin_5;
  35. const uint16_t kPinFreezeLed = GPIO_Pin_9;
  36. void Leds::Init() {
  37. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  38. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  39. GPIO_InitTypeDef gpio_init;
  40. gpio_init.GPIO_Pin = kPinData | kPinFreezeLed;
  41. gpio_init.GPIO_Mode = GPIO_Mode_OUT;
  42. gpio_init.GPIO_OType = GPIO_OType_PP;
  43. gpio_init.GPIO_Speed = GPIO_Speed_25MHz;
  44. gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
  45. GPIO_Init(GPIOB, &gpio_init);
  46. gpio_init.GPIO_Pin = kPinClk | kPinEnable;
  47. gpio_init.GPIO_Mode = GPIO_Mode_OUT;
  48. gpio_init.GPIO_OType = GPIO_OType_PP;
  49. gpio_init.GPIO_Speed = GPIO_Speed_25MHz;
  50. gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
  51. GPIO_Init(GPIOC, &gpio_init);
  52. Clear();
  53. pwm_counter_ = 0;
  54. }
  55. void Leds::Write() {
  56. // Pack data.
  57. pwm_counter_ += 32;
  58. uint8_t data = 0;
  59. for (uint16_t i = 0; i < 4; ++i) {
  60. data <<= 2;
  61. if (red_[i] && red_[i] >= pwm_counter_) {
  62. data |= 0x1;
  63. }
  64. if (green_[i] && green_[i] >= pwm_counter_) {
  65. data |= 0x2;
  66. }
  67. }
  68. // Shift out.
  69. GPIO_WriteBit(GPIOC, kPinEnable, Bit_RESET);
  70. for (uint16_t i = 0; i < 8; ++i) {
  71. GPIO_WriteBit(GPIOC, kPinClk, Bit_RESET);
  72. if (data & 0x80) {
  73. GPIO_WriteBit(GPIOB, kPinData, Bit_SET);
  74. } else {
  75. GPIO_WriteBit(GPIOB, kPinData, Bit_RESET);
  76. }
  77. data <<= 1;
  78. GPIO_WriteBit(GPIOC, kPinClk, Bit_SET);
  79. }
  80. GPIO_WriteBit(GPIOC, kPinEnable, Bit_SET);
  81. GPIO_WriteBit(GPIOB, kPinFreezeLed, static_cast<BitAction>(freeze_led_));
  82. }
  83. void Leds::Clear() {
  84. fill(&red_[0], &red_[4], 0);
  85. fill(&green_[0], &green_[4], 0);
  86. freeze_led_ = false;
  87. }
  88. } // namespace clouds