SC20100S Dev Thread

Questions and answer around this board.

Known Issue (we will update this post)

  • Input voltage can be 24V, label shows 12V
  • GHI Electronics new logo to be added
  • Click show SP1. this is SPI
  • Buzzer is mislabeled PC7. It should be PA7.
  • Buttons show old style labels. These are LDR, APP, MOD pins.

Is there a location for the source that had originally been loaded on this board when shipped?

@Joel_Riley can you share here?

I’m trying to access the Display on board but I’m having errors, must be something silly.

I try this code:

using GHIElectronics.TinyCLR.Devices.Display;
using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Devices.Spi;
using GHIElectronics.TinyCLR.Drawing;
using GHIElectronics.TinyCLR.Drivers.Sitronix.ST7735;
using GHIElectronics.TinyCLR.Pins;
using System;
using System.Drawing;
using System.Threading;

namespace TinyScreen
{
    class Program
    {
        private static GpioController gpio;

        static void Main()
        {
            gpio = GpioController.GetDefault();

            var spi = SpiController.FromName(SC20100.SpiBus.Spi3);

            var st7735 = new ST7735Controller(
                spi.GetDevice(
                    ST7735Controller.GetConnectionSettings(
                        SpiChipSelectType.Gpio, 
                        SC20100.GpioPin.PD10)),
                    gpio.OpenPin(SC20100.GpioPin.PC4),
                    gpio.OpenPin(SC20100.GpioPin.PE15));
            st7735.SetDataAccessControl(true, true, false, false);
            st7735.Enable();

            var disp = DisplayController.FromProvider(st7735);
            disp.SetConfiguration(new SpiDisplayControllerSettings { Width = 160, Height = 128 });
            
            var bl = gpio.OpenPin(SC20100.GpioPin.PE5);
            bl.Write(GpioPinValue.High);
            bl.SetDriveMode(GpioPinDriveMode.Output);

            var hdc = GraphicsManager.RegisterDrawTarget(new DrawTarget(disp));
            var screen = Graphics.FromHdc(hdc);

            var rnd = new Random();
            var x = 10;
            var y = 10;
            var vx = rnd.Next(20) - 10;
            var vy = rnd.Next(20) - 10;
            var color = new Pen(Color.Green);
            var teal = new SolidBrush(Color.Teal);

            // This is a special built-in font for simple SPI displays
            var font = new Font("GHIMono8x5", 8);

            while (true)
            {
                x += vx;
                y += vy;

                if (x >= disp.ActiveConfiguration.Width || x < 0) vx *= -1;
                if (y >= disp.ActiveConfiguration.Height || y < 0) vy *= -1;

                screen.Clear(Color.Black);
                screen.DrawString("Hello Adam!", font, teal, 40, 10);
                screen.DrawEllipse(color, x, y, 10, 10);
                screen.Flush();
                Thread.Sleep(10);
            }
        }

        public sealed class DrawTarget : IDrawTarget
        {
            private readonly DisplayController parent;
            private readonly byte[] buffer;

           public DrawTarget(DisplayController parent)
            {
                this.parent = parent;

                this.Width = parent.ActiveConfiguration.Width;
                this.Height = parent.ActiveConfiguration.Height;

                this.buffer = new byte[this.Width * this.Height * 2];
            }

            public int Width { get; }
            public int Height { get; }

            public void Dispose() { }
            public byte[] GetData() => this.buffer;
            public Color GetPixel(int x, int y) => throw new NotSupportedException();

            public void Clear(Color color) => Array.Clear(this.buffer, 0, this.buffer.Length);

            public void Flush()
            {
                this.parent.DrawBuffer(0, 0, 0, 0, this.Width, this.Height, this.Width, this.buffer, 0);
            }

            public void SetPixel(int x, int y, Color color)
            {
                if (x < 0 || y < 0 || x >= this.Width || y >= this.Height) return;

                var idx = (y * this.Width + x) * 2;
                var clr = color.ToArgb();
                var red = (clr & 0b0000_0000_1111_1111_0000_0000_0000_0000) >> 16;
                var green = (clr & 0b0000_0000_0000_0000_1111_1111_0000_0000) >> 8;
                var blue = (clr & 0b0000_0000_0000_0000_0000_0000_1111_1111) >> 0;

                this.buffer[idx] = (byte)((red & 0b1111_1000) | ((green & 0b1110_0000) >> 5));
                this.buffer[idx + 1] = (byte)(((green & 0b0001_1100) << 3) | ((blue & 0b1111_1000) >> 3));
            }
        }
    }
}

