Description
Describe the bug
I am using https://github.com/dotnet/iot/tree/main/src/devices/Tm16xx on Raspberry Pi 5 (.NET 8) to control 4 digit 7 segment display based on TM1637. Most of the times I get strange characters. Only about one out of 10 tries it is showing correct digits.
Steps to reproduce
Tm1637 tm = new Tm1637(5, 6);
tm.Brightness = 7;
tm.ScreenOn = true;
Character[] toDisplay = new Character[4] {
Character.Digit1,
Character.Digit2 | Character.Dot,
Character.Digit3,
Character.Digit4
};
tm.Display(toDisplay);
Thread.Sleep(TimeSpan.FromSeconds(3));
Character[] toDisplay2 = new Character[4] {
Character.Digit9,
Character.Digit8 | Character.Dot,
Character.Digit7,
Character.Digit6
};
tm.Display(toDisplay2);
tm.Dispose();
Expected behavior
The display shows "12:34" followed by 1 second pause followed by "98:76"
Actual behavior
The displayed characters are very unstable. Sometimes when I start the application I can see "12:34" but then it turns to something like "...87..." (or simply the display does not show anything) where "..." is some unknown digit - looks like only part of the 7 segments of the digits are lit. If I dotnet build
the app and start the application form the output folder multiple times, I get different results. Only about 1 out of 10 app starts will correctly display "12:34" followed by "98:76". Very often the "12:34" is also broken. If I restart the device (by switching its power off and on again) I eventually get "12:34" more often but "98:76" is still broken.
Versions used
dotnet --info
- I am using VSCode and Remote SSH to develop directly on Raspberry Pi so dotnet is installed only on the Raspberry Pi 5 device:
Host:
Version: 8.0.2
Architecture: arm64
Commit: 1381d5ebd2
.NET SDKs installed:
8.0.201 [/home/pi/.dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.2 [/home/pi/.dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.2 [/home/pi/.dotnet/shared/Microsoft.NETCore.App]
- Version of
System.Device.Gpio
package
<PackageReference Include="System.Device.Gpio" Version="3.1.0" />
- Version of
Iot.Device.Bindings
package (not needed if bug is inSystem.Device.Gpio
)
<PackageReference Include="Iot.Device.Bindings" Version="3.1.0" />
If I stop the dotnet app, don't change anything in the wiring and use this Python app - https://gitlab.com/matsievskiysv/gpiod-tm1637/-/tree/master/examples/clock (changed "/dev/gpiochip0"
to "/dev/gpiochip4"
because of Raspberry Pi 5) - I always get correct result no matter what - the display shows all the sample values specified in the python code without any glitch - tried it multiple times.
EDIT: The mentioned Python lib ( https://gitlab.com/matsievskiysv/gpiod-tm1637/-/blob/master/tm1637.py#L41 ) uses 10 microseconds delay between writes:
TM1637_DELAY = 10e-6 # 10us delay between clk/dio pulses
while Tm1637.cs
uses 1 microsecond ( https://github.com/dotnet/iot/blob/8460898df2061230a5d45832a56a14d731d117e5/src/devices/Tm16xx/Tm1637.cs#L19C9-L19C17 ):
// According to the doc, the clock pulse width minimum is 400 ns
// And waiting time between clk up and down is 1 µs
private const byte ClockWidthMicroseconds = 1;
Can we have ClockWidthMicroseconds
configurable ?
EDIT2: I can confirm that setting the ClockWidthMicroseconds
to 10 (I copy/pasted all the necessary .cs files inside https://github.com/dotnet/iot/tree/8460898df2061230a5d45832a56a14d731d117e5/src/devices/Tm16xx and also https://github.com/dotnet/iot/blob/8460898df2061230a5d45832a56a14d731d117e5/src/devices/Common/System/Device/DelayHelper.cs to my project and changed one line to private const byte ClockWidthMicroseconds = 10;
) made the code 100% working.