var vramAddress = RegRead(RegIndex.REG42_MAIN_SADDR_0) | (uint)(RegRead(RegIndex.REG44_MAIN_SADDR_1) << 16);
private void MemWriteByte(uint memAddress, byte memValue)
{
CsPin.Write(GpioPinValue.Low);
SPI.Write(new byte[] {
(byte)SPIBIT.SPIWRITE_8BIT, //command
(byte)(memAddress >> 16), //address bits 18:16
(byte)((memAddress & 0x0000FF00) >> 8), //address bits 15:8
(byte)(memAddress & 0x000000FF), //address bits 7:0
memValue, //data bits 7:0
});
CsPin.Write(GpioPinValue.High);
}
public void DrawPixel(int x, int y, Color color)
{
if (x < 0 || y < 0 || x >= width || y >= height) return;
uint offset = (uint)(vramAddress + (y * stride) + (x * bytesPerPixel));
switch (imageFormat) {
case ImageDataFormat.RGB_888:
case ImageDataFormat.RGB_888LUT:
//write the color components
MemWriteByte(offset, color.B);
MemWriteByte(offset + 1, color.G);
MemWriteByte(offset + 2, color.R);
break;
}
}
//setup the BitBLT registers
RegWrite(RegIndex.REG82_BLT_CTRL_1, (ushort)(dataType << 2));
RegWrite(RegIndex.REG86_BLT_CMD, 0x0002); //Solid Fill BitBLT
RegWrite(RegIndex.REG8C_BLT_DSADDR_0, (ushort)(vadd & 0xFFFF));
RegWrite(RegIndex.REG8E_BLT_DSADDR_1, (ushort)((vadd >> 16) & 0x0007));
RegWrite(RegIndex.REG90_BLT_RECTOFFSET, (ushort)(stride / bytesPerPixel)); // 781 requires stride in pixels (not bytes)
RegWrite(RegIndex.REG92_BLT_WIDTH, width);
RegWrite(RegIndex.REG94_BLT_HEIGHT, height);
// "c" is converted color bytes from Color class
RegWrite(RegIndex.REG9A_BLT_FGCOLOR_0, (ushort)(c & 0xFFFF));
RegWrite(RegIndex.REG9C_BLT_FGCOLOR_1, (ushort)((c >> 16) & 0x00FF));
// Start the BitBLT
RegWrite(RegIndex.REG80_BLT_CTRL_0, 0x0001);
// Wait for completion.
PollWhileRegBitsHigh(RegIndex.REG84_BLT_STATUS, 0x0001, 0, 1000);
CanvasTextFormat ctf = new CanvasTextFormat();
ctf.FontFamily = font.Source;
ctf.FontSize = (float)fontsize;
ctf.HorizontalAlignment = CanvasHorizontalAlignment.Left;
ctf.VerticalAlignment = CanvasVerticalAlignment.Top;
ctf.WordWrapping = CanvasWordWrapping.NoWrap;
var device = CanvasDevice.GetSharedDevice();
CanvasBitmap cb = CanvasBitmap.CreateFromBytes(device,
new byte[w * h * 4], w, h, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized);
var offscreen = new CanvasRenderTarget(device, w, h, 96);
using (var ds = offscreen.CreateDrawingSession()) {
ds.TextAntialiasing = Microsoft.Graphics.Canvas.Text.CanvasTextAntialiasing.Aliased;
ds.DrawText(text, new Rect(0, 0, w, h), Colors.White, ctf);
}
byte[] buffer = offscreen.GetPixelBytes();
for (int yy = 0; yy < h; yy++) {
if (y + yy >= height) break;
int p = yy * w * 4;
for (int xx = 0; xx < w; xx++) {
if (x + xx >= width) break;
if (buffer[p] != 0x00) DrawPixel(x + xx, y + yy, color);
p += 4;
}
}