MEDC Berlin - Micro Framework Session Demo

Those of you at my MEDC session in Berlin would have seen me build a Micro Framework application, from scratch, that allowed you to visually set a desired temperature and then communicate this across an RS232 interface.

For the demo I used the Freescale Emulator and i.MXS board.  However it is possible to recreate the majority of the demo using just the i.MXS emulator, which is freely downloadable.

To recreate the demo you will need to have installed:

Instructions:

  1. Create a new Windows Application Micro Framework project.
  2. Replace/Amend Program.cs with the code below.
  3. Add the Freescale CPU.cs file from the Developer Kit Sample Project
  4. In the project properties, set the Transport as "Emulator" and the device as "i.MXS Emulator"
  5. Build & Run

If you are using real hardware, don't forget to set the Configuration Manager option to "deploy".

Please let me know if you have any questions

-Dave

--------------------------------

using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;

using System.Text;                              // UTF Encoding
using Microsoft.SPOT.Hardware;                  // Added - CPU.cs
using Microsoft.SPOT.Hardware.FreescaleMXSDemo; // Added - CPU.cs
using Microsoft.SPOT.Presentation.Media;        // Colour, etc

namespace MEDCDemo1
{
    public class Program : Microsoft.SPOT.Application
    {
        // Instantiate Resources & variables
        static Bitmap Canvas;                   // Bitmap Canvas
        static Font font = Resources.GetFont(Resources.FontResources.small);
        static SerialPort serialPort = null;    // Serial Communication

        static int temp = 18;                   // Default Temperature
        static int mintemp = 12;                // Minimum Temperature
        static int maxtemp = 24;                // Maximum Temperature
       
        public static void Main()
        {
            Program myApplication = new Program();

            Window mainWindow = myApplication.CreateWindow();

            // Initialise the drawing Canvas
            Canvas = new Bitmap(mainWindow.Width, mainWindow.Height);       // Bitmap holding the picture
           
            // Set up serial port
            SerialPort.Configuration serialConfig = new SerialPort.Configuration((SerialPort.Serial)0, (SerialPort.BaudRate)115200, false);
            serialPort = new SerialPort(serialConfig);
            Debug.Print("Serial port is writing/listening to " + serialPort.Config.Com + " at " + serialPort.Config.Speed);
       
            // Set up Up Button Interrupt - Up button is GPIO_PORT_B_11
            InterruptPort UpButton = new InterruptPort(Pins.GPIO_PORT_B_11, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
            UpButton.OnInterrupt += new GPIOInterruptEventHandler(UpButtonPush);

            // Set up Down Button Interrupt - Down button is GPIO_PORT_B_10
            InterruptPort DownButton = new InterruptPort(Pins.GPIO_PORT_B_10, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
            DownButton.OnInterrupt += new GPIOInterruptEventHandler(DownButtonPush);

            // Create the object that configures the GPIO pins to buttons.
            //GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);

            // Start the application
            myApplication.Run(mainWindow);
        }

        private Window mainWindow;

        public Window CreateWindow()
        {
            // Create a window object and set its size to the
            // size of the display.
            mainWindow = new Window();
            mainWindow.Height = SystemMetrics.ScreenHeight;
            mainWindow.Width = SystemMetrics.ScreenWidth;

            // Create a single text control.
            //Text text = new Text();

            //text.Font = Resources.GetFont(Resources.FontResources.small);
            //text.TextContent = Resources.GetString(Resources.StringResources.String1);
            //text.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
            //text.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;

            // Add the text control to the window.
            //mainWindow.Child = text;

            // Connect the button handler to all of the buttons.
            //mainWindow.AddHandler(Buttons.ButtonUpEvent, new ButtonEventHandler(OnButtonUp), false);

            // Set the window visibility to visible.
            mainWindow.Visibility = Visibility.Visible;

            // Attach the button focus to the window.
            Buttons.Focus(mainWindow);

            return mainWindow;
        }

        private void OnButtonUp(object sender, ButtonEventArgs e)
        {
            // Print the button code to the Visual Studio output window.
            Debug.Print(e.Button.ToString());
        }

        // //
        // Handle Up Button Push Event //
        // //
        public static void UpButtonPush(Cpu.Pin port, Boolean state, TimeSpan time)
        {
            if (state) // Only handle button down
            {
                Debug.Print("Up");

                if (temp < maxtemp)  // Ensure we don't go above max
                {
                    temp++;
                    Drawtemp(temp);
                    ChangeTemp(temp);
                }
            }
        }

        // //
        // Handle Down Button Push Event //
        // //
        public static void DownButtonPush(Cpu.Pin port, Boolean state, TimeSpan time)
        {
            if (state) // Only handle button down
            {
                Debug.Print("Down");

                if (temp > mintemp)  // Ensure we don't go below min
                {
                    temp--;
                    Drawtemp(temp);
                    ChangeTemp(temp);
                }
            }
        }

        // //
        // Helper - Draw Graphic Display //
        // //
        private static void Drawtemp(int temp)
        {
            // Calculate temperature interval
            int y = Canvas.Height - ((Canvas.Height / (maxtemp - mintemp)) * (temp - mintemp));

            // Draw white background
            Canvas.DrawRectangle(Color.White, 1, 0, 0, Canvas.Width, Canvas.Height, 0, 0, Color.White, 0, 0, Color.White, Canvas.Width, Canvas.Height, 256);
            // Draw Temperature Bar
            Canvas.DrawRectangle(Color.Black, 25, 0, y, Canvas.Width, 1, 0, 0, 0, 0, 0, 0, 0, 0, 256);
            // Add Temperature to Bar
            Canvas.DrawText(temp.ToString(), font, Color.White, 150, y - 5);
            //Display the temperature
            Canvas.Flush();
        }

        // //
        // Helper - Change Thermostat //
        // //
        private static void ChangeTemp(int temp)
        {
            // Ensure we have a serial port
            if (serialPort != null)
            {
                // Encode to UTF8
                UTF8Encoding enc = new UTF8Encoding();
                // Encode Byte array
                string tempString = "Temperature = " + temp.ToString() + "\r\n";
                byte[] tempData = enc.GetBytes(tempString);
                // Write to Serial Port
                serialPort.Write(tempData, 0, tempData.Length);

                Debug.Print(tempString);
            }
        }
    }
}