When I get to the line:
var screen = Graphics.FromHdc(hdc);

I get the error :
The thread ‘’ (0x2) has exited with code 0 (0x0).
#### Exception System.ArgumentException - 0x00000000 (1) ####
#### Message: Not supported
#### System.Drawing.Graphics::FromHdc [IP: 0040] ####
#### TinyScreen.Program::Main [IP: 007e] ####
Exception thrown: ‘System.ArgumentException’ in GHIElectronics.TinyCLR.Drawing.dll
An unhandled exception of type ‘System.ArgumentException’ occurred in GHIElectronics.TinyCLR.Drawing.dll
Additional information: Not supported

I’m using all the preview 2 packages, the hdc pointer is 1 and the disp.Hdc is 0.
The screen turns on as soon as I enable the back-light but is has random color pixels all over.
I try changing Graphics.FromHdc(disp.Hdc) to skip the DrawingTarget but got the same result.

Maybe I’m setting the pins wrong?
Not sure what the control pin is, I’m guessing from the label RS: PC4?

I am seeing the same error as Mogollon

Step into: Stepping over non-user code ‘System.Drawing.Graphics.FromHdc’
#### Exception System.ArgumentException - 0x00000000 (1) ####
#### Message: Not supported
#### System.Drawing.Graphics::FromHdc [IP: 0040] ####
#### SPIDisplay.Program::Main [IP: 006b] ####
Exception thrown: ‘System.ArgumentException’ in GHIElectronics.TinyCLR.Drawing.dll
An unhandled exception of type ‘System.ArgumentException’ occurred in GHIElectronics.TinyCLR.Drawing.dll
Additional information: Not supported

We simplified and improved graphics. The tutorials needs to be updated! @Joel_Riley @Dat_Tran we need example here and updated docs

1 Like

this is for N18

static void Main()
        {

            DoTestN18();

            Thread.Sleep(-1);
        }

        const int SCREEN_WIDTH = 128;
        const int SCREEN_HEIGHT = 160;
        static ST7735Controller st7735 = null;

        static void DoTestN18()
        {
            // Init spi controllet for N18
            var spi = SpiController.FromName("GHIElectronics.TinyCLR.NativeApis.STM32H7.SpiController\\2");
            var gpio = GpioController.GetDefault();

            st7735 = new ST7735Controller(
                spi.GetDevice(ST7735Controller.GetConnectionSettings(SpiChipSelectType.Gpio, 3*16+10)), // ChipSelect 
                gpio.OpenPin(2*16+4), // Pin RS
                gpio.OpenPin(4*16+15) // Pin RESET

            );

            var bl = gpio.OpenPin(4*16+5); // back light

            bl.Write(GpioPinValue.High);
            bl.SetDriveMode(GpioPinDriveMode.Output);

            // Hook N18 to TinyCLR DisplayController
            var displayController = DisplayController.FromProvider(st7735);

            st7735.SetDataAccessControl(!true, !true, false, false); // rotate the screen

            displayController.SetConfiguration(new SpiDisplayControllerSettings { Width = SCREEN_WIDTH, Height = SCREEN_HEIGHT, DataFormat = DisplayDataFormat.Rgb565 });

            displayController.Enable();

            // Create flush event 
            Graphics.OnFlushEvent += Graphics_OnFlushEvent;

            // Create bitmap buffer
            var screen = Graphics.FromImage(new Bitmap(displayController.ActiveConfiguration.Width, displayController.ActiveConfiguration.Height));

            var background = Resources.GetBitmap(Resources.BitmapResources.smallJpegBackground);
            var font = Resources.GetFont(Resources.FontResources.small);

            int imageY = 1;
            int imageX = 10;


            var rectY = 35;
            var rectW = 35;
            var rectH = 35;
            var textY = 70;
            var textX = 30;

            screen.DrawImage(background, imageX, imageY);

            imageX += 0;
            imageY += 0;

            if (imageX > SCREEN_WIDTH) imageX = 0;
            if (imageY > SCREEN_HEIGHT) imageY = 0;

            screen.FillEllipse(new SolidBrush(System.Drawing.Color.FromArgb(100, 0xFF, 0, 0)), 0, 0, rectW, rectH);
            screen.DrawRectangle(new Pen(System.Drawing.Color.Red), SCREEN_WIDTH - rectW - 1, SCREEN_HEIGHT - rectH, rectW, rectH);
            screen.DrawEllipse(new Pen(Color.Blue), 0 + 1, SCREEN_HEIGHT - rectH, rectW, rectH);

            screen.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 0xFF)), 0, rectY, SCREEN_WIDTH / 4, rectH);
            screen.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0xFF, 0)), SCREEN_WIDTH / 4, rectY, SCREEN_WIDTH / 4, rectH);
            screen.FillRectangle(new SolidBrush(Color.FromArgb(100, 0xFF, 0, 0)), SCREEN_WIDTH / 4 * 2, rectY, SCREEN_WIDTH / 4, rectH);
            screen.FillRectangle(new SolidBrush(Color.FromArgb(100, 0xFF, 0xFF, 0xFF)), SCREEN_WIDTH / 4 * 3, rectY, SCREEN_WIDTH / 4, rectH);

            screen.DrawString("Transparent", font, new SolidBrush(Color.White), 20, 10);

            screen.DrawString("Blue: Hello, world", font, new SolidBrush(Color.Blue), textX, textY);
            screen.DrawString("Green: Hello, world", font, new SolidBrush(Color.Green), textX, textY + 10);
            screen.DrawString("Red: Hello, world", font, new SolidBrush(Color.Red), textX, textY + 20);
            screen.DrawString("White: Hello, world", font, new SolidBrush(Color.White), textX, textY + 30);
            screen.Flush();

            Thread.Sleep(-1);
        }


        private static void Graphics_OnFlushEvent(IntPtr hdc, byte[] data)
        {
            st7735.DrawBuffer(data);
        }

