C# sample code for SPL engine - Simple SPL script engine programming


1. Open C# project file
  1. Double-click "Samples" directory.

 

  1. Double-click "SPLMFEngine.Sample1" directory.



  2. Double-click "SPLMFEngine.Sample1.sln" file.



  3. Open Program.cs file.




2. C# code to use SPL script engine
  1. Program.cs

      
    
    using System;
    
    using Microsoft.SPOT;
    using Microsoft.SPOT.Input;
    using Microsoft.SPOT.Presentation;
    using Microsoft.SPOT.Presentation.Controls;
    
    using SPL.Script.Engine;
    
    namespace SPLMFEngine.Sample1
    {
        public class Program : Microsoft.SPOT.Application
        {
            public static void Main()
            {
                Program myApplication = new Program();
    
                Window mainWindow = myApplication.CreateWindow();
    
                GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);
    
                myApplication.Run(mainWindow);
            }
    
            private Window mainWindow;
    
            //##########################  Sample Code  #################################
            private ConsoleWindow _consoleWindow;
            private SPLScriptEngine _engine = null;
            //##########################  Sample Code  #################################
    
            public Window CreateWindow()
            {
                mainWindow = new Window();
                mainWindow.Height = SystemMetrics.ScreenHeight;
                mainWindow.Width = SystemMetrics.ScreenWidth;
    
    
                //##########################  Sample Code  #################################
                Font font = Resources.GetFont(Resources.FontResources.small);
                _consoleWindow = new ConsoleWindow(font);
    
                mainWindow.Child = _consoleWindow;
                //##########################  Sample Code  #################################
    
    
                mainWindow.AddHandler(Buttons.ButtonUpEvent, new ButtonEventHandler(OnButtonUp), false);
    
                mainWindow.Visibility = Visibility.Visible;
    
                Buttons.Focus(mainWindow);
    
    
                //##########################  Sample Code  #################################
                _engine = new SPLScriptEngine(mainWindow, _consoleWindow, Dispatcher.CurrentDispatcher);
                _engine.AddFont("small", Resources.GetFont(Resources.FontResources.small));
    
                _consoleWindow.WriteLine("Please push any button!.");
                _consoleWindow.WriteLine("Select button will clear screen.");
                //##########################  Sample Code  #################################
    
    
                return mainWindow;
            }
    
            private void OnButtonUp(object sender, ButtonEventArgs e)
            {
                Debug.Print(e.Button.ToString());
    
                //##########################  Sample Code  #################################
                if (e.Button == Microsoft.SPOT.Hardware.Button.VK_SELECT)
                {
                    _consoleWindow.ClearConsole();
                }
                else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_LEFT)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("a = 10");
                    sb.Append("b = 20");
                    sb.Append("c = a + b");
                    sb.Append("print \"c -> \" + c");
    
                    _engine.ExecuteScriptText(sb.ToText());
                }
                else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_RIGHT)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("procedure Sum(a, b)");
                    sb.Append("  return a+b");
                    sb.Append("end");
                    sb.Append("mysum = Sum(33, 55)");
                    sb.Append("print \"mysum = \" + mysum");
    
                    _engine.ExecuteScriptText(sb.ToText());
                }
                else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_UP)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("a = 0");
                    sb.Append("print \"start for loop\"");
                    sb.Append("for (i=1; i <= 5; i++)");
                    sb.Append("{");
                    sb.Append("  a = a + i");
                    sb.Append("  print \"a -> \" + a");                
                    sb.Append("}");
                    sb.Append("print \"end for loop\"");
    
                    _engine.ExecuteScriptText(sb.ToText());
                }
                else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_DOWN)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("a = 0");
                    sb.Append("s = 0");
                    sb.Append("print \"start while loop\"");
                    sb.Append("while (a < 5)");
                    sb.Append("{");
                    sb.Append("  a++");
                    sb.Append("  s = s + a");
                    sb.Append("  print \"s -> \" + s");                
                    sb.Append("}");
                    sb.Append("print \"end of while loop\"");
    
                    _engine.ExecuteScriptText(sb.ToText());
                }
                //##########################  Sample Code  #################################
            }
        }
    }
    
    
      



3. Execute sample code
  1. Press "F5" key to run sample code.