Skip to content

Instantly share code, notes, and snippets.

Created April 6, 2017 16:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/63595fe71b867356ce45f7489d7a85bc to your computer and use it in GitHub Desktop.
Save anonymous/63595fe71b867356ce45f7489d7a85bc to your computer and use it in GitHub Desktop.
using GHIElectronics.TinyCLR.Devices.Display;
using GHIElectronics.TinyCLR.Devices.Pwm;
using GHIElectronics.TinyCLR.Devices.Spi;
using GHIElectronics.TinyCLR.Pins;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
//Logging
Debugger.Log(0, "Category", "Always sent to debug output.\r\n"); //Note the newline
Trace.Assert(false, "Should assert.");
Trace.WriteLineIf(false, "Should not be written.");
Trace.Listeners.Clear(); //Writing to Debug or Trace will no longer go to Debugger.Log
Trace.WriteLine("Should not be written.");
Trace.Listeners.Add(new DefaultTraceListener()); //Now they will write to Debugger.Log again
Trace.WriteLine("Should be written.");
//PWM
//Make sure you only open pins from the same controller the controller was constructed for, in this case Controller1
var controller = PwmController.FromId(G30.PwmPin.Controller1.Id);
var pin = controller.OpenPin(G30.PwmPin.Controller1.PA8);
//SPI
//Keep in mind there is an overload to the provider that allows you to pass your own GpioProvider. Of course, you can implement your own SPI provider too.
var controllers = SpiController.GetControllers(new SpiSoftwareProvider(G30.GpioPin.PA1, G30.GpioPin.PA2, G30.GpioPin.PA3));
var device = controllers[0].GetDevice(new SpiConnectionSettings(G30.GpioPin.PA4) { ClockFrequency = 1000000, DataBitLength = 8, Mode = SpiMode.Mode0, SharingMode = SpiSharingMode.Exclusive });
//Graphics
var displayController = DisplayController.GetDefault(); //Currently returns the hardware LCD controller by default
//Enables the display
displayController.ApplySettings(new LcdControllerSettings {
Width = 480,
Height = 272,
PixelClockRate = 20000000,
PixelPolarity = false,
OutputEnablePolarity = true,
OutputEnableIsFixed = false,
HorizontalFrontPorch = 2,
HorizontalBackPorch = 2,
HorizontalSyncPulseWidth = 41,
HorizontalSyncPolarity = false,
VerticalFrontPorch = 2,
VerticalBackPorch = 2,
VerticalSyncPulseWidth = 10,
VerticalSyncPolarity = false,
});
var screen = Graphics.FromHdc(displayController.Hdc); //Calling flush on the object returned will flush to the display represented by Hdc. Only one active display is supported at this time.
var background = Resources.GetBitmap(Resources.BitmapResources.Background);
var font = Resources.GetFont(Resources.FontResources.small);
screen.DrawImage(background, 0, 40);
screen.FillEllipse(new SolidBrush(Color.FromArgb(100, 0xFF, 0, 0)), 0, 0, 100, 100);
screen.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 0xFF)), 0, 100, 100, 100);
screen.DrawEllipse(new Pen(Color.Blue), 100, 0, 100, 100);
screen.DrawRectangle(new Pen(Color.Red), 100, 100, 100, 100);
screen.DrawLine(new Pen(Color.Green, 5), 250, 0, 220, 240);
screen.DrawString("Hello, world", font, new SolidBrush(Color.White), 10, 220);
var i = 0;
screen.DrawLine(new Pen(Color.Black, 4), 260 + i, 10, 260 + i, 50); i += 4;
screen.DrawLine(new Pen(Color.White, 4), 260 + i, 10, 260 + i, 50); i += 4;
screen.DrawLine(new Pen(Color.Gray, 4), 260 + i, 10, 260 + i, 50); i += 4;
screen.DrawLine(new Pen(Color.Red, 4), 260 + i, 10, 260 + i, 50); i += 4;
screen.DrawLine(new Pen(Color.Green, 4), 260 + i, 10, 260 + i, 50); i += 4;
screen.DrawLine(new Pen(Color.Blue, 4), 260 + i, 10, 260 + i, 50); i += 4;
screen.DrawLine(new Pen(Color.Yellow, 4), 260 + i, 10, 260 + i, 50); i += 4;
screen.DrawLine(new Pen(Color.Purple, 4), 260 + i, 10, 260 + i, 50); i += 4;
screen.DrawLine(new Pen(Color.Teal, 4), 260 + i, 10, 260 + i, 50); i += 4;
screen.Flush();
//Gets a byte array for the given bitmap. Only MemoryBmp is supported at this time.
using (var bmp = new Bitmap(2, 2)) {
var graphics = Graphics.FromImage(bmp);
graphics.DrawLine(new Pen(Color.White, 1), 0, 0, 1, 0);
using (var stream = new MemoryStream()) {
bmp.Save(stream, ImageFormat.MemoryBmp);
var byteArray = stream.ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment