SWE-350 TOTP Generator Milestone 5
The DE-10 board has six 7-segment displays, this can be used to display and generate a time based one-time pin (TOTP).
Loading...
Searching...
No Matches
7seg.c
Go to the documentation of this file.
1//
2// Created by Erick Grant on 10/27/24.
3//
4
5#include "7seg.h"
6
7#include <stdarg.h>
8#include <stdio.h>
9#include <string.h>
10#include <stdlib.h>
11#include <tgmath.h>
12#include "../address_map_arm.h"
13// Define register locations for the DE-10 Standard Board
14
15 #define HW_REGS_BASE ( ALT_STM_OFST )
16 #define HW_REGS_SPAN ( 0x04000000 )
17 #define HW_REGS_MASK ( HW_REGS_SPAN - 1 )
18int fd = -1; // used to open /dev/mem for access to physical addresses
19// General setup, including LCD
30void *LW_virtual; // used to map physical addresses for the light-weight bridge
31
32int setup() {
33
34
35
36
37
43long long decimalValue;
44
45
46
47
48// Partial ChatGPT used Nov 30 2024
65static const char *digitChar(char A) {
66
67 // 0: 00111111
68 // 1: 00000110
69 // 2: 01011011
70 // 3: 01001111
71 // 4: 01100110
72 // 5: 01101101
73 // 6: 01111101
74 // 7: 00000111
75 // 8: 01111111
76 // 9: 01101111
77
78 switch (A) {
79 case '0':
80 return "00111111";
81 case '1':
82 return "00000110";
83 case '2':
84 return "01011011";
85 case '3':
86 return "01001111";
87 case '4':
88 return "01100110";
89 case '5':
90 return "01101101";
91 case '6':
92 return "01111101";
93 case '7':
94 return "00000111";
95 case '8':
96 return "01111111";
97 case '9':
98 return "01101111";
99}
100 return "X";
101}
102
103
104// Documentation provided by ChatGPT November 30, 2024
118static char *dectobcd7bin(char * buf, int ABCD) {
119
125 char inputChar[7];
126
132 char outputChar[50] = "";
133
134 // Check if the input number exceeds 6 digits
135 if (ABCD > 999999) {
136 printf("ABCD > 999999\n ERROR");
137
138 return "FAILURE";
139
140 }
141
142 sprintf(inputChar, "%06d", ABCD); // Convert the integer to a zero-padded 6-character string
143 // printf("The char input is %s\n", inputChar);
144
145 // Loop through each character in the input string
146 for (int i = 0; i < 6; i++) {
147 strcat(outputChar, digitChar(inputChar[i])); // Append the 7-segment binary representation of each digit to outputChar
148 }
149 // Copy the final concatenated string into the provided buffer
150 strncpy(buf, outputChar, 50);
151 return buf;
152
153}
154
168long long dectodec7(int ABCD) {
174 char outputChar[50] = "";
175 //char array[50]; // Define an array to house the display
176
177 memset(outputChar, 0, 50); /* All bytes are set to '\0' */
178 if ((ABCD > 999999) || (ABCD < 0)) {
179
180 printf("FAILURE, input ABCD n dectodec7 is greater than 999999");
181 }
182 const char* binaryOutput = dectobcd7bin(outputChar, ABCD);
183
184
185 // const char* binaryOutput = dectobcd7bin(564522);
186 // printf("The output %s\n",binaryOutput);
187 decimalValue = strtoll(binaryOutput, NULL, 2);
188
189
190 // printf("The decimal value for %d is in bigInt: %lld\n", ABCD, decimalValue);
191
192 return decimalValue;
193}
194
208long long leftDec2Dec7(int ABCD) {
214 char arrayLeft[17]; // Space for two numbers in binary
220 char arrayLeftTotal[50];
221 memset(arrayLeftTotal, 0, 50); /* All bytes are set to '\0' */
222 const char* binaryOutput2 = dectobcd7bin(arrayLeftTotal, ABCD); // Fill up the binaryOutput2 with the left binary
223
224
225 strncpy (arrayLeft, binaryOutput2, 16); // Copy the binaryOutput2 into arrayLeft
226 //printf("%s\n", arrayLeft);
227
228 decimalValue = strtoll(arrayLeft, NULL, 2); // Convert the binary into a decimal
229 //printf("The decimal value for LEFT %d is in bigInt: %lld\n", ABCD, decimalValueLeft);
230
231 return decimalValue;
232
233
234}
235
236
237// Testing
238#include <assert.h> // Testing
239
240
241// Most of the test cases were generated with some use of AI, using ChatGPT.
247void test7segDigitChar() {
248 // digitChars
249 // Valid input tests
250 assert(digitChar('0') == "00111111");
251 assert(digitChar('1') == "00000110");
252 assert(digitChar('2') == "01011011");
253 assert(digitChar('3') == "01001111");
254 assert(digitChar('4') == "01100110");
255 assert(digitChar('5') == "01101101");
256 assert(digitChar('6') == "01111101");
257 assert(digitChar('7') == "00000111");
258 assert(digitChar('8') == "01111111");
259 assert(digitChar('9') == "01101111");
260
261 // Invalid input tests
262 assert(digitChar('a') == "X");
263 assert(digitChar('#') == "X");
264 assert(digitChar(' ') == "X");
265 assert(digitChar('\n') == "X");
266 assert(digitChar('-') == "X");
267
268 // Testing input boundaries
269 assert(digitChar('9') == "01101111");
270 assert(digitChar('9' + 1) == "X");
271
272 // printf("All test cases for digitChar within 7-segment passed!\n");
273
274}
275
281void testDectobcd7bin() {
282 char buffer[50];
283
284 // using test cases from chatgpt Nov 15, 2024, used the associated method as input.
285 // Test Case 1: Maximum valid input
286 assert(strcmp(dectobcd7bin(buffer, 999999), "011011110110111101101111011011110110111101101111") == 0);
287
288 // Test Case 2: Boundary condition (greater than 1,000,000)
289 assert(strcmp(dectobcd7bin(buffer, 1000000), "FAILURE") == 0);
290
291 // Test Case 3: Smallest valid input
292
293 assert(strcmp(dectobcd7bin(buffer, 0), "001111110011111100111111001111110011111100111111") == 0);
294
295 // Test Case 4: Typical input in range
296 assert(strcmp(dectobcd7bin(buffer, 123456), "000001100101101101001111011001100110110101111101") == 0);
297
298 // Test Case 5: Input with repeating digits
299 assert(strcmp(dectobcd7bin(buffer, 888888), "011111110111111101111111011111110111111101111111") == 0);
300
301 // Test Case 6: Input greater than 1,000,000
302 assert(strcmp(dectobcd7bin(buffer, 1000001), "FAILURE") == 0);
303
304 // Test Case 7: Single-digit number padded with zeros
305 assert(strcmp(dectobcd7bin(buffer, 5), "001111110011111100111111001111110011111101101101") == 0);
306
307 // printf("All test cases passed for the Decimal to BCD 7-segment\n");
308
309 }
310
311 // The numbers were generated without AI, and were created from a binary to decimal website
317 void testDectoDec7() {
318 // Test Case 1: Convert 0 to its binary representation and then to decimal
319 // assert(dectodec7(0) == 6860473343); 17802464409370431
320 assert(dectodec7(0) == 69540876599103);
321 // Binary: "001111110011111100111111001111110011111100111111"
322
323 // Test Case 2: Convert 123456 to its binary representation and then to decimal
324 assert(dectodec7(123456) == 6989243903357);
325 // Binary: "000001100101101101001111011001100110110101111101"
326
327 // Test Case 3: Convert the maximum 6-digit value (999999)
328 assert(dectodec7(999999) == 122524401626991);
329 // Binary: "011011110110111101101111011011110110111101101111"
330
331 // Test Case 4: Convert a number with repeating digits (111111)
332 assert(dectodec7(111111) == 6622940628486);
333 // Binary: "000001100000011000000110000001100000011000000110"
334
335 // Test Case 5: Convert a single-digit number padded with zeros (5)
336 assert(dectodec7(5) == 69540876599149);
337 // Binary: "00111111001111110011111100111111001101101011101"
338
339 // Test Case 6: Boundary value test (smallest 6-digit number)
340 assert(dectodec7(100000) == 6868713815871);
341
342 // Test Case 7: Random number for typical input testing
343 assert(dectodec7(359977) == 87331439576839);
344 // printf("All test cases passed successfully within testDectoDec7!\n");
345
346 }
347
353 void testLeftDec2Dec7() {
354
355 // Test Case 1: Convert 0 to binary and extract the first 16 bits
356 assert(leftDec2Dec7(0) == 16191); // "0011111100111111"
357
358 // Test Case 2: Convert 123456 and extract the first 16 bits
359 assert(leftDec2Dec7(123456) == 1627); // "1111011010011101"
360
361 // Test Case 3: Convert 999999 and extract the first 16 bits
362 assert(leftDec2Dec7(999999) == 28527); // "0110111101101111"
363
364 // Test Case 4: Convert a single-digit number padded with zeros
365 assert(leftDec2Dec7(5) == 16191); // "0011111100111111"
366
367 // Test Case 5: Convert 100010 and extract the first 16 bits
368 assert(leftDec2Dec7(100010) == 1599); // "0101100110010010"
369
370 // Test Case 6: Edge case - 111111 (first 16-bits of the sequence)
371 assert(leftDec2Dec7(111111) == 1542); // Custom validation
372
373 // printf("All test cases passed successfully within testLeftDec2Dec7!\n");
374 }
375
381 int test7Segment2()
382 {
383 test7segDigitChar();
384 testDectobcd7bin();
385 testDectoDec7();
386 testLeftDec2Dec7();
387 printf("All test cases passed successfully within 7-segment\n");
388 return 1;
389 }
390
391
392
393
void * LW_virtual
Used to map physical addresses for the light-weight bridge Not made by Erick Grant,...
Definition 7seg.c:30
void * virtual_base
Base for the LCD Used to define the address of the LCD.
Definition 7seg.c:25
int fd
Used to open /dev/mem for access to physical addresses Not made by Erick Grant, copyright Terasic Inc...
Definition 7seg.c:18
long long leftDec2Dec7(int ABCD)
long long dectodec7(int ABCD)
int test7Segment2()
union _buffer buffer