Appearance
Blinky (coldwave style)
The following example is a minimal application for the SiliconLabs WGM160P WiFi MCU. It creates a coldwave service to toggle a LED on a GPIO via a local network connection.
sysconf.c
First we define our peripheral devices and which drivers to use. While this could go into main.cpp as well, it's cleaner to use a separate file. For details on the driver registration, see Sysconfig
c
#include <sysconfig.h>
// link the SiLabs WGM160P wlan driver sources
sysconf_use_driver(wgm160p_wlan)
// create a device wlan0 using the "silabs-wgm160p-wlan" driver
sysconf_create_device("silabs-wgm160p-wlan", wlan0, 0)main.cpp
In our main code, we get a handle to the wifi device and connect to an access point. Then we initialize coldwave and register a callback for when an external client sets a property. The service runs in router mode (cwRouter) and accepts local Coldwave clients on TCP port 9986 — a cwClient node only connects out to the Coldwave Backend and would not accept local connections.
cpp
#include <gpio.h>
#include <wlan.h>
#include <kernel.h>
#include <coldwave/coldwave.h>
#define GPIO_LED (0) //< GPIO Port A Pin 0
#define WLAN_SSID "mySSID"
#define WLAN_PSK "myWPA2PSK"
#define SRV_UUID "4db4192b-6172-4cc8-8ec7-3e917cc22678"
#define P_LED ((0x1000 << 16) | TT_BOOL | TAG_ACTIONABLE )
int onLED (const flake::Property &prop, const flake::PropArray &transaction, bool internal);
int main(void) {
int wlan_dev;
flake::Service *service = nullptr;
coldwave_init_t cw_init = COLDWAVE_INIT_DEFAULT;
// configure the LED gpio as an output
gpio_set_dir(GPIO_LED, gpioPinDirOutput);
// retrieve a handle to the wifi device registered in sysconf.c
wlan_dev = open("wlan0");
// use the wifi driver API to join a wifi network
wlan_connect (wlan_dev, (uint8_t*)WLAN_SSID, nullptr, WLAN_SEC_MODE_WPA2, (void*)WLAN_PSK, true);
// now, with a running network connection, initialize coldwave as a router,
// so local coldwave clients can connect to us
cw_init.node_type = cwRouter;
cw_init.opt.router.no_local_tls = 1; // plain TCP on the LAN
cw_init.opt.router.local_tcp_port = 9986;
cw_init.opt.router.max_clients = 1;
cw_init.opt.router.auth_callback = nullptr;
cw_init.opt.router.auto_update_disabled = 1;
cw_init.device_id = "MyDevice123";
cw_init.product_id = "BLNK"; // 4-character product identifier
if (coldwave_init(&cw_init, SRV_UUID, &service) != CW_OK) {
// handle error
}
// register a callback for when an external client writes our P_LED property
service->on<P_LED>(onLED);
// keep the main thread alive
while (true) {
osDelay(1000);
}
}
int onLED (const flake::Property &prop, const flake::PropArray &transaction, bool internal)
{
// depending on what the client sent us, turn the gpio for our LED on/off.
auto on = prop.value<bool>();
if (on)
gpio_set(GPIO_LED, *on ? gpioLogicHigh : gpioLogicLow);
return E_OK;
}