C# 基于HT16K33的十四段控制并不总是在Windows 10 IoT Core上显示给定位掩码的正确字符
我想为运行Microsoft Windows 10 IoT Core的Raspberry在我的屏幕上的14段控制显示(4并排)上显示一个字符 对于一些像“HELL”(来自“Hello”)这样的消息,我的源代码是否如预期工作,但对于其他像“DEMO”这样的消息,它显示的是垃圾。这对我来说很奇怪,因为这两条消息都包含字母“E”。但它只工作一次 我的最后一个想法是,C# 基于HT16K33的十四段控制并不总是在Windows 10 IoT Core上显示给定位掩码的正确字符,c#,uwp,raspberry-pi,windows-10-iot-core,C#,Uwp,Raspberry Pi,Windows 10 Iot Core,我想为运行Microsoft Windows 10 IoT Core的Raspberry在我的屏幕上的14段控制显示(4并排)上显示一个字符 对于一些像“HELL”(来自“Hello”)这样的消息,我的源代码是否如预期工作,但对于其他像“DEMO”这样的消息,它显示的是垃圾。这对我来说很奇怪,因为这两条消息都包含字母“E”。但它只工作一次 我的最后一个想法是,位掩码[1]&0xFF;与位移位(位掩码[0]>>8)和&0xFF不同,如果我们要创建一个不拆分的位掩码 示例消息(输入->显示在屏幕上)
位掩码[1]&0xFF;
与位移位(位掩码[0]>>8)和&0xFF
不同,如果我们要创建一个不拆分的位掩码
示例消息(输入->显示在屏幕上)
- “地狱”->地狱
- “演示”->,“(3个空格,逗号)
private readonly byte[] segmentBuffer = Enumerable.Repeat(Convert.ToByte(0b00000000), BUFFER_SIZE).ToArray();
字母D的位掩码:
private static readonly Dictionary<char, byte[]> BITMASK_DICTIONARY = new Dictionary<char, byte[]>{
{ 'D', new byte[]{0b00010010, 0b00001111},
....
}
};
将更改写入设备(message=“DEMO”):
for(int i=0;i
链接
- 我的C#on GitHub
- python源代码
for (int i = 0; i < message.Length; i++)
{
var bitmask = ConvertCharToBitmask(message[i]);
Logger.Log(this, $"Writing char: '{message[i]}' at index: {i}.");
segmentBuffer[i * 2] = Convert.ToByte(bitmask[1] & 0xFF);
segmentBuffer[i * 2 + 1] = Convert.ToByte(bitmask[0] & 0xFF);
}
解决方案 这是一个多步骤的错误修正
0x00
添加到Show()
方法中更新后的源代码现已发布到GitHub。您的意思是只将[0]切换到[1],反之亦然?此更改没有任何效果。另一位用户的意思是,字节交换与位移位不同。我必须深入研究它。感谢您的帮助!我更新了代码,这是我的最后一次响应。它似乎与python代码匹配。谢谢!总是很有帮助的。
for (int i = 0; i < message.Length; i++)
{
var bitmask = ConvertCharToBitmask(message[i]);
segmentBuffer[i * 2] = Convert.ToByte(bitmask[0] & 0xFF);
segmentBuffer[i * 2 + 1] = Convert.ToByte(bitmask[1] & 0xFF);
}
// Write buffer to device.
ht16k33.Write(segmentBuffer);
for (int i = 0; i < message.Length; i++)
{
var bitmask = ConvertCharToBitmask(message[i]);
Logger.Log(this, $"Writing char: '{message[i]}' at index: {i}.");
segmentBuffer[i * 2] = Convert.ToByte(bitmask[1] & 0xFF);
segmentBuffer[i * 2 + 1] = Convert.ToByte(bitmask[0] & 0xFF);
}
private static readonly Dictionary<char, int> BITMASK_DICTIONARY = new Dictionary<char, int>
{
{ ' ', 0b0000000000000000 },
{ 'D', 0b0001001000001111 },
};
private int ConvertCharToBitmask(char character)
{
// Check if char is available.
if (BITMASK_DICTIONARY.Keys.Contains(character))
{
return BITMASK_DICTIONARY[character];
}
// If not, return default.
return GetDefaultCharBitmask();
}
private int GetDefaultCharBitmask()
{
return BITMASK_DICTIONARY[DEFAULT_BITMASK_CHAR];
}
public void Show(string message)
{
// Ensure message has valid length.
if(message.Length > NUMBER_OF_SEGMENTS)
{
throw new OperationCanceledException($"Maximum message length is {NUMBER_OF_SEGMENTS} characters.");
}
// Update buffer
Logger.Log(this, $"Show for {message}");
for (int i = 0; i < message.Length; i++)
{
var bitmask = ConvertCharToBitmask(message[i]);
Logger.Log(this, $"Writing char: '{message[i]}' at index: {i}.");
segmentBuffer[i * 2] = Convert.ToByte(bitmask & 0xFF);
segmentBuffer[i * 2 + 1] = Convert.ToByte(bitmask >> 8 & 0xFF);
}
// Write buffer to device.
ht16k33.Write(segmentBuffer);
}