Testing the components

Ok. now it time to make sure all the components work correctly, and that we can control the components correctly. As I am already know C#, I will be using it for this the project.

Unfortunately, all the example code from Adafruit is in Python - It's not a difficult language to read so I used the Python code, along with the data sheets for the different chips to make a start.

I first installed .NET 5 on the Raspberry Pi using this guide. - The guide boils down to running this script

wget -O - https://raw.githubusercontent.com/pjgpetecodes/dotnet5pi/master/install.sh | sudo bash

Once installed you can run dotnet --info and get

root@devpi:/Apps/AlarmClock# dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.103
Commit: 72dec52dbd

Runtime Environment:
OS Name: raspbian
OS Version: 10
OS Platform: Linux
RID: linux-arm
Base Path: /opt/dotnet/sdk/5.0.103/

Host (useful for support):
Version: 5.0.3
Commit: c636bbdc8a

.NET SDKs installed:
5.0.103 [/opt/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 5.0.3 [/opt/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 5.0.3 [/opt/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download
root@devpi:/Apps/AlarmClock#

Ok. Now for the real code, and I will start with the Led Ring on the ReSpeaker 4Mic board. Reading the specs, the Led Ring is connected on the SPI interface, uses an apa102 chip, and GPIO5 needs to be set high to enable it. There is a Microsoft Library called IOT.Device.Bindings. This library has lots of IOT device bindings for APA102, I2C devices, etc). 

// Connect to LEDRing over SPI
_spiDevice = SpiDevice.Create(new SpiConnectionSettings(0, 1)
{
    ClockFrequency = 20_000_000,
    DataFlow = DataFlow.MsbFirst,
    Mode = SpiMode.Mode3 // ensure data is ready at clock rising edge
});
apa102Device = new Iot.Device.Apa102.Apa102(_spiDevice, LedCount);
gpio.OpenPin(5, PinMode.Output);
gpio.Write(5,PinValue.High);

Once we have the apa102Device instance, we can set the LED ARGB values in the apa102Device.Pixel array and then call apa102Device.Flush() to send the data to the physical LED's. The rest of the code is concerned with filling the Pixel array with values and sending the data at the correct time. You also have to make sure you set the gpio pin 5 to Low before program termination. This can be done in the driver classes Dispose method.

You can see this code here

(C) P J Tewkesbury 2024 - 2023.11.23.1 - All Rights Reserved - Forgot Password
Powered by .NET 7.0.14 and running on serverpi with Unix 6.1.21.8 - Deployed from commit cb263ce4