I am using a TM1637 display for my Arduino Uno. The goal was to just light up one point of the colon in the middle - what seems to be kind of impossible to accomplish. During research I stumbled over the following snippet, which I dont fully understand:
uint8_t segto;
int value = 1244;
segto = 0x80 | display.encodeDigit((value/100)%10);
display.setSegments(&segto, 1, 1);
Ok line 1 seems to declare s.th. like a char, an unsigned 8bit integer. Line 2 should be clear. Line 3: The pipe symbol seems to do a kind of addition - never seen this before. The rest here is also not clear to me - why these strange calculation to pass a value (here 2) to .encodeDigit() ? Finally line 4: Why is the adress of 'segto' passed to the function instead of just the value?
This all ends up showing a "2:" on the display. I got the snippet from this 'manual': https://robojax.com/learn/arduino/robojax-TM1637_display_manual.pdf
Maybe s.o. can help to light things up a bit
1 Answer 1
Let's take the hardest line first. We'll start from the "inside" outwards.
segto = 0x80 | display.encodeDigit((value/100)%10);
The innermost bit is:
value/100
That gives you 12 from 1244.
Then there's modulus 10.
12%10
That gives you 2 (divide by 10 and return the remainder - 12/10 = 1.2, or 1 remainder 2)
That is then passed through a function that calculates what segments need to be turned on to represent the digit 2. Let's say that's 00110111
in binary, where each bit represents a segment to display.
To that you want to add the colon, which is in the most significant bit's place. So you set that bit. That's done with the OR
operator, which in C is |
. 0x80
is 10000000
in binary.
OR the two values together:
00110111
10000000
=わ=わ=わ=わ=わ=わ=わ=わ
10110111
That value is then assigned to the segto
variable.
I am not familiar with that library, but I would assume that the setSegments
function is expecting an array of segment settings, a number of digits to apply it to, and maybe a digit offset in the display (at a guess).
In C there is no real difference between using a 1 element array and using a variable and taking its address.
You could just as well have used:
uint8_t segto[1];
int value = 1244;
segto[0] = 0x80 | display.encodeDigit((value/100)%10);
display.setSegments(segto, 1, 1);
If you were doing more than one digit you'd be wanting to use an array anyway.
Explore related questions
See similar questions with these tags.