Digital clock in C++

closed account (E0p9LyTq)
@TheSmallGuy,

http://www.cplusplus.com/forum/beginner/196272/

Admittedly that clock program has to use Windows console functions to set the output position so the display is in a static location. Using just the two headers and two functions would not allow for a static display.
Last edited on
closed account (E0p9LyTq)
No that doesn't help. I want the numbers to be big.


What do you mean by "big?"

If you mean have the output in a large font then you can't use just the two headers and functions listed for a console program. You need a graphics library at a minimum. Doing it in a GUI, Windows for example, would be easier but that would add a lot more headers and functions.
closed account (E0p9LyTq)
Here's a big sized digital clock in a console window, switchable between 12 and 24 hr format:
http://www.cplusplus.com/forum/beginner/82739/#msg444198

Requires quite a lot of headers and functions, though.
There is no way to have large font in the console. Well... not really.

Assuming that you can get the time, you could probably fake it using ASCII art:

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
#include <iostream>
#include <string>

// assuming 0 <= hours < 24, 0 <= minutes < 60, 0 <= seconds < 60
void print_time_big(int hours, int minutes, int seconds) {
    constexpr int num_lines = 5;
    constexpr int num_nums = 10;
    static const std::string nums[num_nums][num_lines] = {
        {
            "####",
            "#  #",
            "#  #",
            "#  #",
            "####"
        },
        {
            "   #",
            "   #",
            "   #",
            "   #",
            "   #"
        },
        {
            "####",
            "   #",
            "####",
            "#   ",
            "####"
        },
        // etc.
    };

    // print the string
    for (int i = 0; i < num_lines; ++i) {
        std::cout << nums[hours/10][i] << " ";
        std::cout << nums[hours%10][i] << " ";
        std::cout << " | "; // or you could make a colon, whatever
        std::cout << nums[minutes/10][i] << " ";
        std::cout << nums[minutes%10][i] << " ";
        std::cout << " | "; // same here
        std::cout << nums[seconds/10][i] << " ";
        std::cout << nums[seconds%10][i] << "\n";
    }
}

It's untested and incomplete, but you should be able to fix it up for your purposes.
Last edited on
closed account (48T7M4Gy)
I want the numbers to be big.


That's fine and you can do it a number of ways by either writing the symbols you want yourself or by surfing around the web and copying somebody else's efforts as there are some fancy 'fonts' around.

One great way to do it is to use the same stuff that Arduino ppl use for driving LED matrix displays and this is done with binary bitmasking. It's actually very simple and can be used in simple to program console programs or anywhere else in more sophisticated gui platforms. (The process is you set up each horizontal scan line based on the character bitmaps from the overall string at that time and print a character or blank space depending on the map being a 0 or 1.)

Another way is use truetype fonts and set the console font to whatever you want programmatically. All I can do is wish you luck with that because I wouldn't bother.

http://arduino-er.blogspot.com.au/2014/08/port-ascii-font-to-arduino-88-led-matrix.html
closed account (48T7M4Gy)
I want to convert the map full of 0 and 1 into readable strings. How can I do that?


I'm not clear what you mean because the mapping works the other way. For instance if you have the character '1' then the mapping on a 5x5 matrix will be
00100
00100
00100
00100
00100

'11' is:
0010000100
0010000100
0010000100
0010000100
0010000100

So to print '11' you print lines 1 to 5 and with the appropriate coule of lines you can print each 0 and one as anything you like.

BTW the number in the map samples are just the hex equivalents of the binary
closed account (48T7M4Gy)
Why is it unreliable?

Two people have now suggested essentially the same thing despite the post crossover and it is a longstanding and well established technology on all sorts of systems for probably the last 50 or whatever years. :)

Are you simply looking for free software development or you just can't properly explain your problem and waste people's time? I hope it's the latter.

I don't mind if the suggestions I or others make are not exactly or in my case even nearly what you want but this is not a mind-reading forum for prima-donnas to play around.

Are you discourteous all the time or are you just having a practice session?

And, by all means report me. In this case I would consider it a badge of honor.
Last edited on
closed account (E0p9LyTq)
We are trying to help you, but your explanation of the requirements is vague.
closed account (48T7M4Gy)
@TSG

If you were referring to gentleguy then you should have made it clear. Your out-of-character rudeness is still not needed especially as you can now probably see it is easily misunderstood.
closed account (E0p9LyTq)
I want to convert the map full of 0 and 1 into readable strings.

To a C++ programmer a map is a very specific concept, a specialized container.
http://www.cplusplus.com/reference/map/map/

Converting a map container that contains 0s and 1s into a readable string could be a trivial bit of code, but how that applies to creating a digital clock is not clear.
closed account (48T7M4Gy)
Stand by, given the colorful circumstances going on here, I'll post my arduino (it's C++ and very simple) code in a few minutes. It's more than a few lines because of the bitmap (vs <map> map )
closed account (48T7M4Gy)
Forget about a code generator unless you want to make your own fonts. A generator is often a gui driven piece of software that generates bimaps as below based on you drawing the font character you want. There are plenty of freeware (not very good but adequate) downloads around.

(B prefix means binary)

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
byte char_code[11][6] = {
    {   
   B00111110,
   B01000001,
   B01000001, 
   B00111110, 
   B00000000, 
   B00000000
   }, // 0 - if you look carefully
    {B01000010, B01111111, B01000000, B00000000, B00000000, B00000000}, // 1
    {B01100010, B01010001, B01001001, B01000110, B00000000, B00000000}, // 2
    {B00100010, B01000001, B01001001, B00110110, B00000000, B00000000}, // 3
    {B00011000, B00010100, B00010010, B01111111, B00000000, B00000000}, // 4
    {B00100111, B01000101, B01000101, B00111001, B00000000, B00000000}, // 5
    {B00111110, B01001001, B01001001, B00110000, B00000000, B00000000}, // 6
    {B01100001, B00010001, B00001001, B00000111, B00000000, B00000000}, // 7
    {B00110110, B01001001, B01001001, B00110110, B00000000, B00000000}, // 8
    {B00000110, B01001001, B01001001, B00111110, B00000000, B00000000}, // 9
    {B00010100, B00000000, B00000000, B00000000, B00000000, B00000000}, // :
    
};

void setString(char* input, int16_t column)
{
    int16_t column_value = column;
    while (*input != 0)
    {
        byte output = *input - 0x20;
        for (int y=0; y<6; y++)
        {
            boolean send = true;
            if (char_code[output][y]==B00000000)
                if (char_code[output][y-1]==B00000000)
                    send = false;
            
            if (send)
            {
                dotMatrix.setData(column_value,char_code[output][y]);
                column_value++;
            }
        } 
        if (output==0x00)
            for (int y=0; y<3; y++)
            {
                dotMatrix.setData(column_value,char_code[output][y]);
                column_value++;
            }
        input++;
    }
}

int stringSize(char* input)
{
    int returnX = 0;
    while (*input != 0)
    {
        byte output = *input - 0x20;
        for (int y=0; y<6; y++)
        {
            boolean send = true;
            if (char_code[output][y]==B00000000)
                if (char_code[output][y-1]==B00000000)
                    send = false;
            
            if (send)
                returnX++;
        } 
        if (output==0x00)
            returnX = returnX+3;
        input++;
    }
    return (returnX);
}


The principle of this code is the same as what FG is driving at except the binary maps with all sorts of fonts are done for you and the scanning of each line is done another way, bit by bit rather than as a string. The end result is the same.
Last edited on
closed account (48T7M4Gy)
The mind boggles! ROFL.
closed account (48T7M4Gy)
That's where the code I gave you comes in. You have to assemble the string of numbers in such a way that each line scans across each horizontal line of character mappings from top to bottom.

That's because you can write horizontal but not vertical lines of data on the screen. (maybe you can rotate your monitor when you want to know the time, maybe)
Did you look at the code I posted earlier? From what I can gather, it's got what you wanted.
Topic archived. No new replies allowed.