CDP Studio: Trigger LED patterns with a web GUI
Phil King Long-time contributor to The MagPi, Phil is a freelance writer and editor with a focus on technology. @philkingeditor
Use the low-code, block-based environment of CDP Studio to create and deploy a Raspberry Pi project
An ‘out of the box’ software development tool, CDP Studio is used by numerous companies to build industrial control, automation, and edge systems. Yet, it’s fairly easy to get to grips with its low-code programming environment. Better yet, you can deploy projects to a Raspberry Pi – this is often used for testing and prototyping purposes in industrial settings. In this introductory tutorial, we’ll guide you through the process of creating an app to flash LEDs connected to a Raspberry Pi in different patterns, selectable on a web GUI. We’ll mainly be using CDP Studio’s Block Diagram mode, connecting various preset block components, although we’ll also do a tiny bit of hard coding in C++ to create a new custom function.
Visit **cdpstudio.com/getstarted** and download the free non-commercial version for Linux or Windows. During installation, make sure you select both Raspberry Pi ARMv8 32- bit (Debian 11) and Raspberry Pi ARMv6 32-bit (Debian 10) components, along with the one already ticked for your host PC. You will then be able to deploy projects to any Raspberry Pi model, using Raspberry Pi OS Bullseye or Legacy version, by selecting the appropriate toolkit in CDP Studio. **LED1.Create**, then right-click and select CDP > Add > Property. Name it LED2 and change its Type to bool, and Routing Type to Periodic. Tick the box for Create Another. Click OK, then name this one LED3; untick Create Another, then click OK. Finally, we need to build the library. Right-click the LEDs library name in the left panel and select Build, then Save All.
For our first pattern, we’ll create a custom component. Switch to Configure mode and right- click your LEDs library name in the left panel, then Add New > CDP Component Model. Click Choose, then Next; in the Component Model parameters, give it a class name (LEDSine), and change the Frequency to 2 (cycles per second). Click Next, then Finish. The **LEDSine.cpp** file will be auto-created. Right- click in its code, then select CDP > Add > Port. In the Port type drop-down, select LEDS::LEDPort (the one you created). We’ll name it ‘LEDs’. Under **/* Write your code here */** in **LEDSine.cpp**, add the following (indented) code:
LEDs.LED1 = (counter==0); LEDs.LED2 = (counter==1); LEDs.LED3 = (counter==2); counter += increment;
if (counter==0 || counter ==2) increment*=-1;
In the **LEDSine.h** header, under **LEDPort LEDs;**, add these two lines to create the counter and increment variables:
int counter = 0; int increment = 1;
Build the LEDs library again. In Configure mode, under LEDs in the Resources panel, we now have the LEDSine component available.
Right-click in the left panel and select Create New > CDP System. Give it a name (RPiLEDs), click Next twice, then Finish. In the Block Diagram tab of Configure mode, you’ll now see the main block for the project. Double-click it (or select **RPILEDsApp** in the left panel) to see inside. We can now start adding program blocks. In the bottom left Resources panel of Configure mode, open the CDPCore category and drag a CDPComponent into the diagram. Drag another CDPComponent underneath it. Then open the LEDs category in Resources to find your LEDSine component; drag it under the two CDPComponent blocks. Click on LEDs on the LEDSine block and untick Input in the right-hand panel, to make it an output. Select LEDSine in the LEDs library. In the Table Editor tab, click the right arrow next to LEDs (under Ports) and untick Input there too. Back in the Block Diagram view for RPiLEDsApp, open CDPCore in Resources and drag a PortMUX block into the diagram. This acts as a multiplexer for the three LED patterns, avoiding complex, messy individual wiring. Drag another CDPComponent to the right of PortMUX. Right-click and rename it IO. This block will house our GPIO server. Now drag over a CDPPort (under CDPCore) into both CDPComponent blocks. Select it in each block and untick Input in the right-hand panel, to change it to an output.
Now to add more inputs to our PortMUX block. From LEDs in Resources, drag an LEDPort into the PortMUX block. Select it and, in the right- hand panel, rename it ‘Off’. From CDPCore, add three CDPPorts to the PortMUX block – these are for our three LED patterns. Rename CDPPort to Rand, CDPPort1 to Scan, and CDPPort2 to Sine. From LEDs in Resources, drag an LEDPort into PortMUX; untick Input in the right-hand panel. It’s time to make our first connection: drag a wire from the LEDs output of the LEDSine block to the Sine input on PortMUX.
Add a CDPPort to the IO block and rename it LEDs, then connect the LEDs output of PortMUX to it.
Now to add our GPIO server for Raspberry Pi. Double-click the IO block, then open GPIOPinIO in Resources and drag a GPIOServer into IO. With GPIOServer selected, right-click GPIOPin in Resources and select Add Multiple. Change its name to LED1 and increase Count to 3. Click OK to create three outputs – LED1, LED2, LED3; change them all to inputs. Select the LEDs block in IO. Open CDPCore in Resources and drag three PushConnection outputs into the LEDs block. Rename them LED1, LED2, and LED3. Now connect the LED1, LED2, LED3 outputs of LEDs block to the LED1, LED2, LED3 inputs of the GPIOServer block. We need to enter the GPIO pin numbers for our circuit’s LEDs (**Figure 1**). Select each of the LED1, LED2, LED3 inputs on the GPIOServer in turn and set its Nr value (right panel) to the GPIO pin number: 14, 15, and 18 respectively.
Back in RPiLEDApp, rename the top CDPComponent to LEDRand, and the other CDPComponent to LEDScan. Rename both their CDPPorts to LEDs. Connect them up to the Rand and Scan inputs on PortMUX. We’ll create both of those functions using existing elements and no hard coding. Open LEDRand, select the LEDs block, and add three PullConnection inputs. Rename them LED1, LED2, and LED3. From Automation in Resources, drag an AddRandom <unsigned char> component to the left of the LEDs block. Change its Upper value to 2 in the right-hand panel – it will generate values that will be interpreted as a 1 or 0, to turn an LED on or off. Connect its Out output to the LED1 input on the LEDs block. Let’s create two more AddRandom blocks. Right-click the existing AddRandom block and Copy and Paste into a blank area of the diagram, then select Add. Repeat this process to add a third AddRandom block. Connect the two new blocks’ Outs to the LED2 and LED3 inputs respectively on the LEDs block. In the right-hand panel, change the fs (i.e. frequency) of LEDRand to 2 (or whatever you prefer) to slow it down.
Our LEDScan function flashes the LEDs in sequence, using bit shifting. We don’t have room here to describe its creation in detail, but you can recreate it or copy it directly (by holding **SHIFT** while dragging over it) from the downloadable project (**magpi.cc/github**). If you do copy it over, make sure to copy the three LED1, 2, 3, connections (right panel) from the LEDScan block into the LEDs block you created earlier for your LEDScan function, then delete the copied-over LEDScan block. In your LEDScan function, select Count on the left and untick Input, then connect the Out of the MUX block to it. We need to loop it back to the start, so right-click Count and Copy Path. Select the In of the ShiftLeft block and paste the path into the Routing field. Finally, check the values (right panel) are the same as those in the downloaded project version for the ShiftLeft, GT, and MUX blocks. Change the LEDScan function’s fs property to 2 to slow down the sequence.
Let’s add a web GUI to control the program. Right-click RPiLEDsApp in the top-left panel and Add New > Add Web UI. Use the default options in the dialog. Switch to Design mode to start designing the UI. Delete the existing text box, then drag a Grid Layout element into the canvas. Drag four Buttons into it. Drag a Lamp (representing an LED) to the right of the second button from the top. Then drag two more Lamps to the right of the lower buttons. Drag a Label to the right of the top button; double-click it and change it to ‘LEDs’. Double-click the buttons and label them: Off, Rand, Scan, and Sine. Select the Off button. In the bottom-right panel, scroll down to cdpCheckedRouting. Here you’ll need to input the routing for the button so it works. To do so, return to the RPiLEDsApp diagram, right-click Index on PortMUX, and Copy Path. Back in Design mode, paste it into the cdpCheckedRouting field for the Off button. Tick checkableByEmitValue. For the Rand button, tick checkableByEmitValue and change valueToEmit to 1; paste the path (as before) into cdpCheckedRouting. Repeat this for the other buttons, with valueToEmit set to 2 and 3 respectively. Now to route the Lamps. In the Block Diagram, select the LEDPort output on PortMUX, then right- click LED1 (right panel) and Copy Path. In Design mode, select the top Lamp and paste the path into its cdpStyleRouting field. Select each of the other Lamps, then paste the path into cdpStyleRouting and change the last digit to 2 and 3 respectively.
In Configure Mode, select the LEDs library and the Deploy Configuration tab. Tick the box for Raspberry Pi ARMv6 and/or ARMv8 under Toolkits. Rebuild the library. Make sure your Raspberry Pi has SSH enabled and you’ve modified its **/etc/security/limits.conf** file – see **magpi.cc/cdprpisetup** for details. Wire up the LEDs circuit as in **Figure 1**. To find your Raspberry Pi from CDP Studio, in Deploy Configuration for RPiLEDsApp, click Select next to WiFi under Networks to show devices. Enter **pi** as the Username and click Pair, then enter Raspberry Pi’s password. Choose the appropriate Toolkit for your Raspberry Pi OS version; under Device, select your Raspberry Pi’s IP address. Right-click RPiLEDs in the left panel and select Run & Connect. The web UI can be found at your Raspberry Pi’s IP address followed by **:7869/index.html** – for example, ours was **192.168.1.112:7689/index.html**. You can now click the UI buttons to change the LED pattern.