static unsafe void Demo1(RGBLedMatrix matrix) { play = true; try { BdfFont font = BdfFont.Load(@"fonts/10x20.bdf"); BdfFont font1 = BdfFont.Load(@"fonts/8x13B.bdf"); matrix.Fill(0, 0, 0); Thread.Sleep(100); int x = matrix.Width - 1; string text = "Hello .NET IoT"; int fullTextWidth = text.Length * font.Width; while (play) { matrix.DrawText(x, 0, text, font, 0, 0, 255, 0, 0, 0); x--; if (x == -fullTextWidth) { x = matrix.Width - 1; } string d = DateTime.Now.ToString("hh:mm:ss"); matrix.DrawText(0, font.Height + 1, d, font1, 128, 128, 0, 0, 0, 0); Thread.Sleep(25); } } catch (Exception e) { Console.WriteLine(e); } }
private int OnExecute() { var options = new LedMatrixOptions() { Brightness = (byte)Brightness, }; // Check if we have only extreme colors if (Brightness == 100 && FullSaturation(Color) && FullSaturation(BackgroundColor) ) { options.PwmBits = 1; } var matrix = new LedMatrix(options); var canvas = matrix.CreateOffscreenCanvas(); var font = new BdfFont(Font); while (!Console.KeyAvailable) { var text = DateTime.UtcNow.ToString(Format); canvas.Fill(BackgroundColor); canvas.DrawText(font, XOrigin, YOrigin + font.Baseline, Color, text, Spacing); matrix.SwapOnVsync(canvas); Thread.Sleep(TimeSpan.FromMilliseconds(250)); } return(0); }
private Bitmap text2Image(string text) { Bitmap bm_bw = new Bitmap(picBW.Width, picBW.Height); Graphics g = Graphics.FromImage(bm_bw); g.Clear(Color.FromArgb(PIX_WHITE, PIX_WHITE, PIX_WHITE)); StringReader strReader = new StringReader(text); BdfFont bf = BdfFont.getInstance(); int chX = picBW.Width / bf.Width; int chY = picBW.Height / bf.Height; for (int yy = 0; yy < chY; yy++) { string lineText = Strings.StrConv(strReader.ReadLine(), VbStrConv.Wide); if (lineText == null) { break; } int len = lineText.Length; if (len > chX) { len = chX; } int count = 0; for (int xx = 0; xx < len; xx++) { byte[] font = bf.getFontData(lineText[count]); for (int y = 0; y < bf.Height; y++) { int mask = 0x8000; int xbyte = 2; //めんどくさいので横幅2byte限定 int xdata = font[xbyte * y] << 8 | font[xbyte * y + 1]; for (int x = 0; x < bf.Width; x++) { int pix = xdata & mask; int bw; if (pix == 0) { bw = PIX_WHITE; } else { bw = PIX_BLACK; } bm_bw.SetPixel(xx * bf.Width + x, yy * bf.Height + y, Color.FromArgb(bw, bw, bw)); mask >>= 1; } } count++; } } return(bm_bw); }
static unsafe void Demo4(RGBLedMatrix matrix) { play = true; byte blue = 0x15; matrix.Fill(0, 0, blue); using (WebClient client = new WebClient()) { int lastMinute = -1; string temperature = ""; BdfFont font = BdfFont.Load(@"fonts/6x12.bdf"); BdfFont font1 = BdfFont.Load(@"fonts/5x7.bdf"); Bitmap weatherIcon = null; string lastIcon = null; string description = ""; while (play) { DateTime time = DateTime.Now; if (Math.Abs(time.Minute - lastMinute) > 4) { lastMinute = time.Minute; string xml = client.DownloadString("http://api.openweathermap.org/data/2.5/weather?q=Redmond,US&mode=xml&units=imperial&APPID=" + s_weatherKey); XDocument doc = XDocument.Parse(xml); XElement element = doc.Root.Element("temperature"); temperature = ((int)Math.Round(Double.Parse(element.Attribute("value").Value))).ToString(CultureInfo.InvariantCulture); element = doc.Root.Element("weather"); string icon = element.Attribute("icon").Value; description = element.Attribute("value").Value; if (lastIcon != icon) { weatherIcon = new Bitmap("bitmaps/" + icon + ".bmp"); } matrix.DrawBitmap(20, 2, weatherIcon, 255, 255, 255, 0, 0, blue); matrix.DrawText(Math.Max(0, matrix.Width - description.Length * font1.Width), 42, description, font1, 128, 128, 128, 0, 0, blue); matrix.DrawText(2, 2 + font.Height, temperature + "\u00B0", font, 128, 128, 128, 0, 0, blue); } matrix.DrawText(2, 2, time.ToString("ddd"), font, 128, 128, 128, 0, 0, blue); matrix.DrawText(2, matrix.Height - font.Height, time.ToString("hh:mm:sstt"), font, 128, 128, 128, 0, 0, blue); Thread.Sleep(200); } } }
private int OnExecute() { var matrix = new LedMatrix(new LedMatrixOptions()); var canvas = matrix.CreateOffscreenCanvas(); var font = new BdfFont(Font); canvas.DrawText(font, XOrigin, YOrigin + font.Baseline, new Color(0, 255, 0), Text, Spacing); matrix.SwapOnVsync(canvas); while (!Console.KeyAvailable) { Thread.Sleep(250); } return(0); }
private static void ScrollText( RGBLedMatrix matrix, string text, BdfFont font, int startPos, int endPos, byte red, byte green, byte blue, byte bkRed, byte bkGreen, byte bkBlue) { if (startPos < endPos) { return; } text = text + " "; // to clear the text when scrolling int fullTextWidth = text.Length * font.Width; while (startPos >= endPos) { matrix.DrawText(startPos, 0, text, font, red, green, blue, bkRed, bkGreen, bkBlue); startPos--; Thread.Sleep(20); } }
static void Demo3(RGBLedMatrix matrix) { try { play = true; byte blue = 0x10; matrix.Fill(0, 0, blue); TimeZoneInfo [] zones = new TimeZoneInfo [s_citiesData.Length]; string [] weatherUrls = new string [s_citiesData.Length]; for (int i = 0; i < s_citiesData.Length; i++) { weatherUrls[i] = String.Format("http://api.openweathermap.org/data/2.5/weather?q={0},{1}&mode=xml&units=imperial&APPID={2}", s_citiesData[i].City, s_citiesData[i].CountryCode, s_weatherKey); zones[i] = TimeZoneInfo.FindSystemTimeZoneById(s_citiesData[i].ZoneId); } using (WebClient client = new WebClient()) { BdfFont font = BdfFont.Load(@"fonts/6x12.bdf"); BdfFont font1 = BdfFont.Load(@"fonts/5x7.bdf"); BdfFont font2 = BdfFont.Load(@"fonts/4x6.bdf"); int cityIndex = 0; while (play) { string xml = client.DownloadString(weatherUrls[cityIndex]); XDocument doc = XDocument.Parse(xml); XElement element = doc.Root.Element("temperature"); string temperature = ((int)Math.Round(Double.Parse(element.Attribute("value").Value))).ToString(CultureInfo.InvariantCulture); element = doc.Root.Element("weather"); string description = element.Attribute("value").Value + " "; element = doc.Root.Element("humidity"); string humidity = element.Attribute("value").Value + element.Attribute("unit").Value + " "; element = doc.Root.Element("city").Element("sun"); string sunRise = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Parse(element.Attribute("rise").Value, CultureInfo.InvariantCulture), zones[cityIndex]).ToString("hh:mm tt"); string sunSet = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Parse(element.Attribute("set").Value, CultureInfo.InvariantCulture), zones[cityIndex]).ToString("hh:mm tt"); int pos = Math.Max(0, (matrix.Width - (s_citiesData[cityIndex].City.Length * font.Width))) / 2; ScrollText( matrix, s_citiesData[cityIndex].City, font, matrix.Width - 1, pos, 128, 128, 128, 0, 0, blue); int y = font.Height; matrix.DrawText((matrix.Width - (temperature.Length + 1) * font1.Width) / 2, y, temperature + "\u00B0", font1, 255, 255, 0, 0, 0, blue); y += font1.Height + 2; matrix.DrawText(2, y, description, font2, 128, 128, 128, 0, 0, blue); y += font2.Height + 2; matrix.DrawText(2, y, "humidity: ", font2, 128, 128, 128, 0, 0, blue); matrix.DrawText(font2.Width * "humidity: ".Length + 2, y, humidity, font2, 255, 255, 0, 0, 0, blue); y += font2.Height; string localTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, zones[cityIndex]).ToString("hh:mm tt"); matrix.DrawText((matrix.Width - localTime.Length * font.Width) / 2, y, localTime, font, 255, 255, 0, 0, 0, blue); y += font.Height + 2; matrix.DrawText(2, y, "Sun Rise: ", font2, 128, 128, 128, 0, 0, blue); matrix.DrawText(2 + "Sun Rise: ".Length * font2.Width, y, sunRise, font2, 255, 255, 0, 0, 0, blue); y += font2.Height + 2; matrix.DrawText(2, y, "Sun Set: ", font2, 128, 128, 128, 0, 0, blue); matrix.DrawText(2 + "Sun Set: ".Length * font2.Width, y, sunSet, font2, 255, 255, 0, 0, 0, blue); Thread.Sleep(4000); ScrollText( matrix, s_citiesData[cityIndex].City, font, pos, -(pos + s_citiesData[cityIndex].City.Length * font.Width), 128, 128, 128, 0, 0, blue); y = font.Height; matrix.DrawText((matrix.Width - (temperature.Length + 1) * font1.Width) / 2, y, temperature + "\u00B0", font1, 0, 0, blue, 0, 0, blue); y += font1.Height + 2; matrix.DrawText(2, y, description, font2, 0, 0, blue, 0, 0, blue); y += font2.Height + 2; matrix.DrawText(2, y, "humidity: ", font2, 0, 0, blue, 0, 0, blue); matrix.DrawText(font2.Width * "humidity: ".Length + 2, y, humidity, font2, 0, 0, blue, 0, 0, blue); y += font2.Height; matrix.DrawText((matrix.Width - localTime.Length * font.Width) / 2, y, localTime, font, 0, 0, blue, 0, 0, blue); y += font.Height + 2; matrix.DrawText(2, y, "Sun Rise: ", font2, 0, 0, blue, 0, 0, blue); matrix.DrawText(2 + "Sun Rise: ".Length * font2.Width, y, sunRise, font2, 0, 0, blue, 0, 0, blue); y += font2.Height + 2; matrix.DrawText(2, y, "Sun Set: ", font2, 0, 0, blue, 0, 0, blue); matrix.DrawText(2 + "Sun Set: ".Length * font2.Width, y, sunSet, font2, 0, 0, blue, 0, 0, blue); cityIndex = (cityIndex + 1) % s_citiesData.Length; } } } catch (Exception e) { Console.WriteLine(e); } }
private static void WeatherDemo(RGBLedMatrix matrix) { BdfFont font = BdfFont.Load(@"fonts/10x20.bdf"); BdfFont font1 = BdfFont.Load(@"fonts/8x13B.bdf"); matrix.Fill(0, 0, 0); Thread.Sleep(100); int textLeft = 0; const int feedUpdateMs = 500; const int drawIntervalMs = 25; const int iterations = feedUpdateMs / drawIntervalMs; byte Col(float x) { return((byte)Math.Clamp(x * 255, 0, 255)); } string text = string.Empty; int fullTextWidth = 0; Stopwatch sw = Stopwatch.StartNew(); while (s_scenario != null) { text = " * " + string.Join(" * ", WeatherFeed()); fullTextWidth = text.Length * font.Width; for (int i = 0; i < iterations && s_scenario != null; i++, textLeft--) { matrix.DrawText(textLeft, -2, text, font, 0, 0, 255, 0, 0, 0); if (textLeft + fullTextWidth < matrix.Width) { matrix.DrawText(textLeft + fullTextWidth, -2, text, font, 0, 0, 255, 0, 0, 0); } if (textLeft + fullTextWidth <= 0) { textLeft += fullTextWidth; } DateTimeOffset localNow = DateTimeOffset.Now; DateTimeOffset pstNow = TimeZoneInfo.ConvertTime(localNow, s_timeZonePst); string d = pstNow.ToString("hh:mm:ss"); matrix.DrawText(0, font.Height + 1 - 2, d, font1, 0, 255, 0, 0, 0, 0); int halfHeight = matrix.Height / 2; int halfWidth = matrix.Width / 2; float time = sw.ElapsedMilliseconds / 1000f; for (int x = 0; x < matrix.Width; x++) { if (x < halfWidth) { for (int y = halfHeight; y < matrix.Height; y++) { Vector3 col3 = Clock(new Vector2((float)x / halfWidth, (float)(y - halfHeight) / halfHeight), pstNow); Color color = Color.FromArgb(Col(col3.X), Col(col3.Y), Col(col3.Z)); matrix.SetPixel(x, y, color.R, color.G, color.B); } } else { for (int y = halfHeight; y < matrix.Height; y++) { Vector2 uv = new Vector2((float)(x - halfWidth) / halfWidth, (float)(y - halfHeight) / halfHeight); Vector3 col3 = OpenWeatherIcon(uv, s_weatherResponse.Weather[0].Icon, time); Color color = Color.FromArgb(Col(col3.X), Col(col3.Y), Col(col3.Z)); matrix.SetPixel(x, y, color.R, color.G, color.B); } } } Thread.Sleep(drawIntervalMs); } } }