Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Finishing touches for AzureIoTHub sample
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgedevs committed Jan 8, 2024
1 parent fd10758 commit 966427b
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 100 deletions.
276 changes: 198 additions & 78 deletions Source/Azure/AzureIoTHub/Controllers/DisplayController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,120 +2,240 @@
using Meadow.Foundation.Graphics;
using Meadow.Foundation.Graphics.MicroLayout;
using Meadow.Units;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace MeadowAzureIoTHub.Controllers
{
public class DisplayController
{
static Color backgroundColor = Color.FromHex("#23ABE3");
static Color foregroundColor = Color.Black;
Color backgroundColor = Color.FromHex("#23ABE3");

CancellationTokenSource token;
Font8x12 font8x12 = new Font8x12();
Font12x20 font12X20 = new Font12x20();

Image imgConnecting = Image.LoadFromResource("MeadowAzureIoTHub.Resources.img_wifi_connected.bmp");
Image imgConnected = Image.LoadFromResource("MeadowAzureIoTHub.Resources.img_wifi_connecting.bmp");
Image imgRefreshing = Image.LoadFromResource("MeadowAzureIoTHub.Resources.img_refreshing.bmp");
Image imgRefreshed = Image.LoadFromResource("MeadowAzureIoTHub.Resources.img_refreshed.bmp");
CancellationTokenSource token;

MicroGraphics graphics;
Image imgWifi = Image.LoadFromResource("MeadowAzureIoTHub.Resources.img_wifi.bmp");
Image imgWifiFade = Image.LoadFromResource("MeadowAzureIoTHub.Resources.img_wifi_fade.bmp");
Image imgSync = Image.LoadFromResource("MeadowAzureIoTHub.Resources.img_sync.bmp");
Image imgSyncFade = Image.LoadFromResource("MeadowAzureIoTHub.Resources.img_sync_fade.bmp");

DisplayScreen displayScreen;

AbsoluteLayout SplashLayout;

AbsoluteLayout DataLayout;

public DisplayController(IGraphicsDisplay display)
{
graphics = new MicroGraphics(display)
{
CurrentFont = new Font8x12(),
Stroke = 3,
Rotation = RotationType._90Degrees
};
AbsoluteLayout DataLayout;

displayScreen = new DisplayScreen(display);
{
backgroundColor = backgroundColor;
}
Picture Wifi;

graphics.Clear(true);
}
Picture Sync;

Label Title;

Label Temperature;

Label Humidity;

protected void DrawBackground()
public DisplayController(IGraphicsDisplay display)
{
var logo = Image.LoadFromResource("MeadowAzureIoTHub.Resources.img_meadow.bmp");
displayScreen = new DisplayScreen(display, RotationType._90Degrees)
{
BackgroundColor = backgroundColor
};

graphics.Clear(backgroundColor);
LoadSplashLayout();

graphics.DrawImage(
x: graphics.Width / 2 - logo.Width / 2,
y: 63,
image: logo);
displayScreen.Controls.Add(SplashLayout);

graphics.DrawText(graphics.Width / 2, 160, "Azure IoT Hub", foregroundColor, ScaleFactor.X2, HorizontalAlignment.Center);
}
LoadDataLayout();

public void ShowSplashScreen()
displayScreen.Controls.Add(DataLayout);
}

private void LoadSplashLayout()
{
DrawBackground();

graphics.Show();
}
SplashLayout = new AbsoluteLayout(displayScreen, 0, 0, displayScreen.Width, displayScreen.Height)
{
IsVisible = false
};

public async Task ShowConnectingAnimation()
var logo = Image.LoadFromResource("MeadowAzureIoTHub.Resources.img_meadow.bmp");
var displayImage = new Picture(
55,
60,
logo.Width,
logo.Height,
logo)
{
BackColor = backgroundColor,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
SplashLayout.Controls.Add(displayImage);

SplashLayout.Controls.Add(new Label(
0,
160,
displayScreen.Width,
font8x12.Height)
{
Text = $"Azure IoT Hub",
TextColor = Color.Black,
Font = font8x12,
ScaleFactor = ScaleFactor.X2,
HorizontalAlignment = HorizontalAlignment.Center,
});
}

public void LoadDataLayout()
{
token = new CancellationTokenSource();

bool alternateImg = false;
while (!token.IsCancellationRequested)
DataLayout = new AbsoluteLayout(displayScreen, 0, 0, displayScreen.Width, displayScreen.Height)
{
alternateImg = !alternateImg;
IsVisible = false
};

Sync = new Picture(
15,
15,
imgSyncFade.Width,
imgSyncFade.Height,
imgSyncFade);
DataLayout.Controls.Add(Sync);

Title = new Label(
60,
20,
120,
26)
{
Text = "AMQP",
TextColor = Color.Black,
Font = font8x12,
ScaleFactor = ScaleFactor.X2,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
DataLayout.Controls.Add(Title);

Wifi = new Picture(
195,
15,
imgWifiFade.Width,
imgWifiFade.Height,
imgWifiFade);
DataLayout.Controls.Add(Wifi);

DataLayout.Controls.Add(new Box(
20,
57,
200,
76)
{
ForeColor = Color.Black,
IsFilled = false
});

DataLayout.Controls.Add(new Label(
24,
65,
192,
font8x12.Height * 2)
{
Text = "Temperature",
TextColor = Color.Black,
Font = font8x12,
ScaleFactor = ScaleFactor.X2,
HorizontalAlignment = HorizontalAlignment.Center
});

Temperature = new Label(
24,
93,
192,
font12X20.Height * 2)
{
Text = "--°C",
TextColor = Color.Black,
Font = font12X20,
ScaleFactor = ScaleFactor.X2,
HorizontalAlignment = HorizontalAlignment.Center
};
DataLayout.Controls.Add(Temperature);

DataLayout.Controls.Add(new Box(
20,
144,
200,
76)
{
ForeColor = Color.Black,
IsFilled = false
});

DataLayout.Controls.Add(new Label(
24,
152,
192,
font8x12.Height * 2)
{
Text = "Humidity",
TextColor = Color.Black,
Font = font8x12,
ScaleFactor = ScaleFactor.X2,
HorizontalAlignment = HorizontalAlignment.Center
});

Humidity = new Label(
24,
180,
192,
font12X20.Height * 2)
{
Text = "--%",
TextColor = Color.Black,
Font = font12X20,
ScaleFactor = ScaleFactor.X2,
HorizontalAlignment = HorizontalAlignment.Center
};
DataLayout.Controls.Add(Humidity);
}

graphics.DrawImage(204, 6, alternateImg ? imgConnecting : imgConnected);
graphics.Show();
public void ShowSplashScreen()
{
DataLayout.IsVisible = false;
SplashLayout.IsVisible = true;
}

public void ShowDataScreen()
{
SplashLayout.IsVisible = false;
DataLayout.IsVisible = true;
}

await Task.Delay(500);
}
public void UpdateTitle(string title)
{
Title.Text = title;
}

public void ShowConnected()
public void UpdateWiFiStatus(bool isConnected)
{
token.Cancel();
graphics.DrawImage(204, 6, imgConnected);

graphics.DrawImage(6, 6, imgRefreshed);

graphics.DrawRectangle(0, 32, 240, 208, backgroundColor, true);

graphics.DrawRoundedRectangle(19, 47, 200, 80, 15, foregroundColor);
graphics.DrawText(120, 56, "Temperature", foregroundColor, ScaleFactor.X2, HorizontalAlignment.Center);

graphics.DrawRoundedRectangle(19, 141, 200, 80, 15, foregroundColor);
graphics.DrawText(120, 149, "Humidity", foregroundColor, ScaleFactor.X2, HorizontalAlignment.Center);

graphics.Show();
Wifi.Image = isConnected
? imgWifi
: imgWifiFade;
}

public async Task StartSyncCompletedAnimation((Temperature? Temperature, RelativeHumidity? Humidity) reading)
public void UpdateSyncStatus(bool isSyncing)
{
graphics.DrawImage(6, 6, imgRefreshing);
graphics.Show();
await Task.Delay(TimeSpan.FromSeconds(1));

graphics.CurrentFont = new Font12x20();

graphics.DrawRectangle(24, 85, 190, 40, backgroundColor, true);
graphics.DrawText(120, 85, $"{reading.Temperature.Value.Celsius:N1}°C", foregroundColor, ScaleFactor.X2, HorizontalAlignment.Center);
Sync.Image = isSyncing
? imgSync
: imgSyncFade;
}

graphics.DrawRectangle(24, 178, 190, 40, backgroundColor, true);
graphics.DrawText(120, 178, $"{reading.Humidity.Value.Percent:N2}%", foregroundColor, ScaleFactor.X2, HorizontalAlignment.Center);
public void UpdateReadings((Temperature? Temperature, RelativeHumidity? Humidity) reading)
{
Temperature.Text = $"{reading.Temperature.Value.Celsius:N1}°C";

graphics.DrawImage(6, 6, imgRefreshed);
graphics.Show();
Humidity.Text = $"{reading.Humidity.Value.Percent:N2}%";
}
}
}
3 changes: 1 addition & 2 deletions Source/Azure/AzureIoTHub/Controllers/IoTHubAmqpController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
using Amqp.Framing;
using Meadow;
using Meadow.Units;
using MeadowAzureIoTHub.Controllers;
using System;
using System.Text;
using System.Threading.Tasks;

namespace MeadowAzureIoTHub.Azure
namespace MeadowAzureIoTHub.Controllers
{
public class IoTHubAmqpController : IIoTHubController
{
Expand Down
Loading

0 comments on commit 966427b

Please sign in to comment.