Clock.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: Beerware
2 /**
3  * @file Clock.c
4  * @brief Clock handler
5  * @author Michael Fitzmayer
6  * @copyright "THE BEER-WARE LICENCE" (Revision 42)
7  */
8 
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include "Clock.h"
12 #include "MCAL.h"
13 
14 #ifdef USE_BMP180
15 #include <stdlib.h>
16 #include "BMP180.h"
17 #endif
18 
19 /**
20  * @struct ClockData
21  * @brief Clock handler data
22  */
23 typedef struct
24 {
25  uint8_t u8Hours; ///< Hours (0-23)
26  uint8_t u8Minutes; ///< Minutes (0-59)
27  uint8_t u8Seconds; ///< Seconds (0-59)
28 
29  #ifdef USE_BMP180
30  int8_t s8Temperature; ///< Temperature in 1°C
31  #endif
32 
33  uint8_t au8Buffer[64]; ///< Buffer for current clock-face
34 
35 } ClockData;
36 
37 /**
38  * @var _stClock
39  * @brief Clock handler private data
40  */
41 static ClockData _stClock = { 0 };
42 
43 /**
44  * @var _au8ClockFont
45  * @brief 6x6 Clock font
46  */
47 static const uint8_t _au8ClockFont[50] = {
48  0b01111100, 0b00001000, 0b01111000, 0b01111100, 0b01000000, 0b01111100, 0b01111100, 0b01111100, 0b01111100, 0b01111100,
49  0b01001100, 0b00011000, 0b00000100, 0b00000100, 0b01000000, 0b01000000, 0b01000000, 0b00000100, 0b01000100, 0b01000100,
50  0b01010100, 0b00001000, 0b00111000, 0b01111000, 0b01010000, 0b01111000, 0b01111100, 0b00001000, 0b01111100, 0b01111100,
51  0b01100100, 0b00001000, 0b01000000, 0b00000100, 0b01111100, 0b00000100, 0b01000100, 0b00010000, 0b01000100, 0b00000100,
52  0b01111100, 0b00011100, 0b01111100, 0b01111100, 0b00010000, 0b01111000, 0b01111100, 0b00010000, 0b01111100, 0b01111100,
53 };
54 
55 /**
56  * @brief Get address of image buffer
57  * @return Pointer to image buffer
58  */
59 uint8_t* Clock_GetBufferAddr(void)
60 {
61  return (unsigned char*)&_stClock.au8Buffer;
62 }
63 
64 /**
65  * @brief Update clock-face buffer
66  */
67 void Clock_Update(void)
68 {
69  int8_t s8Idx;
70  uint8_t u8Temp;
71  uint8_t u8Offset;
72  uint8_t u8Digit[6] = { 0 };
73 
74  // Fetch current time from RTC
76 
77  // Extract digits
78  #ifdef USE_BMP180
79  u8Temp = abs(_stClock.s8Temperature);
80  for (s8Idx = 5; s8Idx >= 4; s8Idx--)
81  {
82  u8Digit[s8Idx] = u8Temp % 10;
83  u8Temp /= 10;
84  }
85  #endif
86 
87  u8Temp = _stClock.u8Minutes;
88  for (s8Idx = 3; s8Idx >= 2; s8Idx--)
89  {
90  u8Digit[s8Idx] = u8Temp % 10;
91  u8Temp /= 10;
92  }
93 
94  u8Temp = _stClock.u8Hours;
95  for (s8Idx = 1; s8Idx >= 0; s8Idx--)
96  {
97  u8Digit[s8Idx] = u8Temp % 10;
98  u8Temp /= 10;
99  }
100 
101  // Write digits to clock-face buffer
102  for (uint8_t u8Dig = 0; u8Dig < 4; u8Dig++)
103  {
104  uint8_t u8IdxStart = 20;
105  uint8_t u8IdxEnd = 38;
106  #ifdef USE_BMP180
107  u8IdxStart = 8;
108  u8IdxEnd = 26;
109  #endif
110 
111  u8Offset = u8Digit[u8Dig];
112 
113  for (uint8_t u8Idx = u8IdxStart + u8Dig; u8Idx <= u8IdxEnd + u8Dig; u8Idx += 4)
114  {
115  _stClock.au8Buffer[u8Idx] = _au8ClockFont[u8Offset];
116  u8Offset += 10;
117  }
118  }
119 
120  #ifdef USE_BMP180
121  for (uint8_t u8Dig = 4; u8Dig < 6; u8Dig++)
122  {
123  u8Offset = u8Digit[u8Dig];
124 
125  for (uint8_t u8Idx = 33 + u8Dig; u8Idx <= 51 + u8Dig; u8Idx += 4)
126  {
127  _stClock.au8Buffer[u8Idx] = _au8ClockFont[u8Offset];
128  u8Offset += 10;
129  }
130  }
131  #endif
132 
133  // Add clock-face divider
134  #ifndef USE_BMP180
135  _stClock.au8Buffer[25] |= 1 << 0;
136  _stClock.au8Buffer[33] |= 1 << 0;
137  #else
138  _stClock.au8Buffer[13] |= 1 << 0;
139  _stClock.au8Buffer[21] |= 1 << 0;
140 
141  // Add degree symbol
142  _stClock.au8Buffer[34] |= 1 << 0;
143  _stClock.au8Buffer[35] |= 1 << 7;
144  _stClock.au8Buffer[38] |= 1 << 0;
145  _stClock.au8Buffer[39] |= 1 << 7;
146 
147  // Add minus sign if temperature is below 0°C
148  if (0 > _stClock.s8Temperature)
149  {
150  _stClock.au8Buffer[44] |= 1 << 0;
151  _stClock.au8Buffer[44] |= 1 << 1;
152  }
153  #endif
154 }
155 
156 #ifdef USE_BMP180
157 /**
158  * @brief Set temperature
159  * @param s8Temp
160  * Temperature in 1°C
161  */
162 void Clock_SetTemperature(int8_t s8Temp)
163 {
164  _stClock.s8Temperature = s8Temp;
165 }
166 #endif
uint8_t * Clock_GetBufferAddr(void)
Get address of image buffer.
Definition: Clock.c:59
Digital pressure sensor driver.
uint8_t au8Buffer[64]
Buffer for current clock-face.
Definition: Clock.c:33
static ClockData _stClock
Clock handler private data.
Definition: Clock.c:41
int8_t s8Temperature
Temperature in 1°C.
Definition: Clock.c:30
int RTC_GetTime(uint8_t *pu8Hours, uint8_t *pu8Minutes, uint8_t *pu8Seconds)
Get current time from RTC.
Definition: MCAL.c:225
Clock handler.
Clock handler data.
Definition: Clock.c:23
uint8_t u8Seconds
Seconds (0-59)
Definition: Clock.c:27
void Clock_SetTemperature(int8_t s8Temp)
Set temperature.
Definition: Clock.c:162
Microcontroller Abstraction Layer.
void Clock_Update(void)
Update clock-face buffer.
Definition: Clock.c:67
static const uint8_t _au8ClockFont[50]
6x6 Clock font
Definition: Clock.c:47
uint8_t u8Hours
Hours (0-23)
Definition: Clock.c:25
uint8_t u8Minutes
Minutes (0-59)
Definition: Clock.c:26