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.

102 lines
3.1KB

  1. // Copyright 2012 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 4x14-segments display.
  28. #include "braids/drivers/display.h"
  29. #include <string.h>
  30. #include "braids/resources.h"
  31. namespace braids {
  32. const uint16_t kPinClk = GPIO_Pin_11;
  33. const uint16_t kPinEnable = GPIO_Pin_10;
  34. const uint16_t kPinData = GPIO_Pin_1;
  35. const uint16_t kCharacterEnablePins[] = {
  36. GPIO_Pin_5,
  37. GPIO_Pin_6,
  38. GPIO_Pin_7,
  39. GPIO_Pin_8
  40. };
  41. void Display::Init() {
  42. GPIO_InitTypeDef gpio_init;
  43. gpio_init.GPIO_Pin = kPinClk;
  44. gpio_init.GPIO_Pin |= kPinEnable;
  45. gpio_init.GPIO_Pin |= kPinData;
  46. gpio_init.GPIO_Pin |= kCharacterEnablePins[0];
  47. gpio_init.GPIO_Pin |= kCharacterEnablePins[1];
  48. gpio_init.GPIO_Pin |= kCharacterEnablePins[2];
  49. gpio_init.GPIO_Pin |= kCharacterEnablePins[3];
  50. gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
  51. gpio_init.GPIO_Mode = GPIO_Mode_Out_PP;
  52. GPIO_Init(GPIOB, &gpio_init);
  53. GPIOB->BSRR = kPinEnable;
  54. active_position_ = 0;
  55. brightness_pwm_cycle_ = 0;
  56. brightness_ = 3;
  57. memset(buffer_, ' ', kDisplayWidth);
  58. }
  59. void Display::Refresh() {
  60. if (brightness_pwm_cycle_ <= brightness_) {
  61. GPIOB->BRR = kCharacterEnablePins[active_position_];
  62. active_position_ = (active_position_ + 1) % kDisplayWidth;
  63. Shift14SegmentsWord(chr_characters[
  64. static_cast<uint8_t>(buffer_[active_position_])]);
  65. GPIOB->BSRR = kCharacterEnablePins[active_position_];
  66. } else {
  67. GPIOB->BRR = kCharacterEnablePins[active_position_];
  68. }
  69. brightness_pwm_cycle_ = (brightness_pwm_cycle_ + 1) % kBrightnessLevels;
  70. }
  71. void Display::Print(const char* s) {
  72. strncpy(buffer_, s, kDisplayWidth);
  73. }
  74. void Display::Shift14SegmentsWord(uint16_t data) {
  75. GPIOB->BRR = kPinEnable;
  76. for (uint16_t i = 0; i < 16; ++i) {
  77. GPIOB->BRR = kPinClk;
  78. if (data & 1) {
  79. GPIOB->BSRR = kPinData;
  80. } else {
  81. GPIOB->BRR = kPinData;
  82. }
  83. data >>= 1;
  84. GPIOB->BSRR = kPinClk;
  85. }
  86. GPIOB->BSRR = kPinEnable;
  87. }
  88. } // namespace braids