Hi,
my hydra don’t work if i use both enc28 and misic module. can you help me? see my code.
using Gadgeteer;
using GTM = Gadgeteer.Modules;
namespace HydraWebRadio
{
public partial class Program : Gadgeteer.Program
{
// GTM.Module defintions
Gadgeteer.Modules.GHIElectronics.Display_T35 display;
Gadgeteer.Modules.GHIElectronics.SDCard sdCard;
Gadgeteer.Modules.GHIElectronics.Music music;
public static void Main()
{
//Important to initialize the Mainboard first
Mainboard = new GHIElectronics.Gadgeteer.FEZHydra();
Program program = new Program();
program.InitializeModules();
program.ProgramStarted();
program.Run(); // Starts Dispatcher
}
private void InitializeModules()
{
if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU != 3)
{
// Initialize GTM.Modules and event handlers here.
music = new GTM.GHIElectronics.Music(4);
sdCard = new GTM.GHIElectronics.SDCard(8);
display = new GTM.GHIElectronics.Display_T35(10, 11, 12, 13);
}
}
}
}
using System;
using System.Collections;
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;
using Microsoft.SPOT.Input;
using GHIElectronics.NETMF.Glide;
using GHIElectronics.NETMF.Glide.Display;
using GHIElectronics.NETMF.Glide.UI;
using HydraWebRadio.Utils;
using System.IO;
using Microsoft.SPOT.Net.NetworkInformation;
using Microsoft.SPOT.IO;
using GHIElectronics.OSH.NETMF.Hardware;
using System.Text;
namespace HydraWebRadio
{
public partial class Program
{
private static GHIElectronics.NETMF.Glide.Display.Window window;
private static GHIElectronics.NETMF.Glide.Display.Window curWindow;
private static DataGrid senderGrid;
private static TextBlock senderStatus;
private static ShoutcastClient radioClient;
private static DataGrid sdCardGrid;
private static ArrayList sdCardFilesList;
private static String ipAddress = "192.168.1.155";
private static String subnetMask = "255.255.255.0";
private static String gatewayAddress = "192.168.1.1";
private static String dnsAddresses = "192.168.1.1";
/// <summary>
/// Thread for RadiClient->PollForData
/// </summary>
private Thread DoListenRadio;
/// <summary>
/// Start time for radio
/// </summary>
private DateTime StartTime;
/// <summary>
/// Current time
/// </summary>
private DateTime CurrentTime;
/// <summary>
/// When displaying a message we don't want it to dissapear right away so
/// this var tells us when we can show another text
/// </summary>
private DateTime MessageTime = DateTime.Now;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
// Don't work in debugger
sdCard.SDCardMounted += new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
sdCard.SDCardUnmounted += new SDCard.SDCardUnmountedEventHandler(sdCard_SDCardUnmounted);
// Load application settings
loadApplicationSettings();
// Init network
InitializeNetwork();
// Init radio client
radioClient = new ShoutcastClient(music);
// Resize any loaded Window to the LCD's size.
Glide.FitToScreen = true;
window = GetStartWindow();
// Activate touch
GlideTouch.Initialize();
// Assign the Window to MainWindow; rendering it to the LCD.
Glide.MainWindow = window;
}
private void loadApplicationSettings()
{
Stream stream = null;
if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU != 3)
{
if (sdCard.IsCardMounted)
{
try
{
stream = new FileStream(@ "\SD\AppConfig.xml", FileMode.Open, FileAccess.Read);
}
catch (IOException e)
{
Debug.Print("File 'AppConfig.xml' not found. " + e.Message);
}
}
}
else
{
String text = Resources.GetString(Resources.StringResources.AppConfig);
byte[] byteArray = Encoding.UTF8.GetBytes(text);
stream = new MemoryStream(byteArray);
}
if (stream != null)
{
// Loads settings.
ConfigurationManager.Load(stream);
ipAddress = ConfigurationManager.GetAppSetting("IP");
subnetMask = ConfigurationManager.GetAppSetting("Mask");
gatewayAddress = ConfigurationManager.GetAppSetting("Getway");
dnsAddresses = ConfigurationManager.GetAppSetting("DNS");
stream.Close();
}
}
void sdCard_SDCardMounted(SDCard sender, GT.StorageDevice SDCard)
{
Debug.Print("SD Card mounted.");
sdCardFilesList = getFiles();
}
void sdCard_SDCardUnmounted(SDCard sender)
{
Debug.Print("SD Card unmounted.");
if (sdCardFilesList != null)
{
sdCardFilesList.Clear();
}
}
private static void InitializeNetwork()
{
Debug.Print("Initializing network...");
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
if (interfaces != null && interfaces.Length > 0)
{
NetworkInterface networkInterface = interfaces[0];
Boolean isDhcpWorked = false;
// dynamic IP address
if(networkInterface.IsDhcpEnabled == false) networkInterface.EnableDhcp();
// Wait for DHCP (on LWIP devices)
for(int i = 0; i < 5; i++)
{
networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];
if (networkInterface.IPAddress != "0.0.0.0")
{
isDhcpWorked = true;
break;
}
Debug.Print("Waiting for an IP Address...");
Thread.Sleep(1000);
}
if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU != 3)
{
if (isDhcpWorked == false)
{
// static IP address
networkInterface.EnableStaticIP(ipAddress, subnetMask, gatewayAddress);
networkInterface.EnableStaticDns(new[] { dnsAddresses });
}
}
Debug.Print("Network ready.");
Debug.Print(" IP Address: " + networkInterface.IPAddress);
Debug.Print(" Subnet Mask: " + networkInterface.SubnetMask);
Debug.Print(" Default Getway: " + networkInterface.GatewayAddress);
Debug.Print(" DNS Server: " + networkInterface.DnsAddresses[0]);
}
}
// Create start the windows.
private GHIElectronics.NETMF.Glide.Display.Window GetStartWindow()
{
// Load the Window XML string.
GHIElectronics.NETMF.Glide.Display.Window window = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.StartWindow));
// Find the back button within the window.
GHIElectronics.NETMF.Glide.UI.Image imgInternet = (GHIElectronics.NETMF.Glide.UI.Image) window.GetChildByName("internet");
imgInternet.Bitmap = Resources.GetBitmap(Resources.BitmapResources.earth);
// Attach a tap event handler.
imgInternet.TapEvent += new OnTap(GotoWeb_TapEvent);
GHIElectronics.NETMF.Glide.UI.Image imgSD = (GHIElectronics.NETMF.Glide.UI.Image)window.GetChildByName("sd");
imgSD.Bitmap = Resources.GetBitmap(Resources.BitmapResources.disk_blue);
// Attach a tap event handler.
imgSD.TapEvent += new OnTap(GotoSD_TapEvent);
GHIElectronics.NETMF.Glide.UI.Image imgSettings = (GHIElectronics.NETMF.Glide.UI.Image)window.GetChildByName("settings");
imgSettings.Bitmap = Resources.GetBitmap(Resources.BitmapResources.gear);
// Attach a tap event handler.
imgSettings.TapEvent += new OnTap(GotoSettings_TapEvent);
return window;
}
private GHIElectronics.NETMF.Glide.Display.Window GetWebWindow()
{
GHIElectronics.NETMF.Glide.Display.Window window = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.WebWindow));
// Setup the dataGrid reference.
senderGrid = (DataGrid)window.GetChildByName("list");
// Create our three columns.
senderGrid.AddColumn(new DataGridColumn("name", 100));
// Add data
addSenders(senderGrid);
// Add the data grid to the window before rendering it.
window.AddChild(senderGrid);
if (senderGrid.NumItems > 0) senderGrid.SelectedIndex = 9;
senderGrid.Render();
GHIElectronics.NETMF.Glide.UI.Image imgplay = (GHIElectronics.NETMF.Glide.UI.Image)window.GetChildByName("play");
imgplay.Bitmap = Resources.GetBitmap(Resources.BitmapResources.media_play);
imgplay.TapEvent += new OnTap(SenderPlay_TapEvent);
GHIElectronics.NETMF.Glide.UI.Image imgstop = (GHIElectronics.NETMF.Glide.UI.Image)window.GetChildByName("stop");
imgstop.Bitmap = Resources.GetBitmap(Resources.BitmapResources.media_stop);
imgstop.TapEvent += new OnTap(SenderStop_TapEvent);
GHIElectronics.NETMF.Glide.UI.Image imgup = (GHIElectronics.NETMF.Glide.UI.Image)window.GetChildByName("up");
imgup.Bitmap = Resources.GetBitmap(Resources.BitmapResources.nav_up_blue);
imgup.TapEvent += new OnTap(SenderUp_TapEvent);
GHIElectronics.NETMF.Glide.UI.Image imgdown = (GHIElectronics.NETMF.Glide.UI.Image)window.GetChildByName("down");
imgdown.Bitmap = Resources.GetBitmap(Resources.BitmapResources.nav_down_blue);
imgdown.TapEvent += new OnTap(SenderDown_TapEvent);
GHIElectronics.NETMF.Glide.UI.Image imgHome = (GHIElectronics.NETMF.Glide.UI.Image)window.GetChildByName("home");
imgHome.Bitmap = Resources.GetBitmap(Resources.BitmapResources.home);
imgHome.TapEvent += new OnTap(GotoHome_TapEvent);
senderStatus = (TextBlock)window.GetChildByName("status");
ShoutcastClient.RadioStation[] stations = radioClient.getStations();
ShoutcastClient.RadioStation station = stations[senderGrid.SelectedIndex];
radioClient.Play(station);
// Add OnMP3InfoUpdated Event Handler
radioClient.OnMP3InfoUpdated += new ShoutcastClient.Mp3InfoUpdatedEvent(RadioClient_OnMP3InfoUpdated);
radioClient.OnRadioConnectionLost += new ShoutcastClient.RadioConnectionLostEvent(RadioClient_OnRadioConnectionLost);
// Check if error happened while init Radio Client
if (radioClient.ErrorString == "")
{
StartTime = DateTime.MinValue;
// Make thread for polling stream data
DoListenRadio = new Thread(new ThreadStart(radioClient.PollForData));
DoListenRadio.Start();
}
return window;
}
private GHIElectronics.NETMF.Glide.Display.Window GetSDCardWindow()
{
GHIElectronics.NETMF.Glide.Display.Window window = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.SDCardWindow));
// Setup the dataGrid reference.
sdCardGrid = (DataGrid)window.GetChildByName("list");
// Create our three columns.
sdCardGrid.AddColumn(new DataGridColumn("name", 100));
// Add data
addFiles(sdCardFilesList, sdCardGrid);
// Add the data grid to the window before rendering it.
window.AddChild(sdCardGrid);
if (sdCardGrid.NumItems > 0) sdCardGrid.SelectedIndex = 0;
sdCardGrid.Render();
GHIElectronics.NETMF.Glide.UI.Image imgplay = (GHIElectronics.NETMF.Glide.UI.Image)window.GetChildByName("play");
imgplay.Bitmap = Resources.GetBitmap(Resources.BitmapResources.media_play);
imgplay.TapEvent += new OnTap(SDCardPlay_TapEvent);
GHIElectronics.NETMF.Glide.UI.Image imgstop = (GHIElectronics.NETMF.Glide.UI.Image)window.GetChildByName("stop");
imgstop.Bitmap = Resources.GetBitmap(Resources.BitmapResources.media_stop);
imgstop.TapEvent += new OnTap(SDCardStop_TapEvent);
GHIElectronics.NETMF.Glide.UI.Image imgup = (GHIElectronics.NETMF.Glide.UI.Image)window.GetChildByName("up");
imgup.Bitmap = Resources.GetBitmap(Resources.BitmapResources.nav_up_blue);
imgup.TapEvent += new OnTap(SDCardUp_TapEvent);
GHIElectronics.NETMF.Glide.UI.Image imgdown = (GHIElectronics.NETMF.Glide.UI.Image)window.GetChildByName("down");
imgdown.Bitmap = Resources.GetBitmap(Resources.BitmapResources.nav_down_blue);
imgdown.TapEvent += new OnTap(SDCardDown_TapEvent);
GHIElectronics.NETMF.Glide.UI.Image imgHome = (GHIElectronics.NETMF.Glide.UI.Image)window.GetChildByName("home");
imgHome.Bitmap = Resources.GetBitmap(Resources.BitmapResources.home);
imgHome.TapEvent += new OnTap(GotoHome_TapEvent);
senderStatus = (TextBlock)window.GetChildByName("status");
return window;
}
private ArrayList getFiles()
{
ArrayList files = MyDirectory.getFiles(@ "\SD\MP3");
return files;
}
private static void addFiles(ArrayList files, DataGrid dataGrid)
{
if (files == null) return;
for (int i = 0; i < files.Count; i++)
{
// DataGridItems must contain an object array whose length matches the number of columns.
String title = new FileInfo((String) files[i]).Name;
DataGridItem Item = new DataGridItem(new Object[1] { title });
dataGrid.AddItem(Item);
}
}
private static void addSenders(DataGrid dataGrid)
{
ShoutcastClient.RadioStation[] stations = radioClient.getStations();
// Add Radio Stations to List
for (int i = 0; i < stations.Length; i++)
{
// DataGridItems must contain an object array whose length matches the number of columns.
String title = ((int)(i + 1)).ToString() + ". " + stations[i].Title;
DataGridItem Item = new DataGridItem(new Object[1] {title});
dataGrid.AddItem(Item);
}
}
// Handles the next button tap event.
void GotoWeb_TapEvent(object sender)
{
GHIElectronics.NETMF.Glide.Display.Window webWindow = GetWebWindow();
Tween.SlideWindow(window, webWindow, Direction.Up);
curWindow = webWindow;
}
private void GotoSD_TapEvent(object sender)
{
GHIElectronics.NETMF.Glide.Display.Window sdWindow = GetSDCardWindow();
Tween.SlideWindow(window, sdWindow, Direction.Up);
curWindow = sdWindow;
}
private void GotoSettings_TapEvent(object sender)
{
}
private void GotoHome_TapEvent(object sender)
{
Tween.SlideWindow(curWindow, window, Direction.Up);
}
private void SenderDown_TapEvent(object sender)
{
if (senderGrid.SelectedIndex < senderGrid.NumItems - 1)
senderGrid.SelectedIndex++;
}
private void SenderUp_TapEvent(object sender)
{
if (senderGrid.SelectedIndex > 0)
senderGrid.SelectedIndex--;
}
private void SenderPlay_TapEvent(object sender)
{
// Get the data from the row we tapped.
object[] data = senderGrid.GetRowData(senderGrid.SelectedIndex);
if (data != null)
{
ShoutcastClient.RadioStation[] stations = radioClient.getStations();
ShoutcastClient.RadioStation station = stations[senderGrid.SelectedIndex];
senderStatus.Text = "Connect to " + station.Title + " ...";
// upadate elemenet
GHIElectronics.NETMF.Glide.UI.Image element = (GHIElectronics.NETMF.Glide.UI.Image)sender;
GHIElectronics.NETMF.Glide.Display.Window window = (GHIElectronics.NETMF.Glide.Display.Window) element.Parent;
window.FillRect(senderStatus.Rect);
senderStatus.Invalidate();
}
}
private void SenderStop_TapEvent(object sender)
{
}
private void SDCardDown_TapEvent(object sender)
{
if (sdCardGrid.SelectedIndex < sdCardGrid.NumItems - 1)
sdCardGrid.SelectedIndex++;
if (music.IsBusy)
{
music.StopPlaying();
}
}
private void SDCardUp_TapEvent(object sender)
{
if (sdCardGrid.SelectedIndex > 0)
sdCardGrid.SelectedIndex--;
if (music.IsBusy)
{
music.StopPlaying();
}
}
private void SDCardPlay_TapEvent(object sender)
{
if (sdCardGrid == null) return;
if (music == null) return;
int index = sdCardGrid.SelectedIndex;
if (index >= 0)
{
String file = (String)sdCardFilesList[index];
FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read);
music.Play(stream);
}
}
private void SDCardStop_TapEvent(object sender)
{
if(music.IsBusy)
{
music.StopPlaying();
}
}
/// <summary>
/// Event handler for Radio Client - OnRadioConnectionLost closes ListenRadioDialog
/// </summary>
void RadioClient_OnRadioConnectionLost()
{
//this.Dispatcher.BeginInvoke(new CloseListenRadioDialogDelegate(CloseListenRadioDialog), new object[] { radioClient });
}
/// <summary>
/// Event Handler for Radio Client - OnMP3InfoUpdated Event. Displays stream information
/// </summary>
private void RadioClient_OnMP3InfoUpdated()
{
if (radioClient.status == ShoutcastClient.StatusOptions.Message)
{
String StatusText = radioClient.Message;
String SubText1 = radioClient.SubMessage1;
String SubText2 = radioClient.SubMessage2;
// Message should disappear in 1 second
MessageTime = DateTime.Now + TimeSpan.FromTicks(1 * TimeSpan.TicksPerSecond);
// Invoke DoUpdateWindowText for updating text
//this.Dispatcher.BeginInvoke(new UpdateDialogText(DoUpdateWindowText), new String[] { StatusText, SubText1, SubText2 });
}
// This is for messages. Stop displaying other text until message should dissapear
if (DateTime.Now < MessageTime)
{
return;
}
// Update text while connecting
else if (radioClient.status == ShoutcastClient.StatusOptions.Connecting)
{
String StatusText = "Connecting";
String SubText1 = "";
String SubText2 = "";
// Invoke DoUpdateWindowText for updating text
//Dispatcher.BeginInvoke(new UpdateDialogText(DoUpdateWindowText), new String[] { StatusText, SubText1, SubText2 });
}
// Update text while buffering
else if (radioClient.status == ShoutcastClient.StatusOptions.Buffering)
{
String StatusText = "Buffering";
String SubText1 = radioClient.inBuffer.ToString() + "Bytes";
String SubText2 = "";
// Invoke DoUpdateWindowText for updating text
//this.Dispatcher.BeginInvoke(new UpdateDialogText(DoUpdateWindowText), new String[] { StatusText, SubText1, SubText2 });
}
// Update text while playing
else if (radioClient.status == ShoutcastClient.StatusOptions.Playing)
{
// Store Starting Time
if (StartTime == DateTime.MinValue) { StartTime = DateTime.Now; }
// Update current time
CurrentTime = DateTime.Now;
// Calculate playing time
TimeSpan PlayingTime = CurrentTime - StartTime;
String StatusText = Str_Int_Func.IntToString(PlayingTime.Hours, 2) + ":" + Str_Int_Func.IntToString(PlayingTime.Minutes, 2) + ":" + Str_Int_Func.IntToString(PlayingTime.Seconds, 2);
String SubText1 = radioClient.Mp3Info.getBitrate().ToString() + "kbps, " + radioClient.Mp3Info.getFrequency() + "Hz";
String SubText2;
if (radioClient.ICYStreamName != "")
SubText2 = radioClient.ICYStreamName;
else
SubText2 = radioClient.Mp3Info.getMode();
// Invoke DoUpdateWindowText for updating text
//this.Dispatcher.BeginInvoke( new UpdateDialogText(DoUpdateWindowText), new String[] { StatusText, SubText1, SubText2 });
}
// Update text for error
else if (radioClient.status == ShoutcastClient.StatusOptions.Error)
{
String StatusText = "Error!";
String SubText1 = "";
String SubText2 = radioClient.ErrorString;
// Invoke DoUpdateWindowText for updating text
//this.Dispatcher.BeginInvoke(new UpdateDialogText(DoUpdateWindowText), new String[] { StatusText, SubText1, SubText2 });
}
}
}
}