.NET Gadgeteer; more Simple Graphics

The Display_T35 module and the DisplayModule.SimpleGraphics object can display various graphics.  This example draws ellipses and sets the colors and sizes of the ellipses according to the voltage a GHI Electonics Potentiometer module.  Just for fun it also matches the color of a GHIElectronics MulticolorLed to that of the ellipse that is drawn on the screen.

.NET Gadgeteer Simple Graphics Ellipses

.NET Gadgeteer Simple Graphics Ellipses

On each Timer.Tick event the .NET Gadgeteer.Timer.TickEventHandler reads the potentiometer and resets the size and color of the ellipse on the display to match the color of the MulticolorLed.  Access the SimpleGraphics object from the Gadgeteer.Modules.DisplayModule.SimpleGraphics property.

    void timer_Tick(GT.Timer timer)
    {
        double doubPotVoltage = potentiometer.ReadPotentiometerVoltage();
        PotVoltage potVoltEnum = SetPotEnumAndLED(doubPotVoltage);

        display.SimpleGraphics.DisplayText("Potentiometer ~~~~~ Voltage ~~~~~ Setting",
                        Resources.GetFont(Resources.FontResources.NinaB), led.GetCurrentColor(), 10, 5);

        uint intCode = (uint)potVoltEnum;

        uint xRadius = 30 * intCode;
        uint yRadius = 20 * intCode;

        display.SimpleGraphics.DisplayEllipse(led.GetCurrentColor(), 160, 130, xRadius, yRadius);
    }

.NET Gadgeteer Simple Graphics Ellipses

.NET Gadgeteer Simple Graphics Ellipses

Complete application code follows.

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

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

namespace GadgeteerDisplayGraphicsEllipse
{
    public partial class Program
    {
        GT.Timer timer = new GT.Timer(500);

        void ProgramStarted()
        {
            // Do one-time tasks here
            timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
            Debug.Print("Program Started");
        }

        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            if (button.IsLedOn)
                timer.Stop();
            else
            {
                display.SimpleGraphics.Clear();
                timer.Start();
            }

            button.ToggleLED();
        }

        void timer_Tick(GT.Timer timer)
        {
            double doubPotVoltage = potentiometer.ReadPotentiometerVoltage();
            PotVoltage potVoltEnum = SetPotEnumAndLED(doubPotVoltage);

            display.SimpleGraphics.DisplayText("Potentiometer ~~~~~ Voltage ~~~~~ Setting",
                            Resources.GetFont(Resources.FontResources.NinaB), led.GetCurrentColor(), 10, 5);

            uint intCode = (uint)potVoltEnum;

            uint xRadius = 30 * intCode;
            uint yRadius = 20 * intCode;

            display.SimpleGraphics.DisplayEllipse(led.GetCurrentColor(), 160, 130, xRadius, yRadius);
        }

        enum PotVoltage
        {
            one = 1,
            low,
            mid,
            high,
            top
        }

        private PotVoltage SetPotEnumAndLED(double voltage)
        {
            PotVoltage enumPot = PotVoltage.one;
            for (int i = 0; i < 5; i++)
            {
                if (voltage == 0.0)
                {
                    enumPot = PotVoltage.one;
                    led.TurnWhite();
                    break;
                }
                if (voltage < 1.0)
                {
                    enumPot = PotVoltage.low;
                    led.TurnGreen();
                    break;
                }
                if (voltage < 2.0)
                {
                    enumPot = PotVoltage.mid;
                    led.TurnBlue();
                    break;
                }
                if (voltage < 3.0)
                {
                    enumPot = PotVoltage.high;
                    led.TurnRed();
                    break;
                }

                led.BlinkRepeatedly(GT.Color.Yellow);
                enumPot = PotVoltage.top;
            }

            return enumPot;
        }
    }
}

Advertisements
  1. Gadgeteer Hello World | Devhammer Enterprises

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: