Skip to content

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 just get a handle to the wifi device, connect to an access point. Then we initialize coldwave and register a callback for when an external client set a property:

c
#include <gpio.h>
#include <wlan.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 | TT_ACTIONABLE )

int onLED (const Property &prop, const PropArray &transaction, bool internal);

int main(void) {

    int wlan_dev;
    flake::Service *service;
    coldwave_init_t cw_init = COLDWAVE_INIT_DEFAULT;
     
    // 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,WLAN_SSID, 0, WLAN_SEC_MODE_WPA2, WLAN_PSK, true);
    // now, with a running network connection, Initialize coldwave, 
    cw_init.device_id = "MyDevice123";
    cw_init.product_id = "MySampleProduct";
    coldwave_init(&cw_init, SRV_UUID, &service);
    // register a callback for when an external client writes our P_LED property
    service->on (P_LED, onLED);

    return 0;
}

int onLED (const Property &prop, const PropArray &transaction, bool internal)
{
  // depending on what the client sent us, turn the gpio for our LED on/off.
  gpio_set(GPIO_LED, prop.b ? gpioLogicHigh : gpioLogicLow);
  
  return E_OK;
}