Skip to content

Instantly share code, notes, and snippets.

@hatsunea
Last active August 12, 2022 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hatsunea/46431f6dd7079b30333eeb450ea5f984 to your computer and use it in GitHub Desktop.
Save hatsunea/46431f6dd7079b30333eeb450ea5f984 to your computer and use it in GitHub Desktop.
using System;
using nanoFramework.M5Stack;
using nanoFramework.Presentation.Media;
namespace M5StickCRTCSample
{
/// <summary>
/// 横45x縦113=5085
/// </summary>
internal class Lcd7Segment
{
const int Width = 45;
const int Height = 113;
internal Color BackgroundColor { get; set; } = Color.Black;
internal Color ForegroundColor { get; set; } = Color.Green;
private ushort[] Buffer = new ushort[5085];
internal void Display(int x, int y, int value)
{
CreateSprinte(value);
Screen.Write((ushort)x, (ushort)y, Width, Height, Buffer);
}
private void CreateSprinte(int value)
{
switch (value)
{
case 0:
{
DisplaySegA(ForegroundColor);
DisplaySegB(ForegroundColor);
DisplaySegC(ForegroundColor);
DisplaySegD(ForegroundColor);
DisplaySegE(ForegroundColor);
DisplaySegF(ForegroundColor);
DisplaySegG(BackgroundColor);
break;
}
case 1:
{
DisplaySegA(BackgroundColor);
DisplaySegB(ForegroundColor);
DisplaySegC(ForegroundColor);
DisplaySegD(BackgroundColor);
DisplaySegE(BackgroundColor);
DisplaySegF(BackgroundColor);
DisplaySegG(BackgroundColor);
break;
}
case 2:
{
DisplaySegA(ForegroundColor);
DisplaySegB(ForegroundColor);
DisplaySegC(BackgroundColor);
DisplaySegD(ForegroundColor);
DisplaySegE(ForegroundColor);
DisplaySegF(BackgroundColor);
DisplaySegG(ForegroundColor);
break;
}
case 3:
{
DisplaySegA(ForegroundColor);
DisplaySegB(ForegroundColor);
DisplaySegC(ForegroundColor);
DisplaySegD(ForegroundColor);
DisplaySegE(BackgroundColor);
DisplaySegF(BackgroundColor);
DisplaySegG(ForegroundColor);
break;
}
case 4:
{
DisplaySegA(BackgroundColor);
DisplaySegB(ForegroundColor);
DisplaySegC(ForegroundColor);
DisplaySegD(BackgroundColor);
DisplaySegE(BackgroundColor);
DisplaySegF(ForegroundColor);
DisplaySegG(ForegroundColor);
break;
}
case 5:
{
DisplaySegA(ForegroundColor);
DisplaySegB(BackgroundColor);
DisplaySegC(ForegroundColor);
DisplaySegD(ForegroundColor);
DisplaySegE(BackgroundColor);
DisplaySegF(ForegroundColor);
DisplaySegG(ForegroundColor);
break;
}
case 6:
{
DisplaySegA(ForegroundColor);
DisplaySegB(BackgroundColor);
DisplaySegC(ForegroundColor);
DisplaySegD(ForegroundColor);
DisplaySegE(ForegroundColor);
DisplaySegF(ForegroundColor);
DisplaySegG(ForegroundColor);
break;
}
case 7:
{
DisplaySegA(ForegroundColor);
DisplaySegB(ForegroundColor);
DisplaySegC(ForegroundColor);
DisplaySegD(BackgroundColor);
DisplaySegE(BackgroundColor);
DisplaySegF(BackgroundColor);
DisplaySegG(BackgroundColor);
break;
}
case 8:
{
DisplaySegA(ForegroundColor);
DisplaySegB(ForegroundColor);
DisplaySegC(ForegroundColor);
DisplaySegD(ForegroundColor);
DisplaySegE(ForegroundColor);
DisplaySegF(ForegroundColor);
DisplaySegG(ForegroundColor);
break;
}
case 9:
{
DisplaySegA(ForegroundColor);
DisplaySegB(ForegroundColor);
DisplaySegC(ForegroundColor);
DisplaySegD(ForegroundColor);
DisplaySegE(BackgroundColor);
DisplaySegF(ForegroundColor);
DisplaySegG(ForegroundColor);
break;
}
default:
{
DisplaySegA(BackgroundColor);
DisplaySegB(BackgroundColor);
DisplaySegC(BackgroundColor);
DisplaySegD(BackgroundColor);
DisplaySegE(BackgroundColor);
DisplaySegF(BackgroundColor);
DisplaySegG(BackgroundColor);
break;
}
}
}
private void DisplaySegA(Color color)
{
// 6,0 -
DisplaySegH(6, 0, color);
}
private void DisplaySegB(Color color)
{
// 32,10 |
DisplaySegV(32, 10, color);
}
private void DisplaySegC(Color color)
{
// 32,61 |
DisplaySegV(32, 61, color);
}
private void DisplaySegD(Color color)
{
// 6,100 -
DisplaySegH(6, 100, color);
}
private void DisplaySegE(Color color)
{
// 0,61 |
DisplaySegV(0, 61, color);
}
private void DisplaySegF(Color color)
{
// 0,10 |
DisplaySegV(0, 10, color);
}
private void DisplaySegG(Color color)
{
// 6,51 -
DisplaySegH(6, 51, color);
}
private void DisplaySegV(int x, int y, Color color)
{
var color16bit = ColorUtility.To16Bpp(color);
//0,6 - 0,35
for (var posY = y + 6; posY <= y + 35; posY++) WriteBuffer((ushort)(x + 0), (ushort)posY, color16bit);
//1,6 - 0,35
for (var posY = y + 6; posY <= y + 35; posY++) WriteBuffer((ushort)(x + 1), (ushort)posY, color16bit);
//2,4 - 2,37
for (var posY = y + 4; posY <= y + 37; posY++) WriteBuffer((ushort)(x + 2), (ushort)posY, color16bit);
//3,4 - 3,37
for (var posY = y + 4; posY <= y + 37; posY++) WriteBuffer((ushort)(x + 3), (ushort)posY, color16bit);
//4,2 - 4,39
for (var posY = y + 2; posY <= y + 39; posY++) WriteBuffer((ushort)(x + 4), (ushort)posY, color16bit);
//5,2 - 5,39
for (var posY = y + 2; posY <= y + 39; posY++) WriteBuffer((ushort)(x + 5), (ushort)posY, color16bit);
//6,0 - 6,41
for (var posY = y + 0; posY <= y + 41; posY++) WriteBuffer((ushort)(x + 6), (ushort)posY, color16bit);
//7,2 - 7,39
for (var posY = y + 2; posY <= y + 39; posY++) WriteBuffer((ushort)(x + 7), (ushort)posY, color16bit);
//8,2 - 8,39
for (var posY = y + 2; posY <= y + 39; posY++) WriteBuffer((ushort)(x + 8), (ushort)posY, color16bit);
//9,4 - 9,37
for (var posY = y + 4; posY <= y + 37; posY++) WriteBuffer((ushort)(x + 9), (ushort)posY, color16bit);
//10,4 - 10,37
for (var posY = y + 4; posY <= y + 37; posY++) WriteBuffer((ushort)(x + 10), (ushort)posY, color16bit);
//11,6 - 11,35
for (var posY = y + 6; posY <= y + 35; posY++) WriteBuffer((ushort)(x + 11), (ushort)posY, color16bit);
//12,6 - 12,35
for (var posY = y + 6; posY <= y + 35; posY++) WriteBuffer((ushort)(x + 12), (ushort)posY, color16bit);
}
private void DisplaySegH(int x, int y, Color color)
{
var color16bit = ColorUtility.To16Bpp(color);
//6,0 - 26,0
for (var posX = x + 6; posX <= x + 26; posX++) WriteBuffer((ushort)posX, (ushort)(y + 0), color16bit);
//6,1 - 26,1
for (var posX = x + 6; posX <= x + 26; posX++) WriteBuffer((ushort)posX, (ushort)(y + 1), color16bit);
//4,2 - 28.2
for (var posX = x + 4; posX <= x + 28; posX++) WriteBuffer((ushort)posX, (ushort)(y + 2), color16bit);
//4.3 - 28,3
for (var posX = x + 4; posX <= x + 28; posX++) WriteBuffer((ushort)posX, (ushort)(y + 3), color16bit);
//2,4 - 30,4
for (var posX = x + 2; posX <= x + 30; posX++) WriteBuffer((ushort)posX, (ushort)(y + 4), color16bit);
//2,5 - 30,5
for (var posX = x + 2; posX <= x + 30; posX++) WriteBuffer((ushort)posX, (ushort)(y + 5), color16bit);
//0,6 - 32,6
for (var posX = x + 0; posX <= x + 32; posX++) WriteBuffer((ushort)posX, (ushort)(y + 6), color16bit);
//2,7 - 30,7
for (var posX = x + 2; posX <= x + 30; posX++) WriteBuffer((ushort)posX, (ushort)(y + 7), color16bit);
//2,8 - 30,8
for (var posX = x + 2; posX <= x + 30; posX++) WriteBuffer((ushort)posX, (ushort)(y + 8), color16bit);
//4,9 - 28,9
for (var posX = x + 4; posX <= x + 28; posX++) WriteBuffer((ushort)posX, (ushort)(y + 9), color16bit);
//4,10 - 28,10
for (var posX = x + 4; posX <= x + 28; posX++) WriteBuffer((ushort)posX, (ushort)(y + 10), color16bit);
//6,11 - 26,11
for (var posX = x + 6; posX <= x + 26; posX++) WriteBuffer((ushort)posX, (ushort)(y + 11), color16bit);
//6,12 - 26 - 12
for (var posX = x + 6; posX <= x + 26; posX++) WriteBuffer((ushort)posX, (ushort)(y + 12), color16bit);
}
private void WriteBuffer(ushort x, ushort y, ushort color)
{
Buffer[x + y * Width] = color;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment