-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
320 lines (265 loc) · 9.43 KB
/
Copy pathmain.cpp
File metadata and controls
320 lines (265 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**********************************************************************
*
* ECE 4180 Final Project - Nixie Tube Clock with DS3231 and Bluetooth Control
* Edited from the original demo by Benjamin Ventimiglia
*
***********************************************************************
* Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
**********************************************************************/
#include "mbed.h"
#include "stdio.h"
#include "ds3231.h"
#define ESC 0x1B
#define BLUETOOTH
Ds3231 rtc(p9, p10); //rtc object
Serial bluemod(p28,p27); // Bluetooth device
BusOut Anodes(p11, p12, p13, p14);
BusOut Digit(p24, p23, p22, p21);
// RTC variables
//default, use bit masks in ds3231.h for desired operation
ds3231_cntl_stat_t rtc_control_status = {0,0};
ds3231_time_t rtc_time;
time_t epoch_time;
Ticker timer;
bool debug = 0;
// User input functions
void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member);
void get_user_input(char* message, uint8_t min, uint8_t max, bool* member);
void get_bt_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member);
void get_bt_user_input(char* message, uint8_t min, uint8_t max, bool* member);
// internal functions
int fix_digit(int num);
void show_time(int hour, int min);
void update_epoch() { epoch_time = rtc.get_epoch(); }
int main(void)
{
rtc.set_cntl_stat_reg(rtc_control_status);
#ifndef BLUETOOTH
// Ask for debug mode from user
get_user_input("\nTurn on debug? 0 for no, 1 for yes: ", 0, 1, &debug);
//Get hour from user
get_user_input("\nPlease enter the hour (0-23): ", 0, 23, &rtc_time.hours);
//Get minutes from user
get_user_input("\nPlease enter the minute (0-59): ", 0, 59, &rtc_time.minutes);
//Get seconds from user
get_user_input("\nPlease enter the second (0-59): ", 0, 59, &rtc_time.seconds);
// If BLUETOOTH is defined, use bt inputs
#else
// Ask for debug mode from user
get_bt_user_input("\nTurn on debug? 0 for no, 1 for yes: ", 0, 1, &debug);
//Get hour from user
get_bt_user_input("\nPlease enter the hour (0-23): ", 0, 23, &rtc_time.hours);
//Get minutes from user
get_bt_user_input("\nPlease enter the minute (0-59): ", 0, 59, &rtc_time.minutes);
//Get seconds from user
get_bt_user_input("\nPlease enter the second (0-59): ", 0, 59, &rtc_time.seconds);
#endif
//Set the time, uses inverted logic for return value
if(rtc.set_time(rtc_time))
{
printf("\nrtc.set_time failed!!\n");
bluemod.printf("\nrtc.set_time failed!!\n");
exit(0);
}
// Get time from RTC every second (to reduce load on mbed)
timer.attach(&update_epoch, 1);
tm calendar_time;
for(;;)
{
// convert epoch_time into hours and minutes
calendar_time = *localtime(&epoch_time);
int hours = int(calendar_time.tm_hour);
int minutes = int(calendar_time.tm_min);
// if debug mode, print out full time to pc and bluetooth every second
if (debug) {
printf("\n%s", ctime(&epoch_time));
bluemod.printf("\n%s", ctime(&epoch_time));
wait(1.0);
}
// Send time to nixie tubes in 24hr format
show_time(hours, minutes);
}//loop
}
/**********************************************************************
* Function: get_user_input() / get_bt_user_input()
* Parameters: message - user prompt
* min - minimum value of input
* max - maximum value of input
* member - pointer to struct member
* Returns: none
*
* Description: get time/date input from user from either
* serial terminal or bluetooth connection
*
**********************************************************************/
void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member)
{
uint32_t temp;
do
{
printf("\n%s", message);
//for some reason mbed doesn't like a pointer to a member in scanf
//term.scanf("%d", member); works with gcc on RPi
scanf("%d", &temp);
*member = temp;
if((*(member)< min) || (*(member) > max))
{
printf("\nERROR-RTI");
}
}
while((*(member) < min) || (*(member) > max));
}
void get_user_input(char* message, uint8_t min, uint8_t max, bool* member)
{
uint32_t temp;
do
{
printf("\n%s", message);
//for some reason mbed doesn't like a pointer to a member in scanf
//term.scanf("%d", member); works with gcc on RPi
scanf("%d", &temp);
*member = temp;
if((*(member)< min) || (*(member) > max))
{
printf("\nERROR-RTI");
}
}
while((*(member) < min) || (*(member) > max));
}
void get_bt_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member)
{
uint32_t temp;
do
{
bluemod.printf("\n%s", message);
//for some reason mbed doesn't like a pointer to a member in scanf
//term.scanf("%d", member); works with gcc on RPi
bluemod.scanf("%d", &temp);
*member = temp;
if((*(member)< min) || (*(member) > max))
{
bluemod.printf("\nERROR-RTI");
}
}
while((*(member) < min) || (*(member) > max));
}
void get_bt_user_input(char* message, uint8_t min, uint8_t max, bool* member)
{
uint32_t temp;
do
{
bluemod.printf("\n%s", message);
//for some reason mbed doesn't like a pointer to a member in scanf
//term.scanf("%d", member); works with gcc on RPi
bluemod.scanf("%d", &temp);
*member = temp;
if((*(member)< min) || (*(member) > max))
{
bluemod.printf("\nERROR-RTI");
}
}
while((*(member) < min) || (*(member) > max));
}
/**********************************************************************
* Function: fix_digit()
* Parameters: num - number to show on nixie tube
* Returns: value to send to multiplexer
*
* Description: the wiring for the multiplexer is a bit off, so this
* function makes the proper adjustment to set the multiplexer
* bus
**********************************************************************/
int fix_digit(int num)
{
switch(num){
case 0:
return 1;
case 1:
return 0;
case 2:
return 9;
case 3:
return 8;
case 4:
return 7;
case 5:
return 6;
case 6:
return 5;
case 7:
return 4;
case 8:
return 3;
case 9:
return 2;
}
return num;
}
/**********************************************************************
* Function: show_time()
* Parameters: hour - hour
* min - minute
* Returns: none
*
* Description: Displays the hour and minutes on the nixie tubes
**********************************************************************/
void show_time(int hour, int min)
{
if(hour > 9)
{
Digit = fix_digit(1);
hour = hour - 10;
}
else
{
Digit = fix_digit(0);
}
wait_ms(2);
Anodes = 1;
wait_ms(1);
Anodes = 0;
Digit = fix_digit(hour);
wait_ms(2);
Anodes = 2;
wait_ms(1);
Anodes = 0;
Digit = fix_digit(min / 10);
wait_ms(2);
Anodes = 4;
wait_ms(1);
Anodes = 0;
Digit = fix_digit(min % 10);
wait_ms(2);
Anodes = 8;
wait_ms(1);
Anodes = 0;
return;
}