Softata - Adding a new display: 6. Run the Display
softata rpipico firmata arduino csharp bme280 grove softatadisplay
Implement the SetUp code and some of the Display methods.
_Now to get the code to do a few things!
The object of this exercise is to provide a template for adding devices, specifically for adding displays to Softata.
The previous post covered testing the display code which had implemnted just th shell of the functionality… That is it works but does nothing. This post covers implementing some of the display’s functionality
1. Links
- The repository latest version
-
The version before the Bargraph was added that is modified in this discussion.
-
A comparison of the repository before and after this extension is here.
- Open the bargraph.ino sketch as per a previous post. We will use copy and use some of the code with minimal modifications.
**
2. Setup
- In VS Code open the src folder
- Open
grove_bargraph.cpp
- Add the following (in a global manner just below the #includes)
int dataPin = 16; // Pin connected to DS of 74HC595(Pin14)
int latchPin = 20; // Pin connected to ST_CP of 74HC595(Pin12)
int clockPin = 21; // Pin connected to SH_CP of 74HC595(Pin11)
- Include the following in
Custom_Bargraph::Setup()
before the return statement:pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT);
- Connect the display as above
- Do another test run as per previous post. It should just run and exit as previous. Nothing to see yet.
3. Write
- Add the following to the global space at the top of
grove_bargraph.cpp
(its a direct copy form bargraph.ino) :
void writeTo595(BitOrder order, byte _data ) {
// Output low level to latchPin
digitalWrite(latchPin, LOW);
// Send serial data to 74HC595
shiftOut(dataPin, clockPin, order, _data);
// Output high level to latchPin, and 74HC595 will update the data to the parallel output port.
digitalWrite(latchPin, HIGH);
}
- Then in
Custom_Bargraph::WriteString(String msg)
insert:
int num = msg.toInt();
writeTo595(LSBFIRST, num);
For simplicity for now, assume the Console app does send a valid string.
- While we are about it, include the following code for
Custom_Bargraph::Clear()
:
writeTo595(LSBFIRST, 0);
- Build and run the Pico app, resolving any build issues.
4. Console App Test
- In program.cs locate
case CommandType.Displays:
- Find
if (displayLinkedListIndex < 0)
- Then
switch (displayDevice)
- Collapse the 3 display’s case here. It should look something like:
switch (displayDevice)
{
case DisplayDevice.NEOPIXEL:[]
case DisplayDevice.LCD1602:[]
case DisplayDevice.OLED096[]
}
- Before the closing brace insert the BARGRAPH case:
case DisplayDevice.BARGRAPH:
break;
- Embellish this case with
Console.WriteLine($"Instantiated {display} linked at {displayLinkedListIndex}");
SoftataLib.Display.Clear(displayLinkedListIndex);
Thread.Sleep(1000);
SoftataLib.Display.WriteString(displayLinkedListIndex, "0x01");
Thread.Sleep(5000);
SoftataLib.Display.Clear(displayLinkedListIndex);
Thread.Sleep(1000);
-
Rerun (just) the Console app and one LED segment should display for 5 sec.
-
Now test all segments with:
case DisplayDevice.BARGRAPH:
Console.WriteLine($"Instantiated {display} linked at {displayLinkedListIndex}");
SoftataLib.Display.Clear(displayLinkedListIndex);
Thread.Sleep(1000);
SoftataLib.Display.WriteString(displayLinkedListIndex, "1");
Thread.Sleep(1000);
SoftataLib.Display.WriteString(displayLinkedListIndex, "2");
Thread.Sleep(1000);
SoftataLib.Display.WriteString(displayLinkedListIndex, "4");
Thread.Sleep(1000);
SoftataLib.Display.WriteString(displayLinkedListIndex, "8");
Thread.Sleep(1000);
SoftataLib.Display.WriteString(displayLinkedListIndex, "16");
Thread.Sleep(1000);
SoftataLib.Display.WriteString(displayLinkedListIndex, "32");
Thread.Sleep(1000);
SoftataLib.Display.WriteString(displayLinkedListIndex, "64");
Thread.Sleep(1000);
SoftataLib.Display.WriteString(displayLinkedListIndex, "128");
Thread.Sleep(1000);
SoftataLib.Display.Clear(displayLinkedListIndex);
Thread.Sleep(1000);
break;
All good again! :)
5. Custom Setup
This is so any Pico pins can be assigned. Modify and embellish the SetUp methods as follows.
bool Custom_Bargraph::Setup()
{
byte settings[3];
settings[0] = 16;
settings[1] = 20;
settings[2] = 21;
return Setup(settings,3);
}
bool Custom_Bargraph::Setup(byte * settings, byte numSettings)
{
if (numSettings >2)
{
dataPin = (int)settings[0];
latchPin = (int)settings[1];
clockPin = (int)settings[2];
}
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
Serial.begin();
return true;
}
You can now send an array of 3 bytes to specify the Pico pins for the latch,clock and data pins in that order.
- In Program.cs, seraching up, find the line:
// Only do non-default setup for Neopixel
- Collapse the line below that:
if (displayDevice == DisplayDevice.NEOPIXEL)
``
- It should look like:
```cs
if (displayDevice == DisplayDevice.NEOPIXEL)[...]
else
displayLinkedListIndex = (byte)SoftataLib.Display.SetupDefault(idisplay);
- Insert the following above the else, that is an opton for the BARGRAPH:
else if (displayDevice == DisplayDevice.BARGRAPH)
{
// Use default settings
//displayLinkedListIndex = (byte)SoftataLib.Display.SetupDefault(idisplay);
//Or use custom settings: {data,latch,clock} GPIO Pins
List<byte> settings = new List<byte> {20,21}; // Send the data pin as 16
displayLinkedListIndex = (byte)SoftataLib.Display.Setup(idisplay, 16, settings);
}
- This facilitates using default settings or custom ones. A menu could be used here.
Comment
Note that the other class display methods such as Home are meaningless for this display so are mot embellished beyond returning true. Also, the Misc Flow command could trigger an autonomous continuous display of the successive segments using the Core 2 infrastructure that periodically sends telemetry data.
Conclusion
The Bargarph display has provided a working example of adding a custom display to Softata.
Next: Are we there yet? :)
Topic | Subtopic | |
This Category Links | ||
Category: | Softata Index: | Softata |
Next: > | Softata - Adding a new display | 7. Epilog |
< Prev: | Softata - Adding a new display | 5. Simple Test |