/* Copyright 2010 GHI Electronics LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using GHIElectronics.NETMF.FEZ; using GHIElectronics.NETMF.USBClient; using System.Collections; using System.Text; using Fez.IO; using System.Reflection; using System.IO.Ports; namespace CdcPlusDebugger { public class Program { static FezTermHost term; static AutoResetEvent are = new AutoResetEvent(false); static FEZ_Components.Piezo speaker = new FEZ_Components.Piezo(FEZ_Pin.PWM.Di5); static readonly byte newLine = 255; static FEZ_Components.Button myButton; public static void Main() { myButton = new FEZ_Components.Button(FEZ_Pin.Interrupt.Di4); myButton.ButtonPressEvent +=new FEZ_Components.Button.ButtonPressEventHandler(myButton_ButtonPressEvent); term = new FezTermHost(newLine); term.MessageReceived += new MessageFunc(term_MessageReceived); term.Stopped += new Action(term_Stopped); term.Start(); // Stopped event will set are. are.WaitOne(); Debug.Print("Main exited."); } static void myButton_ButtonPressEvent(FEZ_Pin.Interrupt pin, FEZ_Components.Button.ButtonState state) { speaker.Play(5000, 300); } static void term_Stopped() { are.Set(); } static void term_MessageReceived(string value) { if (value == null) return; string result = HandleCommands(value); if (result == "bye") { term.WriteMessage("bye"); term.Stop(); return; } else { term.WriteMessage(result); } } static string HandleCommands(string cmd) { string lcmd = cmd.ToLower(); switch (lcmd) { case "exit": return "bye"; case "v": case "ver": return Assembly.GetExecutingAssembly().FullName; case "time": return DateTime.Now.ToString(); case "?": case "h": case "help": return GetHelp(); case "info": return GetSystemInfo(); case "beephigh": speaker.Play(8000, 500); return "OK"; case "beeplow": case "beep": speaker.Play(3000, 500); return "OK"; case "btnleft": speaker.Play(4000, 500); return "OK"; case "btnright": speaker.Play(6000, 500); return "OK"; case "ping": return "reply from FezTermHost."; case "dir": case "ls": return GetDir(); case "xbee": XBeeSend(); return "OK"; default: return "'" + cmd + "' is not recognized as in internal or external command."; } } // Just a simulation listing. static string GetDir() { string list = @" Volume in drive C is OS Volume Serial Number is 3895-9268 Directory of C:\Program Files (x86)\GHI Electronics\GHI NETMF v4.1 SDK 10/15/2010 10:33 PM . 10/15/2010 10:33 PM .. 10/15/2010 10:33 PM Assemblies 10/15/2010 10:33 PM CANxtra 10/15/2010 10:33 PM ChipworkX 10/15/2010 10:33 PM Embedded Master 10/15/2010 10:33 PM EMX 10/15/2010 10:33 PM FEZ 10/15/2010 10:33 PM Installation 10/15/2010 11:10 AM 71,294 Release Notes.rtf 10/15/2010 10:33 PM USB Drivers 10/15/2010 10:33 PM USBizi 1 File(s) 71,294 bytes 11 Dir(s) 373,516,726,272 bytes free"; return list; } static string GetSystemInfo() { string si = "Is Big Endian: " + Microsoft.SPOT.Hardware.SystemInfo.IsBigEndian; si += "\r\nOEM String: " + Microsoft.SPOT.Hardware.SystemInfo.OEMString; si += "\r\nMF Version: " + Microsoft.SPOT.Hardware.SystemInfo.Version; si += "\r\nBattery voltage: " + Microsoft.SPOT.Hardware.Battery.ReadVoltage(); return si; } static string GetHelp() { string h = @" ? - Help info - System info v - Version time - Get time exit - Quit session. Host returns bye. dir - Directory listing ls - Directory listing ping - Ping host beep - beep "; return h; } static SerialPort xbee; static void XBeeSend() { if (xbee == null) { xbee = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One); xbee.Open(); } // create "Hello!" string as bytes byte[] helloBytes = Encoding.UTF8.GetBytes("Hello!"); xbee.Write(helloBytes, 0, helloBytes.Length); } } }