Main Site Documentation

Simple Class to Display Debug Messages on T35 Display


#1

I needed to display some basic debug messages on the T35 Display without worrying about the position of the text or create a complicated ui, so I created a simple DebugLCD class. It displays about 20 lines and then scrolls up when next text is added after that. Don’t know if there is already a simple way to do it, but it works for me :wink:



using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace DebugLCDTest
{
  class DebugLCD
  {
    private Display_T35 lcd = null;
    private Bitmap buffer = null;

    private string[] log = new string[22];
    private int counter = 0;

    private const int startX = 15;
    private const int startY = 10;

    public DebugLCD(Display_T35 LCD)
    {
      lcd = LCD;
      buffer = new Bitmap( (int)LCD.Width, (int)LCD.Height);

      // clear lcd
      lcd.SimpleGraphics.Clear();
    }

    private void drawBuffer()
    {
      lcd.SimpleGraphics.DisplayImage(buffer, 0, 0);
    }

    private void scrollLog()
    {
      for (int c = 0; c < counter - 1; c++)
      {
        log[c] = log[c + 1];
      }

      counter--;
    }

    public void Print(string msg)
    {
      log[counter++] = msg;

      if (counter > 21)
        scrollLog();

      buffer.Clear();

      // draw frame
      buffer.DrawRectangle(Color.White, 1, 5, 5, (int)(lcd.Width - 10), (int)(lcd.Height - 10), 5, 5, Color.White, 0, 0, Color.White, 0, 0, 125);

      // to buffer
      for (int c = 0; c < counter; c++)
      {
        buffer.DrawText(log[c], Resources.GetFont(Resources.FontResources.small), Color.White, startX, startY + (c * 10));
      }

      drawBuffer();
    }

  }
}

Example Program:


using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace DebugLCDTest
{
  public partial class Program
  {
    private DebugLCD debugLCD = null;
    private int counter = 0;

    // This method is run when the mainboard is powered up or reset.   
    void ProgramStarted()
    {
      debugLCD = new DebugLCD(display);

      button.ButtonReleased += new Button.ButtonEventHandler(button_ButtonReleased);

      debugLCD.Print("Program Started");
    }

    void button_ButtonReleased(Button sender, Button.ButtonState state)
    {
      debugLCD.Print("Button Pressed - " + counter.ToString());
      counter++;
    }
  }
}


#2

@ Tom11 - please put this up on the code site. helpful snippets like this tend to get lost in the shuffle here.