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.

183 lines
5.0KB

  1. // Copyright 2013 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 "yarns/drivers/display.h"
  29. #include <stm32f10x_conf.h>
  30. #include <string.h>
  31. #include "yarns/resources.h"
  32. namespace yarns {
  33. const uint16_t kPinClk = GPIO_Pin_7;
  34. const uint16_t kPinEnable = GPIO_Pin_8;
  35. const uint16_t kPinData = GPIO_Pin_9;
  36. const uint16_t kScrollingDelay = 180;
  37. const uint16_t kScrollingPreDelay = 1500;
  38. const uint16_t kBlinkMask = 128;
  39. const uint16_t kCharacterEnablePins[] = {
  40. GPIO_Pin_6,
  41. GPIO_Pin_5
  42. };
  43. void Display::Init() {
  44. GPIO_InitTypeDef gpio_init;
  45. gpio_init.GPIO_Pin = kPinClk;
  46. gpio_init.GPIO_Pin |= kPinEnable;
  47. gpio_init.GPIO_Pin |= kPinData;
  48. gpio_init.GPIO_Pin |= kCharacterEnablePins[0];
  49. gpio_init.GPIO_Pin |= kCharacterEnablePins[1];
  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. memset(short_buffer_, ' ', kDisplayWidth);
  57. memset(long_buffer_, ' ', kScrollBufferSize);
  58. fading_counter_ = 0;
  59. fading_increment_ = 0;
  60. blinking_ = false;
  61. brightness_ = 6;
  62. }
  63. void Display::Scroll() {
  64. if (long_buffer_size_ > kDisplayWidth) {
  65. scrolling_ = true;
  66. scrolling_step_ = 0;
  67. scrolling_timer_ = kScrollingDelay;
  68. scrolling_pre_delay_timer_ = kScrollingPreDelay;
  69. }
  70. }
  71. void Display::RefreshSlow() {
  72. #ifdef APPLICATION
  73. if (scrolling_) {
  74. if (scrolling_pre_delay_timer_) {
  75. --scrolling_pre_delay_timer_;
  76. } else {
  77. --scrolling_timer_;
  78. if (scrolling_timer_ == 0) {
  79. ++scrolling_step_;
  80. if (scrolling_step_ > long_buffer_size_ - kDisplayWidth + 1) {
  81. scrolling_ = false;
  82. }
  83. scrolling_timer_ = kScrollingDelay;
  84. }
  85. }
  86. }
  87. displayed_buffer_ = (scrolling_ && !scrolling_pre_delay_timer_)
  88. ? long_buffer_ + scrolling_step_
  89. : short_buffer_;
  90. if (fading_increment_) {
  91. fading_counter_ += fading_increment_;
  92. }
  93. uint8_t brightness = brightness_;
  94. if (fading_increment_) {
  95. brightness = fading_counter_ < 0x8000
  96. ? fading_counter_ >> 12
  97. : 15 - (fading_counter_ >> 12);
  98. }
  99. actual_brightness_ = brightness >> 1;
  100. blink_counter_ = (blink_counter_ + 1) % (kBlinkMask * 2);
  101. #else
  102. displayed_buffer_ = short_buffer_;
  103. actual_brightness_ = 3;
  104. #endif // APPLICATION
  105. }
  106. void Display::RefreshFast() {
  107. if (brightness_pwm_cycle_ <= actual_brightness_
  108. && (!blinking_ || blink_counter_ < kBlinkMask)) {
  109. GPIOB->BRR = kCharacterEnablePins[active_position_];
  110. active_position_ = (active_position_ + 1) % kDisplayWidth;
  111. Shift14SegmentsWord(chr_characters[
  112. static_cast<uint8_t>(displayed_buffer_[active_position_])]);
  113. GPIOB->BSRR = kCharacterEnablePins[active_position_];
  114. } else {
  115. GPIOB->BRR = kCharacterEnablePins[active_position_];
  116. }
  117. brightness_pwm_cycle_ = (brightness_pwm_cycle_ + 1) % kBrightnessLevels;
  118. }
  119. void Display::Print(const char* short_buffer, const char* long_buffer) {
  120. strncpy(short_buffer_, short_buffer, kDisplayWidth);
  121. #ifdef APPLICATION
  122. strncpy(long_buffer_, long_buffer, kScrollBufferSize);
  123. long_buffer_size_ = strlen(long_buffer_);
  124. #endif // APPLICATION
  125. scrolling_ = false;
  126. }
  127. # define SHIFT_BIT \
  128. GPIOB->BRR = kPinClk; \
  129. if (data & 1) { \
  130. GPIOB->BSRR = kPinData; \
  131. } else { \
  132. GPIOB->BRR = kPinData; \
  133. } \
  134. data >>= 1; \
  135. GPIOB->BSRR = kPinClk;
  136. void Display::Shift14SegmentsWord(uint16_t data) {
  137. GPIOB->BRR = kPinEnable;
  138. for (int i = 0; i < 2; ++i) {
  139. #ifdef APPLICATION
  140. SHIFT_BIT
  141. SHIFT_BIT
  142. SHIFT_BIT
  143. SHIFT_BIT
  144. SHIFT_BIT
  145. SHIFT_BIT
  146. SHIFT_BIT
  147. SHIFT_BIT
  148. #else
  149. for (int j = 0; j < 8; ++j) {
  150. SHIFT_BIT
  151. }
  152. #endif // APPLICATION
  153. }
  154. GPIOB->BSRR = kPinEnable;
  155. }
  156. } // namespace yarns