Also, GraphicsManager is no longer on TinyCLR2.0

Thanks Dat.

This makes it very easy :slight_smile:

I did face some trouble in adding the resource and had to resort to using

Resource1.GetFont(Resource1.FontResources.arial);

Resource1 is my resx file name, was Resources your resx file name or was it the folder/package ?

I will try to create a new project to see if I can reproduce this again - it could be something that I did incorrectly.

2 Likes

Thanks Gus - it would be really helpful if we had a working copy of the generic 4.3 inch or 7 inch display/camera demo that we could load onto the SC20260D - I tried loading the tutorial from new-docs/tinyclr/tutorials/camera and it’s using the older graphics routines. The tutorials/display section only has a demo on the little built in display on the SC20100. Github doesn’t have anything on sitcore samples or demos yet, right? - many of the links take you there.

The samples are getting uploaded this week. @Greg_Norris?

2 Likes

Thanks Guys!!

1 Like

Is there any known issue with SPI4 on the SC20100 Dev REV A ?

I just switched a device from SPI3 to SPI4 and unable to get back a response

        SPIDataSettings = new SpiConnectionSettings()
        {
            ChipSelectType = SpiChipSelectType.Gpio,
            ChipSelectLine = SC20100.GpioPin.PA2,
            Mode = SpiMode.Mode0,
            ClockFrequency = 2 * 1000 * 1000,       //2Mhz
            DataBitLength = 8,
        };

        var controller = SpiController.FromName(SC20100.SpiBus.Spi4);
        SPIDataDevice = controller.GetDevice(SPIDataSettings);

In code or you looked at analyzer and no activity on the bus?

No activity on bus

SPI4

For comparison on SPI3 this is what I get

1 Like

we will check

I see the clock out on SPI4, SPI4 on

PE12, PE13, PE14,

Make sure right pins connected.

Thanks Dat - I will verify my pins again.

I am still using Preview1 firmware - should I use Preview 2 ?

Why use preview1? In this early stage, we all should always use the latest release unless we are comparing something to previous release.

Will upgrade as soon as I get to my workbench :slight_smile:

I was under the impression that Preview2 was related to performance only - I am trying to use the slower version until I encounter a show stopper :wink:

2 Likes