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