A Standing Desk

Summary

The installation of the AITERMINAL Electric Standing Desk Frame Dual Motor Height Adjustable Desk Motorized Stand Up Desk-White (Frame Only).  Which is only relevant to IoT in that

  1. I worked on this rather than finishing the really cool article that is coming next week.\
  2. It allows me improved place to work

The Story

Last week in California I used a standup desk… which I enjoyed.  I have one at my office in Lexington as well.  But, if you remember from this picture, I don’t have a standing setup at home.

Since my desk is one solid top, I didn’t really know how I could make a change to standup.  But, one afternoon browsing on Amazon I found this:

 

Basically it is a stand-up-desk, but with no table top.  So I ordered it.

 

 

 

 

 

It came in an absolutely giant box.

With a few statistics on the side

The first thing to do is get out the “Modus Toolbox” thanks to my friends in Ukraine

So, my lab assistant got to it.

Then we made a disaster area, by removing all the crap on my desk.

After that we took the tabletop to the barn and ran a track saw on it.

Once the desk was removed… things were REALLY screwed up on the wall.

And the side cabinet.

Here is Nicholas installing the sawed off table top onto the desk.

Here is the desk back in place.  You can see that I have started repairing the drywall.

Which is always an ugly job.

Now that the wall is fixed, Nicholas worked to repair the networking infrastructure.

And here is back together, with the desk in the standing position

And a close up.

It is awesome because it is almost perfectly integrated into the old desk.  Notice that we trimmed about 5 inches off the back of the desk.

EW21: Lesson 0: Introduction & Resources

Summary

Hello everyone.  This is lesson 0 of a series of 7 lessons about creating an IoT application using the Infineon ModusToolbox Software Environment to create a WiFi enabled drone.  In the next two hours we will build a remote control that uses the PSoC 6 MCU, WiFi, MQTT, CapSense and a 3-D Magnetic Joystick.  Then we will build the drone which will use a PSoC 6, WiFi, MQTT, CapSense and a BLDC motor controller.

What I will do today is take you lesson by lesson through the class and talk about how it all works and what you need to do.  When I built the class it was absolutely my goal to have every button click and line of code described.  That being said,  it is likely that I made some errors.  So, during the class you will be able to send messages to my team who will answer the questions, or ask me and I’ll answer live.  If you missed the class, that’s OK, you will be able to watch it on replay.  In addition if you have a question after the live stream is over, leave a comment here and I’ll answer.

I will attempt to go slowly enough for you to follow along, but if I go to fast, don’t worry you should be able to follow along with the instructions on this website.

Every lesson will have this table in it and you will be able to click to follow along with the different lessons.

Embedded World 2021 - Infineon ModusToolbox PSoC 6 Drone 

 

Here is the overall system architecture:

The Remote Control

The remote control is built with two Infineon development boards

Kit Features
CY8CPROTO-062-4343W PSoC 6,CYW4343W WiFi Bluetooth Combo Radio,CapSense
TLE493D-W2B6 XENSIV 3-D Magnetic Sensor

Here are some pictures.

CY8CPROTO-062-4343W.  The top left of the board is a KitProg programmer.  The middle third on the left is the 4343W and the PSoC 6.  The bottom right are the CapSense buttons.  Just to the right of the programmer is a SD Card holder and S512FL Quad SPI Flash.

This the the top of the TLE-493D-W2B6 3-D magnetic sensor board.  On the far right, the tiny 6 pin chip is the actual sensor.  The big hole to the left of the sensor is to mount a magnet.  The two chips on the left are used as a bridge to USB (if you are developing).  I attach to this device using the I2C interface pins.

Here is the back where you can see the SCL, SDA, Power and Ground labeled.  Unfortunately, they are in the wrong order to plug directly into the PSoC kit so Greg had to make a little wire switcher.

Here is the board with the really really cool 3-d printed joystick.  Simply two pieces of plastic with a magnet in the bottom.

This picture show how the magnetic sense board is mounted onto the kit

Here is the whole thing assembled.

The Crazy Drone

The drone was built with

Kit Features
CY8CKIT-062S2-43012 PSoC 6 + CYW43012 Low Power Bluetooth WiFI Combo
TLE9879WXA40 BLDC Control Shield

Here is a picture of the CY8CKIT-062S2-43012.  On the far right in the middle is the PSoC 6 and CYW43012.  In the lower right are the CapSense Buttons and Slider.  The KitProg IC is just below the top Arduino Header.

In order to drive the BLDC motor I use the TLE9879WXA40 Motor Shield.  This has everything needed to do Field Oriented Control of a 3-phase BLDC motor.  The Blue, Green and White wires are the 3-phases of the BLDC.  The Red and Black are simply +12V and Ground.  You interface from the PSoC to the BLDC shield via a SPI interface (attached to the Arduino pins).

The BLDC motor is mounted into a 3-d printed holder.  The mount is attached to hollow carbon fiber tubes that run on bearings that you see below.  The wires run down through the tubes.

At the bottom, the blue box just provides +12v to the drone and the PSoC board.

Here is a closer picture of the BLDC motor.

Here is a picture of the whole crazy thing running.  If you look in the background you can see a top secret new PSoC motor controller.  Is that an Easter egg?

And yes, it will cut your fingers off if you aren’t careful.

Resources

You will need a few things for this class:

ModusToolbox Software Environment

You can download Modus Toolbox from here

CY8CPROTO-062-4343W

You can read all about this development kit on the website or in the KitGuide

CY8CKIT-062S2-43012

You can read all about this development kit on the website or in the KitGuide

TLE9879QXA40 BLDC Motor Controller

You can read all about this development kit on the website or in the KitGuide

XENSIV TLE493D

You can read all about this development kit on the website or in the KitGuide

EW21: Lesson 1: FreeRTOS + CapSense

Embedded World 2021 - Infineon ModusToolbox PSoC 6 Drone 

 

Summary

In lesson 1 we will create our first project, which like every good embedded project, will start with the blinking LED.  Then I will show you how to add a CapSense slider and two buttons.  At the end of lesson one you will have the “CapSense Task” colored green.  Here is the architecture:

Learning Objectives

By the end of this lesson I would like you to know about:

  1. Creating and building your first ModusToolbox PSoC 6 MCU Project
  2. The organization of a ModusToolbox project
  3. An introduction to CapSense

Procedure

We will follow these steps:

  1. Update manifest.loc
  2. Create the new project
  3. A Tour of your project
  4. Test the basic project
  5. Add a CapSense task header file
  6. Add a CapSense task dot-c file
  7. Update main.c
  8. Test the CapSense

    1. Update the manifest.loc

    After you install ModusToolbox, there is a subdirectory in your home directory called “.modustoolbox”.  You will almost never need to look in this directory.  However, it contains two interesting things:

    1. A cache of the libraries (more on this later)
    2. A file called “manifest.loc” which will allow you to extend the functionality of Modus Toolbox.

    By adding a URL to the manifest.loc you can add in new

    1. Middleware
    2. Board Support Packages
    3. Example Projects

    To make the Embedded World easier, I have extended ModusToolbox to give you access to some example projects and libraries that I created.  To get access you need to add the line “https://github.com/iotexpert/mtb2-iotexpert-manifests/raw/master/iotexpert-super-manifest.xml” into the manifest.loc file.  You can do that with any editor.  Here is how it looks on my Mac.

    arh ~ $ cd .modustoolbox/
    arh .modustoolbox $ ls
    cache		manifest.loc	manifest.loc~
    arh .modustoolbox $ more manifest.loc
    https://github.com/iotexpert/mtb2-iotexpert-manifests/raw/master/iotexpert-super-manifest.xml
    

    2. Create the new project

    Now I start the new project creator from the start menu.  Our first project will be using the CY8CPROTO-062-4343W, pick it and press next.

    In the filter box type “IoT” which will filter to my template projects.  To start with we will use the “IoT Expert FreeRTOS Template” which is a basic project I created that gives you a blinking LED, a UART console and FreeRTOS.  Pick that and give it a sensible name.

    After a bit of processing you will have a new application, press close.

    For this class I am going to use Visual Studio Code.  ModusToolbox also has an Eclipse IDE which you can use, or you can use the command line, IAR, or MicroVision – it’s entirely up to you.  To tell Visual Studio code about my project I will run “make vscode” which will build a vscode project.  Then I run vscode with “code .”

    The make vscode will build all of the files you need including a workspace.  Click”Open Workspace”

    3. A Tour of your Project

    Start a file browser and look around inside your project.

    You can also see these in Visual Studio Code.

    4. Test the basic project

    Now click the debug icon along the left and click the play button to build the project, program it and launch the debugger.

    (You can use the drop-down menu to select “Program (KitProg3_MiniProg4)” and then click the play button if you just want to build and program without launching the debugger.)

    Notice on the console a bunch of stuff comes ripping out… then it programs your development board.

    Once that is done the debugger starts and runs until the start of main.  Press play to get things going.

    5. Add a CapSense task header file

    Now that we have a functioning blinking LED project, let’s add CapSense.  Remember from the architecture picture above that I am going to have a task just for CapSense.  Start by making the public header file called “capsense_task.h”

    In that file there will only be the definition of the task.

    6. Add a CapSense task dot-c file

    Now we will create capsense_task.c.  In that file, you will need some includes to bring in the Infineon drivers.  After the includes I will define several functions

    1. capsense_init: Starts up the CapSense block
    2. process_touch: Figures out what is being touched on the buttons and slider
    3. capsense_isr: Used by the CapSense middleware to process the CapSense interrupt
    4. capsense_end_of_scan_callback: Tells your application when the scanning is done
    5. capsense_register_deepsleep: This allows the CPU to know when it can go to Deep Sleep without interfering with CapSense

    Finally I will define a semaphore to use to block my task until the CapSense scanning is done.

    #include <stdio.h>
    
    #include "cybsp.h"
    #include "cyhal.h"
    #include "cycfg.h"
    #include "cycfg_capsense.h"
    
    #include "FreeRTOS.h"
    #include "task.h"
    #include "semphr.h"
    
    #include "capsense_task.h"
    
    static void capsense_init(void);
    static void process_touch(void);
    static void capsense_isr(void);
    static void capsense_end_of_scan_callback(cy_stc_active_scan_sns_t* active_scan_sns_ptr);
    static void capsense_register_deepsleep(void);
    static QueueHandle_t capsense_done; // Semaphore set in Capsense Callback End of Scan

    The capsense_task function is called by FreeRTOS when it starts up.  This function does what it says

    1. Sets up a sempahore
    2. Initializes the CapSense
    3. Starts scanning
    4. Waits for a scan to complete using a semaphore
    5. Process the widgets
    6. Looks at the touches
    7. Starts another scan
    8. Waits, then loops back
    /*******************************************************************************
    * Function Name: task_capsense
    ********************************************************************************
    * Summary:
    *  Task that initializes the CapSense block and processes the touch input.
    *
    * Parameters:
    *  void *param : Task parameter defined during task creation (unused)
    *
    *******************************************************************************/
    void capsense_task(void* param)
    {
        (void)param;
    
        capsense_done = xQueueCreateCountingSemaphore(1,0);
    
        capsense_init();
    
        Cy_CapSense_ScanAllWidgets(&cy_capsense_context);
    
        for(;;)
        {
            xSemaphoreTake(capsense_done,portMAX_DELAY);
    
            Cy_CapSense_ProcessAllWidgets(&cy_capsense_context);
            process_touch();
            Cy_CapSense_ScanAllWidgets(&cy_capsense_context);
            vTaskDelay(50); // ~20hz update
        }
    }
    

    To initialize the CapSense you need to

    1. Initialize the  CapSense block
    2. Setup the CapSense interrupt
    3. Register the DeepSleep function
    4. Ask to be called back when a scan finishes
    5. Enable the CapSense block
    /*******************************************************************************
    * Function Name: capsense_init
    ********************************************************************************
    * Summary:
    *  This function initializes the CSD HW block, and configures the CapSense
    *  interrupt.
    *******************************************************************************/
    static void capsense_init(void)
    {
        Cy_CapSense_Init(&cy_capsense_context);
        
        static const cy_stc_sysint_t capSense_intr_config =
        {
            .intrSrc = csd_interrupt_IRQn,
            .intrPriority = 7,
        };
    
        /* Initialize CapSense interrupt */
        Cy_SysInt_Init(&capSense_intr_config, &capsense_isr);
        NVIC_ClearPendingIRQ(capSense_intr_config.intrSrc);
        NVIC_EnableIRQ(capSense_intr_config.intrSrc);
    
        capsense_register_deepsleep();
        Cy_CapSense_RegisterCallback(CY_CAPSENSE_END_OF_SCAN_E,
                                                  capsense_end_of_scan_callback, &cy_capsense_context);
        
        Cy_CapSense_Enable(&cy_capsense_context);
    }
    

    Infineon provides the ISR which is part of the CapSense scanning engine

    /*******************************************************************************
    * Function Name: capsense_isr
    ********************************************************************************
    * Summary:
    *  Wrapper function for handling interrupts from CSD block.
    *
    *******************************************************************************/
    static void capsense_isr(void)
    {
        Cy_CapSense_InterruptHandler(CYBSP_CSD_HW, &cy_capsense_context);
    }

    When a scan is done we want to unlock the capsense_task by giving the semaphore.

    /*******************************************************************************
    * Function Name: capsense_end_of_scan_callback
    ********************************************************************************
    * Summary:
    *  CapSense end of scan callback function. This function sends a command to
    *  CapSense task to process scan.
    *
    * Parameters:
    *  cy_stc_active_scan_sns_t * active_scan_sns_ptr (unused)
    *
    *******************************************************************************/
    static void capsense_end_of_scan_callback(cy_stc_active_scan_sns_t* active_scan_sns_ptr)
    {
        BaseType_t xYieldRequired;
    
        (void)active_scan_sns_ptr;
        xYieldRequired = xSemaphoreGiveFromISR(capsense_done,&xYieldRequired);
    
        portYIELD_FROM_ISR(xYieldRequired);
    }

    The DeepSleep registration function looks like this:

    /*******************************************************************************
    * Function Name: capsense_register_deepsleep
    ********************************************************************************
    * Summary:
    *  Wrapper function for setting up the Deep Sleep callback for CapSense.
    *  This is necessary so that a transition from active to Deep Sleep does not
    *  occur during a CapSense scan.
    *
    *  See the "MTB CAT1 Peripheral driver library documentation > PDL API Reference > SysPM"
    *  link in the Quick Panel Documentation for information on setting up the SysPm callbacks
    *******************************************************************************/
    static void capsense_register_deepsleep(void)
    {
        static cy_stc_syspm_callback_params_t callback_params =
        {
            .base       = CYBSP_CSD_HW,
            .context    = &cy_capsense_context
        };
    
        static cy_stc_syspm_callback_t capsense_deep_sleep_cb =
        {
            Cy_CapSense_DeepSleepCallback,
            CY_SYSPM_DEEPSLEEP,
            0,
            &callback_params,
            NULL,
            NULL
        };
    
        Cy_SysPm_RegisterCallback(&capsense_deep_sleep_cb);
    }

    The last bit of magic is the processing of the touches.  We will

    1. Look at the buttons
    2. If they are pressed do a print out
    3. Look a the slider (which gives you a value between 0 and 300).
    4. If the slider is being pressed, print out the value
    5. Save the current state of the buttons and slider for next time around.
    /*******************************************************************************
    * Function Name: process_touch
    *******************************************************************************/
    static void process_touch(void)
    {
        /* Variables used to store previous touch information */
        static uint32_t button0_status_prev = 0;
        static uint32_t button1_status_prev = 0;
        static uint16_t slider_pos_prev = 0;
    
        uint32_t button0_status = 0;
        uint32_t button1_status = 0;
        uint16_t slider_pos = 0;
        uint8_t slider_touched = 0;
        cy_stc_capsense_touch_t *slider_touch;
    
    // Process the buttons
    
        button0_status = Cy_CapSense_IsWidgetActive(CY_CAPSENSE_BUTTON0_WDGT_ID,&cy_capsense_context);
        button1_status = Cy_CapSense_IsSensorActive( CY_CAPSENSE_BUTTON1_WDGT_ID, CY_CAPSENSE_BUTTON1_SNS0_ID, &cy_capsense_context);
    
        if((0u != button0_status) && (0u == button0_status_prev))
        {
            printf("Button 0 pressed\n");
        }
    
        if((0u != button1_status) && (0u == button1_status_prev))
        {
            printf("Button 1 pressed\n");
        }
    
    // Process the slider
        slider_touch = Cy_CapSense_GetTouchInfo( CY_CAPSENSE_LINEARSLIDER0_WDGT_ID, &cy_capsense_context);
        slider_pos = (slider_touch->ptrPosition->x / 3); // Transform 0-300 into 0-100
        slider_touched = slider_touch->numPosition;
    
        if((0u != slider_touched) && (slider_pos_prev != slider_pos ))
        {
            printf("Slider position %d\n",slider_pos);
        }
    
        button0_status_prev = button0_status;
        button1_status_prev = button1_status;
        slider_pos_prev = slider_pos;
    }

    7. Update main.c

    In main.c we need to include the capsense_task header file

    #include "capsense_task.h"
    

    And startup the capsense_task

    xTaskCreate(capsense_task, "CapSense", configMINIMAL_STACK_SIZE*4, NULL, 1, 0 );
    

    8. Test the CapSense

    Program your kit and you will be able to press the buttons and slider.

    Resources for Project

    Remember above when I started the project creator there was a list of projects.  This exact completed project is available to you as “IoT Expert Embedded World 2021 Lesson1”

    You can also clone this project from git@github.com:iotexpert/ew21-lesson1.git or https://github.com/iotexpert/ew21-lesson1.git

    For reference, here is the whole capsense_task.c file in one chunk
    #include <stdio.h>
    #include "cybsp.h"
    #include "cyhal.h"
    #include "cycfg.h"
    #include "cycfg_capsense.h"
    #include "FreeRTOS.h"
    #include "task.h"
    #include "semphr.h"
    #include "capsense_task.h"
    static void capsense_init(void);
    static void process_touch(void);
    static void capsense_isr(void);
    static void capsense_end_of_scan_callback(cy_stc_active_scan_sns_t* active_scan_sns_ptr);
    static void capsense_register_deepsleep(void);
    static QueueHandle_t capsense_done; // Semaphore set in Capsense Callback End of Scan
    /*******************************************************************************
    * Function Name: task_capsense
    ********************************************************************************
    * Summary:
    *  Task that initializes the CapSense block and processes the touch input.
    *
    * Parameters:
    *  void *param : Task parameter defined during task creation (unused)
    *
    *******************************************************************************/
    void capsense_task(void* param)
    {
    (void)param;
    capsense_done = xQueueCreateCountingSemaphore(1,0);
    capsense_init();
    Cy_CapSense_ScanAllWidgets(&cy_capsense_context);
    for(;;)
    {
    xSemaphoreTake(capsense_done,portMAX_DELAY);
    Cy_CapSense_ProcessAllWidgets(&cy_capsense_context);
    process_touch();
    Cy_CapSense_ScanAllWidgets(&cy_capsense_context);
    vTaskDelay(50); // ~20hz update
    }
    }
    /*******************************************************************************
    * Function Name: process_touch
    *******************************************************************************/
    static void process_touch(void)
    {
    /* Variables used to store previous touch information */
    static uint32_t button0_status_prev = 0;
    static uint32_t button1_status_prev = 0;
    static uint16_t slider_pos_prev = 0;
    uint32_t button0_status = 0;
    uint32_t button1_status = 0;
    uint16_t slider_pos = 0;
    uint8_t slider_touched = 0;
    cy_stc_capsense_touch_t *slider_touch;
    // Process the buttons
    button0_status = Cy_CapSense_IsWidgetActive(CY_CAPSENSE_BUTTON0_WDGT_ID,&cy_capsense_context);
    button1_status = Cy_CapSense_IsSensorActive( CY_CAPSENSE_BUTTON1_WDGT_ID, CY_CAPSENSE_BUTTON1_SNS0_ID, &cy_capsense_context);
    if((0u != button0_status) && (0u == button0_status_prev))
    {
    printf("Button 0 pressed\n");
    }
    if((0u != button1_status) && (0u == button1_status_prev))
    {
    printf("Button 1 pressed\n");
    }
    // Process the slider
    slider_touch = Cy_CapSense_GetTouchInfo( CY_CAPSENSE_LINEARSLIDER0_WDGT_ID, &cy_capsense_context);
    slider_pos = (slider_touch->ptrPosition->x / 3); // Transform 0-300 into 0-100
    slider_touched = slider_touch->numPosition;
    if((0u != slider_touched) && (slider_pos_prev != slider_pos ))
    {
    printf("Slider position %d\n",slider_pos);
    }
    button0_status_prev = button0_status;
    button1_status_prev = button1_status;
    slider_pos_prev = slider_pos;
    }
    /*******************************************************************************
    * Function Name: capsense_init
    ********************************************************************************
    * Summary:
    *  This function initializes the CSD HW block, and configures the CapSense
    *  interrupt.
    *******************************************************************************/
    static void capsense_init(void)
    {
    Cy_CapSense_Init(&cy_capsense_context);
    static const cy_stc_sysint_t capSense_intr_config =
    {
    .intrSrc = csd_interrupt_IRQn,
    .intrPriority = 7,
    };
    /* Initialize CapSense interrupt */
    Cy_SysInt_Init(&capSense_intr_config, &capsense_isr);
    NVIC_ClearPendingIRQ(capSense_intr_config.intrSrc);
    NVIC_EnableIRQ(capSense_intr_config.intrSrc);
    capsense_register_deepsleep();
    Cy_CapSense_RegisterCallback(CY_CAPSENSE_END_OF_SCAN_E,
    capsense_end_of_scan_callback, &cy_capsense_context);
    Cy_CapSense_Enable(&cy_capsense_context);
    }
    /*******************************************************************************
    * Function Name: capsense_end_of_scan_callback
    ********************************************************************************
    * Summary:
    *  CapSense end of scan callback function. This function sends a command to
    *  CapSense task to process scan.
    *
    * Parameters:
    *  cy_stc_active_scan_sns_t * active_scan_sns_ptr (unused)
    *
    *******************************************************************************/
    static void capsense_end_of_scan_callback(cy_stc_active_scan_sns_t* active_scan_sns_ptr)
    {
    BaseType_t xYieldRequired;
    (void)active_scan_sns_ptr;
    xYieldRequired = xSemaphoreGiveFromISR(capsense_done,&xYieldRequired);
    portYIELD_FROM_ISR(xYieldRequired);
    }
    /*******************************************************************************
    * Function Name: capsense_isr
    ********************************************************************************
    * Summary:
    *  Wrapper function for handling interrupts from CSD block.
    *
    *******************************************************************************/
    static void capsense_isr(void)
    {
    Cy_CapSense_InterruptHandler(CYBSP_CSD_HW, &cy_capsense_context);
    }
    /*******************************************************************************
    * Function Name: capsense_register_deepsleep
    ********************************************************************************
    * Summary:
    *  Wrapper function for setting up the Deep Sleep callback for CapSense.
    *  This is necessary so that a transition from active to Deep Sleep does not
    *  occur during a CapSense scan.
    *
    *  See the "MTB CAT1 Peripheral driver library documentation > PDL API Reference > SysPM"
    *  link in the Quick Panel Documentation for information on setting up the SysPm callbacks
    *******************************************************************************/
    static void capsense_register_deepsleep(void)
    {
    static cy_stc_syspm_callback_params_t callback_params =
    {
    .base       = CYBSP_CSD_HW,
    .context    = &cy_capsense_context
    };
    static cy_stc_syspm_callback_t capsense_deep_sleep_cb =
    {
    Cy_CapSense_DeepSleepCallback,
    CY_SYSPM_DEEPSLEEP,
    0,
    &callback_params,
    NULL,
    NULL
    };
    Cy_SysPm_RegisterCallback(&capsense_deep_sleep_cb);
    }
    

    EW21: Lesson 2: Joystick

    Embedded World 2021 - Infineon ModusToolbox PSoC 6 Drone 

     

    Summary

    In lesson 2 we will add the Infineon XENSIV 3-D Magnetic Sensor with a Joystick to the project .  At the end of lesson two you will have the “JoyStick Task” colored green.  Here is the architecture:

    Learning Objectives

    In this lesson I will teach you about

    1. Libraries & the Library Manager
    2. Multiple RTOS Tasks
    3. XENSIV 3-D Magnetic JoyStick

    Procedure

    I am going to follow these steps:

    1. Look at the Sensor GUI
    2. Start a new project
    3. Add the TLx493D library
    4. Create the joystick_task public header
    5. Create the joystick_task implementation file
    6. Test

    1.Look at the Sensor GUI

    Start the GUI and click the connect button.

    Press start and you will now see x,y,z values.

    Click on the joystick view to see how it looks as a joystick.

    2. Start a new project

    Start the new project creator and pick out “CY8CPROTO-062-4343W”

    This time I will start from the previous project.  Press the “Import” button and select your old project to open.

    Give your project a new name (notice that we are starting from 1 as the template)

    Open up the command line and make vscode.

    Do a command line build/program by running “make -j program”.

    It will finish compiling and then program your board.

    Now look at the serial terminal and make sure that the CapSense is still working

    3. Add the TLx493D library

    In order to interact with the TLx493D it would be much simpler if you had a library.  Well, I have just the solution for that.  Start the library browser and filter for IoT.  Notice that one of your choices is the TLx493D 3D Magnetic Sensor library.  Check the box, then press Update. Once the update finishes, press Close.

    After running the update you will notice some new stuff.  First there is a file in your “deps” directory.  We will look at that in a second.  Also notice that there is a new directory in the mtb_shared directory.

    When you look at the dot-MTB file you will see that it is just a

    1. URL to GitHub
    2. A branch name = “master”
    3. A location to put it = mtb_shared/TLx493D_3Dmagnetic/master

    This is JUST A SIMPLE GIT REPOSITORY.

    When you open vscode and look in the repository you will see normal c-code.

    4. Create the Joystick_task public header

    We want to have a task called “joystick_task”.  So create the public header file for that task.

    Then add the long and extensive code.  Just a function prototype for the joystick task.

    #pragma once
    void joystick_task(void *param);
    

    5. Create the joystick_task implementation file

    Now the actual code for the joystick is straight forward

    1. Some includes
    2. The task definition
    3. Initialize the interface to the joystick with TLxI2CInit
    4. Initialize the library with TLx493xInit
    5. Loop repeatedly
    6. Read one frame from the joystick sensor
    7. Convert the frame to x-y coordinates
    8. If there is a new position print it.
    9. Save the old data
    10. Wait a bit
    11. Loop back to the start
    #include "cybsp.h"
    #include "FreeRTOS.h"
    #include "task.h"
    #include <stdio.h>
    #include "joystick_task.h"
    #include "PSoC_TLx_interface.h"
    #include "TLxJoystick.h"
    #define JOYSTICK_INTERVAL_MS    (100)   /* in milliseconds*/
    #define JOYSTICK_HYSTERESIS		(1)
    /*******************************************************************************
    * Function Name: task_joystick
    ********************************************************************************
    * Summary:
    *  Task that initializes the Joystick block and processes the input.
    *
    * Parameters:
    *  void *param : Task parameter defined during task creation (unused)
    *
    *******************************************************************************/
    void joystick_task(void* param)
    {
    (void)param;
    cy_rslt_t result;
    TLx493D_data_frame_t frame;
    TLxJoyStickXY_t joystick_curr;
    TLxJoyStickXY_t joystick_prev;
    /* Initialize I2C interface to talk to the TLx493D sensor */
    TLxI2CInit(CYBSP_I2C_SDA,
    CYBSP_I2C_SCL,
    0 /* Use Default Sensor Address */,
    0 /* Use Default Speed */,
    NULL /* Use Auto-assigned clock */);
    /* Configure the TLx493D sensor */
    result = TLx493D_init();
    if (result != CY_RSLT_SUCCESS)
    {
    printf("Joystick not detected. Exiting Joystick task.\n");
    vTaskDelete(NULL);
    }
    /* Set Sensor to Master Control Mode */
    TLx493D_set_operation_mode(TLx493D_OP_MODE_MCM);
    /* Repeatedly running part of the task */
    for(;;)
    {
    TLx493D_read_frame(&frame);
    TLxJoystickCovertXY(&frame,&joystick_curr);
    /* Only update/print new value if it has changed by more than the hysteresis value */
    if((joystick_curr.x > (joystick_prev.x + JOYSTICK_HYSTERESIS)) || (joystick_curr.x < (joystick_prev.x - JOYSTICK_HYSTERESIS)))
    {
    printf("Joystick Position: %d\n", joystick_curr.x);
    joystick_prev.x = joystick_curr.x;
    joystick_prev.y = joystick_curr.y;
    }
    vTaskDelay(JOYSTICK_INTERVAL_MS);
    }
    }
    

    6. Edit main.c

    In main.c you need to include the joystick_task.h and then start the task

    #include "cyhal.h"
    #include "cybsp.h"
    #include "cy_retarget_io.h"
    #include <stdio.h>
    #include "FreeRTOS.h"
    #include "task.h"
    #include "capsense_task.h"
    #include "joystick_task.h"
    volatile int uxTopUsedPriority ;
    void blink_task(void *arg)
    {
    cyhal_gpio_init(CYBSP_USER_LED,CYHAL_GPIO_DIR_OUTPUT,CYHAL_GPIO_DRIVE_STRONG,0);
    for(;;)
    {
    cyhal_gpio_toggle(CYBSP_USER_LED);
    vTaskDelay(500);
    }
    }
    int main(void)
    {
    uxTopUsedPriority = configMAX_PRIORITIES - 1 ; // enable OpenOCD Thread Debugging
    /* Initialize the device and board peripherals */
    cybsp_init() ;
    __enable_irq();
    cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX, CY_RETARGET_IO_BAUDRATE);
    // Stack size in WORDs
    // Idle task = priority 0
    xTaskCreate(blink_task,    "blink"     ,configMINIMAL_STACK_SIZE*1  ,0 /* args */ ,0 /* priority */, 0 /* handle */);
    xTaskCreate(capsense_task, "CapSense"  ,configMINIMAL_STACK_SIZE*4  , NULL, 1, 0);
    xTaskCreate(joystick_task, "Joystick"  ,configMINIMAL_STACK_SIZE*4  , NULL, 1, 0);
    vTaskStartScheduler();
    }

    7. Test

    When you test, you will see that the joystick moves around and prints new values.  Plus the CapSense still works.

    Resources for Project

    You can find this completed project in your project creator dialog by filtering for “IoT Expert Embedded”.  This is lesson2

    You can also clone this project at git@github.com:iotexpert/ew21-lesson2.git or https://github.com/iotexpert/ew21-lesson2

    EW21: Lesson 3: WiFi

    Embedded World 2021 - Infineon ModusToolbox PSoC 6 Drone 

     

    Summary

    In lesson 3 we will add a task to manage the “Cloud”, this is IoT after all.  We will start by adding the connection to the WiFi network using the AnyCloud Wireless Connection Manager within ModusToolbox.  At the end of lesson three you will have the “Cloud Task” colored green – well actually partially green.  Here is the architecture:

    Learning Objectives

    1. AnyCloud Wireless Libraries
    2. Architecture of the PSoC 6 + 43xxxx WiFi Combo
    3. Wireless Connection Manager

    Procedure

    In this lesson we will follow these steps:

    1. Create a new project by copying lesson 2
    2. Add the WiFi Libraries
    3. Copy the wireless configuration templates files into your project
    4. Look at the Wireless Library Documentation
    5. Update the Makefile
    6. Add cloud_task.h
    7. Add cloud_task.c
    8. Update main.c
    9. Test

    1. Create a new project by copying lesson 2

    Start the new project creator and select the CY8CPROTO-062-4343W

    Click the import button.  Then select your previous project and press open.

    Select your new template and give it a reasonable name.

    Now program your project to make sure that things are still working.

    Here is the serial terminal.

    2. Add the WiFi Libraries

    Start up the library manager and go to the Libraries tab.  Click on wireless connection manager.  Notice that it will bring in a bunch of other libraries that it depends on.

    3. Copy the wireless configuration templates into your project

    MBED TLS and LWIP have about a billion options.  We provide a template for those options which you will need to copy into your project.  The templates are located in the wifi-mw-core library in the “config” directory.  Copy and paste them into your project.

    Here is a clip of the lwip configuration options.  I wouldn’t recommend changing anything unless you are sure you know what you are doing.

    4. Look at the Wireless Library Documentation

    All of the Infineon libraries will have a directory called “doc” and inside of that directory there will be a file called api_reference_manual.html

    When you open the file you will  see our doxygen generated documentation.  Here the WiFi connection manager documentation.

    5. Update the Makefile

    The makefile controls all of the libraries.  To enable these libraries you need to add the following “components” to the list if they aren’t already there. (Note that all of this information is in the Quick Start section of the library’s documentation).

    COMPONENTS= FREERTOS LWIP MBEDTLS

    And you need to tell the system about the configuration.

    DEFINES+= MBEDTLS_USER_CONFIG_FILE='"mbedtls_user_config.h"' 
    DEFINES+=CYBSP_WIFI_CAPABLE
    DEFINES+=CY_RTOS_AWARE

    6. Add the cloud_task.h

    Just like the other lessons you will need to create a public header file for the cloud task.

    Inside of that file there will be only one function prototype for the task.

    #pragma once
    void cloud_task(void *param);

    7. Add the cloud_task.c

    Make the cloud_task.c file to add the cloud infrastructure to your project.

    At the top of the file we will have some includes.  And I will define some macros that tell WiFi which AP to attach to. When you do this on your own, you will need to update those to match your WiFi AP SSID and password. The task itself is really simple for now, just connect to WiFi and then do nothing.

    #include <stdio.h>
    #include "FreeRTOS.h"
    #include "task.h"
    #include "cy_wcm.h"
    #include "cloud_task.h"
    #define CLOUD_WIFI_AP        "ew2021"
    #define CLOUD_WIFI_PW        "ew2021ap"
    #define CLOUD_WIFI_SECURITY  CY_WCM_SECURITY_WPA2_MIXED_PSK
    #define CLOUD_WIFI_BAND      CY_WCM_WIFI_BAND_ANY
    static void cloud_connectWifi();
    void cloud_task(void* param)
    {
    (void)param;
    cloud_connectWifi();
    while(1)
    {
    vTaskDelay(1);
    }
    }

    The connect to WiFi function will

    1. Setup the Access Point information
    2. Initialize the wireless connection manager
    3. Then go into a loop that will attempt to connect until there is success.
    4. Once a connection is made, it will print out the IP information.
    static void cloud_connectWifi()
    {
    cy_rslt_t result;
    cy_wcm_connect_params_t connect_param = {
    .ap_credentials.SSID = CLOUD_WIFI_AP,
    .ap_credentials.password = CLOUD_WIFI_PW,
    .ap_credentials.security = CLOUD_WIFI_SECURITY,
    .BSSID = {0},
    .band = CLOUD_WIFI_BAND,
    };
    cy_wcm_config_t config = {.interface = CY_WCM_INTERFACE_TYPE_STA}; // We are a station (not a Access Point)
    cy_wcm_init(&config); // Initialize the connection manager
    printf("\nWi-Fi Connection Manager initialized.\n");
    do
    {
    cy_wcm_ip_address_t ip_address;
    printf("Connecting to Wi-Fi AP '%s'\n", connect_param.ap_credentials.SSID);
    result = cy_wcm_connect_ap(&connect_param, &ip_address);
    if (result == CY_RSLT_SUCCESS)
    {
    printf("Successfully connected to Wi-Fi network '%s'.\n",
    connect_param.ap_credentials.SSID);
    // Print IP Address
    if (ip_address.version == CY_WCM_IP_VER_V4)
    {
    printf("IPv4 Address Assigned: %d.%d.%d.%d\n", (uint8_t)ip_address.ip.v4,
    (uint8_t)(ip_address.ip.v4 >> 8), (uint8_t)(ip_address.ip.v4 >> 16),
    (uint8_t)(ip_address.ip.v4 >> 24));
    }
    else if (ip_address.version == CY_WCM_IP_VER_V6)
    {
    printf("IPv6 Address Assigned: %0X:%0X:%0X:%0X\n", (unsigned int)ip_address.ip.v6[0],
    (unsigned int)ip_address.ip.v6[1], (unsigned int)ip_address.ip.v6[2],
    (unsigned int)ip_address.ip.v6[3]);
    }
    break; /* Exit the for loop once the connection has been made */
    }
    else
    {
    printf("WiFi Connect Failed Retrying\n");
    vTaskDelay(2000); // wait 2 seconds and try again;
    }
    } while (result != CY_RSLT_SUCCESS);
    }
    

    8. Update main.c

    In main.c you need to include the cloud task

    #include "cloud_task.h"

    Then start the cloud task.

    xTaskCreate(cloud_task,    "Cloud"     ,configMINIMAL_STACK_SIZE*8  , NULL, 2, 0);  
    

    9. Test

    Program your project and make sure that it connects, and the CapSense and joystick still work.

    Resources for Project

    You can find this projects in your project creator dialog by filtering for “IoT Expert Embedded”.  This is lesson3

    You can also clone this project at git@github.com:iotexpert/ew21-lesson3.git or https://github.com/iotexpert/ew21-lesson3

    EW21: Lesson 4: MQTT

    Embedded World 2021 - Infineon ModusToolbox PSoC 6 Drone 

     

    Summary

    In lesson 4 we will finish the cloud task by adding a connection to the MQTT broker.  Then I will make a very minor update to the CapSense and Joystick tasks to send their data to the Cloud.  At the end of this lesson you will have the Cloud truly colored green.  Here is the architecture:

    Learning Objectives

    1. MQTT
    2. AnyCloud use of AWS libraries

    Procedure

    1. Create a new project by copying lesson 3
    2. Add the MQTT Library
    3. Copy the core MQTT configuration
    4. Update the Makefile
    5. Update the cloud_task.h
    6. Update the cloud_task.c
    7. Update joystick_task.c
    8. Update capsense_task.c
    9. Test (using an MQTT Client)
    10. Test (using our C++/Qt GUI)

    1. Create a new project by copying lesson 3

    Start the new project creator.  Pick the CY8CPROTO-062-4343W and press next.

    Click on the import button

    And select project three to start from.

    Give it a sensible name

    2. Add the MQTT Library

    Start the library browser and pick out the MQTT library.  Notice that it brings in a bunch of other libraries including the AWS libraries.

    3. Copy the core MQTT configuration

    When you look at the README for the MQTT it tells you to copy the file core_mqtt_config.h into your project.  Do so.

    4. Update the Makefile

    For this to work you need to make two changes to your Makefile.  First enable the secure sockets.

    COMPONENTS= FREERTOS LWIP MBEDTLS SECURE_SOCKETS

    Then ignore a few files from the AWS library.

    CY_IGNORE+=$(SEARCH_aws-iot-device-sdk-embedded-C)/libraries/standard/coreHTTP
    CY_IGNORE+=libs/aws-iot-device-sdk-embedded-C/libraries/standard/coreHTTP
    

    5. Update the cloud_task.h

    In order for other tasks to send a message I will create a function where they can send a “speed” also known as a percent 0-100.  This function will be called from both the CapSense task as well as the JoyStick task.

    void cloud_sendMotorSpeed(int speed);
    

    6. Update the cloud_task.c

    In the previous lesson we created the cloud task which connects to the WiFI network.  In this lesson we add MQTT.  Start by adding the includes for the MQTT and the RTOS queue.

    #include "cy_mqtt_api.h"
    #include "queue.h"

    Rather than have magic numbers (or values) hidden in my code I provide #defines for:

    1. The MQTT broker
    2. My Client ID
    3. The Topic to broadcast to
    4. The JSON key for the motor speed

    For other tasks to talk to this task they will push a value of the speed into a queue.  So I need to define that queue.  I will also use a static local variable to hold the handle of the MQTT connection.

    I then define functions to

    1. Connect to MQTT
    2. Start MQTT
    3. Publish to MQTT
    #define CLOUD_MQTT_BROKER        "mqtt.eclipseprojects.io"
    #define CLOUD_MQTT_CLIENT_PREFIX "remote"
    #define CLOUD_MQTT_TOPIC         "motor_speed"
    #define MOTOR_KEY                "motor"
    static QueueHandle_t motor_value_q;
    static cy_mqtt_t mqtthandle;
    static void cloud_connectWifi();
    static void cloud_startMQTT();
    static void cloud_mqtt_event_cb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data);
    static void cloud_publishMessage(char *topic,char *message);

    The updates to the cloud task are:

    1. Start WiFi
    2. Start MQTT
    3. Start the queue
    4. In the main loop, wait for a message, then publish it.
    void cloud_task(void* param)
    {
    (void)param;
    cloud_connectWifi();
    cloud_startMQTT();
    motor_value_q = xQueueCreate(1,sizeof(uint32_t));
    for(;;)
    {
    int motorSpeed;
    char message[32];
    xQueueReceive(motor_value_q, &motorSpeed, portMAX_DELAY);
    snprintf(message, sizeof(message)-1, "{\"%s\":%d}",MOTOR_KEY,motorSpeed);
    cloud_publishMessage(CLOUD_MQTT_TOPIC,message);
    }
    }
    

    This function is used in other tasks to send the motor speed into the queue.

    void cloud_sendMotorSpeed(int speed)
    {
    if(motor_value_q)
    xQueueOverwrite(motor_value_q,&speed);
    }
    

    To start MQTT you need to

    1. Define the MQTT broker structure
    2. Create the MQTT system
    3. Define your connection information
    4. Create a random clientID (so there are no duplicated clients on the MQTT broker)
    5. Make the connection
    static void cloud_startMQTT()
    {
    static cy_mqtt_connect_info_t    	connect_info;
    static cy_mqtt_broker_info_t     	broker_info;
    static uint8_t buffer[1024];
    cy_rslt_t result;
    result = cy_mqtt_init();
    broker_info.hostname = CLOUD_MQTT_BROKER;
    broker_info.hostname_len = strlen(broker_info.hostname);
    broker_info.port = 1883;
    result = cy_mqtt_create( buffer, sizeof(buffer),
    NULL, &broker_info,
    cloud_mqtt_event_cb, NULL,
    &mqtthandle );
    CY_ASSERT(result == CY_RSLT_SUCCESS);
    static char clientId[32];
    srand(xTaskGetTickCount());
    snprintf(clientId,sizeof(clientId),"%s%6d",CLOUD_MQTT_CLIENT_PREFIX,rand());
    memset( &connect_info, 0, sizeof( cy_mqtt_connect_info_t ) );
    connect_info.client_id      = clientId;
    connect_info.client_id_len  = strlen(connect_info.client_id);
    connect_info.keep_alive_sec = 60;
    connect_info.will_info      = 0;
    connect_info.clean_session = true;
    result = cy_mqtt_connect( mqtthandle, &connect_info );
    CY_ASSERT(result == CY_RSLT_SUCCESS);
    printf("MQTT Connect Success to %s Client=%s\n",CLOUD_MQTT_BROKER,clientId);
    }

    You need an MQTT callback.  This is unused in this lesson but we will modify it for the drone.  Just copy it into your project

    static void cloud_mqtt_event_cb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data )
    {
    cy_mqtt_publish_info_t *received_msg;
    printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
    (void)user_data;
    switch( event.type )
    {
    case CY_MQTT_EVENT_TYPE_DISCONNECT :
    if( event.data.reason == CY_MQTT_DISCONN_TYPE_BROKER_DOWN )
    {
    printf( "\nCY_MQTT_DISCONN_TYPE_BROKER_DOWN .....\n" );
    }
    else
    {
    printf( "\nCY_MQTT_DISCONN_REASON_NETWORK_DISCONNECTION .....\n" );
    }
    break;
    case CY_MQTT_EVENT_TYPE_PUBLISH_RECEIVE :
    received_msg = &(event.data.pub_msg.received_message);
    printf( "Incoming Publish Topic Name: %.*s\n", received_msg->topic_len, received_msg->topic );
    printf( "Incoming Publish message Packet Id is %u.\n", event.data.pub_msg.packet_id );
    printf( "Incoming Publish Message : %.*s.\n\n", ( int )received_msg->payload_len, ( const char * )received_msg->payload );
    break;
    default :
    printf( "\nUNKNOWN EVENT .....\n" );
    break;
    }
    }

    To publish a message you

    1. Create the message
    2. Send the publish
    static void cloud_publishMessage(char *topic,char *message)
    {
    cy_mqtt_publish_info_t  pub_msg;
    pub_msg.qos = CY_MQTT_QOS0;
    pub_msg.topic = topic;
    pub_msg.topic_len = strlen(pub_msg.topic);
    pub_msg.payload = message;
    pub_msg.payload_len = strlen(message);
    cy_mqtt_publish( mqtthandle, &pub_msg );
    printf("Published to Topic=%s Message=%s\n",topic,message);
    }
    

    7. Update joystick_task.c

    We want the joystick task to be able to send joystick positions.  Include the cloud_task.h so that it gets access to the function to send messages.

    #include "cloud_task.h"

    Update the joystick function to call the send message function.

    /* Only update/print new value if it has changed by more than the hysteresis value */
    if((joystick_curr.x > (joystick_prev.x + JOYSTICK_HYSTERESIS)) || (joystick_curr.x < (joystick_prev.x - JOYSTICK_HYSTERESIS)))
    {
    printf("Joystick Position: %d\n", joystick_curr.x);
    cloud_sendMotorSpeed(joystick_curr.x); 
    }

    8. Update capsense_task.c

    We want the CapSense task to be able to send CapSense positions.  Include the cloud_task.h so that it gets access to the function to send messages.

    #include "cloud_task.h"

    Update the button code to send either 0 or 75 (0=STOP!!!#*##&&#) (75=take off)

    if((0u != button0_status) && (0u == button0_status_prev))
    {
    printf("Button 0 pressed\n");
    cloud_sendMotorSpeed(0); // Stop the Motor
    }
    if((0u != button1_status) && (0u == button1_status_prev))
    {
    printf("Button 1 pressed\n");
    cloud_sendMotorSpeed(75); // Set the motor to 75%
    }

    The slider just sends the current position.

    if((0u != slider_touched) && (slider_pos_prev != slider_pos ))
    {
    printf("Slider position %d\n",slider_pos);
    cloud_sendMotorSpeed(slider_pos);
    }

    9. Test (using an MQTT Client)

    You can download/install MQTTBox from here  This is a Mac/Linux/Windows MQTT client.  This will enable you to attach to the MQTT broker and then subscribe to the motor speed topic.

    Setup the configuration for the MQTT broker by pressing the gear button

    Then program your remote control and send some joystick and CapSense values.

    And you will see them coming out on the MQTTBox client

    10. Test (using our C++/Qt GUI)

    My friend Butch created a C++/Qt GUI to “simulate” the drone.  You can find it in the “simulator” directory of project 4.  Here is what it looks like.  You will need to put in

    1. Which broker you are using
    2. The name of your Topic

    Resources for Project

    You can find this completed project in your project creator dialog by filtering for “IoT Expert Embedded”.  This is lesson 4

    You can also clone this project at git@github.com:iotexpert/ew21-lesson4.git or https://github.com/iotexpert/ew21-lesson4

    Here is the final cloud_task.c

    #include <stdio.h>
    #include "FreeRTOS.h"
    #include "task.h"
    #include "queue.h"
    #include "cy_wcm.h"
    #include "cy_mqtt_api.h"
    #include "cloud_task.h"
    #define CLOUD_WIFI_AP        "ew2021"
    #define CLOUD_WIFI_PW        "ew2021ap"
    #define CLOUD_WIFI_SECURITY  CY_WCM_SECURITY_WPA2_AES_PSK
    #define CLOUD_WIFI_BAND      CY_WCM_WIFI_BAND_ANY
    #define CLOUD_MQTT_BROKER        "mqtt.eclipseprojects.io"
    #define CLOUD_MQTT_CLIENT_PREFIX "remote"
    #define CLOUD_MQTT_TOPIC         "motor_speed"
    #define MOTOR_KEY                "motor"
    static QueueHandle_t motor_value_q;
    static cy_mqtt_t mqtthandle;
    static void cloud_connectWifi();
    static void cloud_startMQTT();
    static void cloud_mqtt_event_cb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data);
    static void cloud_publishMessage(char *topic,char *message);
    void cloud_task(void* param)
    {
    (void)param;
    cloud_connectWifi();
    cloud_startMQTT();
    motor_value_q = xQueueCreate(1,sizeof(uint32_t));
    for(;;)
    {
    int motorSpeed;
    char message[32];
    xQueueReceive(motor_value_q, &motorSpeed, portMAX_DELAY);
    snprintf(message, sizeof(message)-1, "{\"%s\":%d}",MOTOR_KEY,motorSpeed);
    cloud_publishMessage(CLOUD_MQTT_TOPIC,message);
    }
    }
    void cloud_sendMotorSpeed(int speed)
    {
    if(motor_value_q)
    xQueueSend(motor_value_q,&speed,0);
    }
    static void cloud_connectWifi()
    {
    cy_rslt_t result;
    cy_wcm_connect_params_t connect_param = {
    .ap_credentials.SSID = CLOUD_WIFI_AP,
    .ap_credentials.password = CLOUD_WIFI_PW,
    .ap_credentials.security = CLOUD_WIFI_SECURITY,
    .static_ip_settings = 0,
    .BSSID = {0},
    .band = CLOUD_WIFI_BAND,
    };
    cy_wcm_config_t config = {.interface = CY_WCM_INTERFACE_TYPE_STA}; // We are a station (not a Access Point)
    cy_wcm_init(&config); // Initialize the connection manager
    printf("\nWi-Fi Connection Manager initialized.\n");
    do
    {
    cy_wcm_ip_address_t ip_address;
    printf("Connecting to Wi-Fi AP '%s'\n", connect_param.ap_credentials.SSID);
    result = cy_wcm_connect_ap(&connect_param, &ip_address);
    if (result == CY_RSLT_SUCCESS)
    {
    printf("Successfully connected to Wi-Fi network '%s'.\n",
    connect_param.ap_credentials.SSID);
    // Print IP Address
    if (ip_address.version == CY_WCM_IP_VER_V4)
    {
    printf("IPv4 Address Assigned: %d.%d.%d.%d\n", (uint8_t)ip_address.ip.v4,
    (uint8_t)(ip_address.ip.v4 >> 8), (uint8_t)(ip_address.ip.v4 >> 16),
    (uint8_t)(ip_address.ip.v4 >> 24));
    }
    else if (ip_address.version == CY_WCM_IP_VER_V6)
    {
    printf("IPv6 Address Assigned: %0X:%0X:%0X:%0X\n", (unsigned int)ip_address.ip.v6[0],
    (unsigned int)ip_address.ip.v6[1], (unsigned int)ip_address.ip.v6[2],
    (unsigned int)ip_address.ip.v6[3]);
    }
    break; /* Exit the for loop once the connection has been made */
    }
    else
    {
    printf("WiFi Connect Failed Retrying\n");
    vTaskDelay(2000); // wait 2 seconds and try again;
    }
    } while (result != CY_RSLT_SUCCESS);
    }
    static void cloud_startMQTT()
    {
    static cy_mqtt_connect_info_t    	connect_info;
    static cy_mqtt_broker_info_t     	broker_info;
    static uint8_t buffer[1024];
    cy_rslt_t result;
    result = cy_mqtt_init();
    broker_info.hostname = CLOUD_MQTT_BROKER;
    broker_info.hostname_len = strlen(broker_info.hostname);
    broker_info.port = 1883;
    result = cy_mqtt_create( buffer, sizeof(buffer),
    NULL, &broker_info,
    cloud_mqtt_event_cb, NULL,
    &mqtthandle );
    CY_ASSERT(result == CY_RSLT_SUCCESS);
    static char clientId[32];
    srand(xTaskGetTickCount());
    snprintf(clientId,sizeof(clientId),"%s%6d",CLOUD_MQTT_CLIENT_PREFIX,rand());
    memset( &connect_info, 0, sizeof( cy_mqtt_connect_info_t ) );
    connect_info.client_id      = clientId;
    connect_info.client_id_len  = strlen(connect_info.client_id);
    connect_info.keep_alive_sec = 60;
    connect_info.will_info      = 0;
    connect_info.clean_session = true;
    result = cy_mqtt_connect( mqtthandle, &connect_info );
    CY_ASSERT(result == CY_RSLT_SUCCESS);
    printf("MQTT Connect Success to %s Client=%s\n",CLOUD_MQTT_BROKER,clientId);
    }
    static void cloud_mqtt_event_cb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data )
    {
    cy_mqtt_publish_info_t *received_msg;
    printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
    (void)user_data;
    switch( event.type )
    {
    case CY_MQTT_EVENT_TYPE_DISCONNECT :
    if( event.data.reason == CY_MQTT_DISCONN_TYPE_BROKER_DOWN )
    {
    printf( "\nCY_MQTT_DISCONN_TYPE_BROKER_DOWN .....\n" );
    }
    else
    {
    printf( "\nCY_MQTT_DISCONN_REASON_NETWORK_DISCONNECTION .....\n" );
    }
    break;
    case CY_MQTT_EVENT_TYPE_PUBLISH_RECEIVE :
    received_msg = &(event.data.pub_msg.received_message);
    printf( "Incoming Publish Topic Name: %.*s\n", received_msg->topic_len, received_msg->topic );
    printf( "Incoming Publish message Packet Id is %u.\n", event.data.pub_msg.packet_id );
    printf( "Incoming Publish Message : %.*s.\n\n", ( int )received_msg->payload_len, ( const char * )received_msg->payload );
    break;
    default :
    printf( "\nUNKNOWN EVENT .....\n" );
    break;
    }
    }
    static void cloud_publishMessage(char *topic,char *message)
    {
    cy_mqtt_publish_info_t  pub_msg;
    pub_msg.qos = CY_MQTT_QOS0;
    pub_msg.topic = topic;
    pub_msg.topic_len = strlen(pub_msg.topic);
    pub_msg.payload = message;
    pub_msg.payload_len = strlen(message);
    cy_mqtt_publish( mqtthandle, &pub_msg );
    printf("Published to Topic=%s Message=%s\n",topic,message);
    }
    

    EW21: Lesson 5: Low Power

    Embedded World 2021 - Infineon ModusToolbox PSoC 6 Drone 

     

    Summary

    The FreeRTOS template we started with already enables low power mode transitions such as Sleep and Deep Sleep. In lesson 5 we will update our remote control project to have even lower power as remote controls are almost always driven by batteries.  We will do this by using lower power regulators, slowing down clocks, and by offloading some functionality to the WiFi chip. This will be the final lesson on the remote control.  Here is the architecture:

    Learning Objectives

    This lesson will show you two important features of Modus Toolbox.

    1. How to reduce system power using the Low Power Assistant
    2. How to create a custom board support package

    Procedure

    1. Create your project
    2. Create a custom Board Support Package
    3. Modify the Makefile to use the new BSP
    4. Add the Low Power Assistant to the project
    5. Use the Configurator to setup Low Power
    6. Examine FreeRTOSConfig.h
    7. Test the project

    1. Create the project

    Start the project creator and make a copy of your 4th project.

    Call it Lesson_5

    2. Override the BSP Configuration

    Make a directory called “COMPONENT_CUSTOM_DESIGN_MODUS” to contain the configuration files that will override the configuration from the BSP.  This will be brought into the project automatically by the build system.

    Note: You can also create a complete custom BSP which allows changing things like linker scripts, startup code, etc. See the ModusToolbox User Guide for details on how to do that.

    Make a directory called “TARGET_CY8CPROTO-062-4343W” in your project.  This directory will hold the custom BSP files.

    Copy and paste the configuration files from the Modus Toolbox BSP directly into your project.

    Now your project should have these files:

    3. Modify the Makefile

    You need to

    1. Enable your new BSP
    2. Disable the existing BSP

    Do this by adding to the COMPONENTS and DISABLE_COMPONENTS line in the Makefile

    COMPONENTS= FREERTOS LWIP MBEDTLS SECURE_SOCKETS CUSTOM_DESIGN_MODUS WCM
    DISABLE_COMPONENTS=BSP_DESIGN_MODUS

    4. Add the Low Power Assistant to the project

    Start the Library Manager and add the LPA (Low Power Assistant) library to your project

    5. Use the configurator to Setup for Low Power

    Set the System Active Power Mode to ULP and enable the “Normal Current Buck”.

    Notice that the System Idle Power Mode is already set to System Deep Sleep. This means that when the RTOS is not busy, it will attempt to enter Deep Sleep mode.

    Turn down the FLL to 50 Mhz

    Lower the frequency of the peripherals by cutting the divider in half.

    In order to save power the host can sleep and wait for WiFi packets.  The WiFi chip needs to be able to wakeup the host PSoC.  Enable P0[4] as the wakeup pin and turn on the interrupt.

     

    Click on the CYW4343WKUGB tab.  Enable the host wakeup option.  Set the pin to P0[4].  Enable ARP off loads.  You can also look through the other low power offload filters.

    6. Examine FreeRTOSConfig.h

    Along with the System Idle Power Mode in the Configurator, this block of code tells FreeRTOS that when the system is idle that it should go to sleep.

    The function vApplicationSleep is in the abstraction-rtos library and it allows the system to go to sleep and wake up cleanly in the context of FreeRTOS.

    #if CY_CFG_PWR_SYS_IDLE_MODE == CY_CFG_PWR_MODE_SLEEP || CY_CFG_PWR_SYS_IDLE_MODE == CY_CFG_PWR_MODE_DEEPSLEEP
    extern void vApplicationSleep( uint32_t xExpectedIdleTime );
    #define portSUPPRESS_TICKS_AND_SLEEP( xIdleTime ) vApplicationSleep( xIdleTime )
    #define configUSE_TICKLESS_IDLE  2
    #endif
    #if CY_CFG_PWR_DEEPSLEEP_LATENCY>0
    #define configEXPECTED_IDLE_TIME_BEFORE_SLEEP CY_CFG_PWR_DEEPSLEEP_LATENCY
    #endif
    

    7. Test

    Program and test to make sure that things still work.  If you have a DMM plug it in and find out how much power you saved.

    Resources for Project

    You can find the completed project in your project creator dialog by filtering for “IoT Expert Embedded”.  This is lesson5

    You can also clone this project at git@github.com:iotexpert/ew21-lesson5.git or https://github.com/iotexpert/ew21-lesson5

    EW21: Lesson 6: Motor Control

    Embedded World 2021 - Infineon ModusToolbox PSoC 6 Drone 

     

    Summary

    In lesson 6 we will turn our attention to the drone. Everything on the drone side is almost exactly the same as remote control, so I will copy all of that code to start with then add the motor controller.  At the end of lesson one you will have the functioning drone controller.  Here is the architecture:

    Learning Objectives

    1. Retargeting a project to a different board
    2. Using the Infineon BLDC Motor Controller
    3. Subscribing to a MQTT topic

    Procedure

    1. Make a new project
    2. Rip out the joystick_task.c/h and fix main.c
    3. Add the motor driver library
    4. Create motor_task.h
    5. Create motor_task.c
    6. Update the CapSense task to submit to the motor_task
    7. Fix the Cloud Task to perform MQTT subscribe instead of publish
    8. Fix main.c to start the motor task
    9. Test

    1. Make a new project

    In order to control the BLDC motor I am going to a development kit that has Arduino headers, the CY8CKIT-062S2-43012.  Start your project from that BSP.

    Our project is basically the same as Lesson4, so import that as a template.

    Don’t sully your project’s name, pick something good.

    2. Rip out the joystick_task

    This project will not have the joystick task.  Nuke it! Don’t forget to remove the task start from main.c

    3. Add the Motor Driver Library and Remove the Magnetic Sensor

    For this project we will be talking to the TLE9879 BLDC motor shield.  I provide you a driver library for that.  And we won’t need the 3-D Magnetic sensing library any more.

    4. Create motor_task.h

    Create a motor_task.h  This will have the definition of the task, plus a function to set the motor speed by submitting to a queue.

    #pragma once
    void motor_task(void* param);
    void motor_update(int speed);
    

    5. Create motor_task.c

    The motor task needs some include files.  This task will also use a queue to manage other tasks (cloud and CapSense) that will submitting data to change the speed of the motor.

    #include "cybsp.h"
    #include "cyhal.h"
    #include "cycfg.h"
    #include "FreeRTOS.h"
    #include "task.h"
    #include "queue.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include "motor_task.h"
    #include "tle9879_system.h"
    static QueueHandle_t motor_value_q;

    I will define a few constants that will help me map 0-100% into 0-5500 RPM (the max for this motor). The motor needs to run at a decent speed to start up properly so we will map 10% to 1000 RPM. Below 10%, the motor will be off.

    /*******************************************************************************
    * Global constants
    *******************************************************************************/
    #define		RPM_CHANGE_INTERVAL		(100)
    #define		RPM_CHANGE_RATE			(10)
    #define		RPM_PERCENT_MIN			(10)
    #define  	RPM_MAX 				5500.0
    #define  	RPM_MIN 				1000.0
    

    The task is pretty simple, but with one trick.  Apparently you ramp changes in motor speed, so I use a timeout in the message queue to give me a periodic change.  The task is

    1. Start the shield
    2. Then wait for a new value in the queue (or the timeout)
    3. If it is a new value then change the desired value
    4. If the current value is not the same as the desired value then take a step towards the desired value
    void motor_task(void* param)
    {
    (void)param;
    tle9879_sys_t tle9879_sys;
    uint8_t numberOfBoards = 1;
    BaseType_t rtos_api_result;
    motor_value_q = xQueueCreate(1,sizeof(int));
    /* Initialize and configure the motor driver */
    tle9879sys_init(&tle9879_sys,
    CYBSP_D11,
    CYBSP_D12,
    CYBSP_D13,
    NULL,
    CYBSP_D4,
    CYBSP_D5,
    CYBSP_D6,
    CYBSP_D7,
    &numberOfBoards);
    if (tle9879_sys.board_count == 0)
    {
    printf("Motor board not detected. Exiting Motor task.\n");
    vTaskDelete(NULL);
    }
    tle9879sys_setMode(&tle9879_sys, FOC, 1, false);
    bool motorState=false;
    int currentPercentage=0;
    int desiredPercentage=0;
    while(1)
    {
    rtos_api_result = xQueueReceive(motor_value_q, &desiredPercentage, RPM_CHANGE_INTERVAL);
    /* Value has been received from the queue (i.e. not a timeout) */
    if(rtos_api_result == pdTRUE)
    {
    if(desiredPercentage < RPM_PERCENT_MIN) /* Any value less than 10% will result in stopping the motor */
    desiredPercentage = 0;
    if(desiredPercentage>100)
    desiredPercentage = 100;
    }
    if(currentPercentage != desiredPercentage)
    {
    if(abs(currentPercentage-desiredPercentage) < RPM_CHANGE_RATE)
    currentPercentage = desiredPercentage;
    if (currentPercentage < desiredPercentage)
    currentPercentage = currentPercentage + RPM_CHANGE_RATE;
    if (currentPercentage > desiredPercentage)
    currentPercentage = currentPercentage - RPM_CHANGE_RATE;
    if(currentPercentage>0 && motorState==false)
    {
    tle9879sys_setMotorMode(&tle9879_sys, START_MOTOR, 1);
    motorState = true;
    }
    if(currentPercentage == 0 && motorState==true)
    {
    tle9879sys_setMotorMode(&tle9879_sys, STOP_MOTOR, 1);
    motorState = false;
    }
    float motorSpeed = ((float)(currentPercentage-RPM_PERCENT_MIN))/(float)(100.0-RPM_PERCENT_MIN) * (RPM_MAX - RPM_MIN) + RPM_MIN;
    tle9879sys_setMotorSpeed(&tle9879_sys, motorSpeed, 1);
    printf("Current %d%% Desired=%d%% Speed=%f\n",currentPercentage,desiredPercentage,motorSpeed);
    }
    }
    }

    In order to simplify the CapSense and cloud tasks I provide a function to submit a new percent to the queue.

    void motor_update(int speed)
    {
    if(motor_value_q)
    xQueueOverwrite(motor_value_q,&speed);
    }
    

    6. Update the CapSense task to submit to the motor_task

    Lets fix the CapSense task to submit to the motor queue.  First fix the include.

    #include "motor_task.h"

    Then update the buttons to use the submit function

    if((0u != button0_status) && (0u == button0_status_prev))
    {
    printf("Button 0 pressed\n");
    motor_update(0); // Stop the Motor
    }
    if((0u != button1_status) && (0u == button1_status_prev))
    {
    printf("Button 1 pressed\n");
    motor_update(75); // Set the motor to 75%
    }
    

    Fix the slider to use the submit function as well.

    if((0u != slider_touched) && (slider_pos_prev != slider_pos ))
    {
    printf("Slider position %d\n",slider_pos);
    motor_update(slider_pos);
    }
    

    7. Fix the Cloud Task

    Remember that we copied our cloud task from the remote control.  It publishes MQTT messages.  We need to fix the cloud task to subscribe instead of publish.   If you recall from Lesson 4 we submit the messages as JSON.  To decode the JSON I will use a JSON parser.

    #include "cy_json_parser.h"

    The function prototypes are almost the same, except for a subscribe instead of publish, plus a JSON callback

    static void cloud_connectWifi();
    static void cloud_startMQTT();
    static void cloud_subscribeMQTT();
    static void cloud_mqtt_event_cb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data);
    static cy_rslt_t json_cb(cy_JSON_object_t *json_object, void *arg);
    

    The cloud task is much simpler this time.  connect, start, subscribe and then wait for some action.

    void cloud_task(void* param)
    {
    (void)param;
    cloud_connectWifi();
    cloud_startMQTT();
    cloud_subscribeMQTT();
    for(;;)
    {
    vTaskSuspend(NULL);
    }
    }

    To subscribe you just setup a subscribe info structure.  Then call the subscribe.  You also need to configure the JSON parser.

    static void cloud_subscribeMQTT()
    {
    cy_rslt_t result;
    cy_mqtt_subscribe_info_t    sub_msg[1];
    /* Subscribe to motor speed MQTT messages */
    sub_msg[0].qos = 0;
    sub_msg[0].topic = CLOUD_MQTT_TOPIC;
    sub_msg[0].topic_len = strlen(sub_msg[0].topic);
    result = cy_mqtt_subscribe( mqtthandle, sub_msg, 1 );
    CY_ASSERT(result == CY_RSLT_SUCCESS);
    printf("Subscribe Success to Topic %s\n",CLOUD_MQTT_TOPIC);
    /* Register JSON callback function */
    cy_JSON_parser_register_callback(json_cb, NULL);
    }
    

    When you get a callback from the MQTT, you will look and see which topic.  If it matches the motor topic then call the JSON parser.

    static void cloud_mqtt_event_cb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data )
    {
    cy_mqtt_publish_info_t *received_msg;
    printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
    (void)user_data;
    switch( event.type )
    {
    case CY_MQTT_EVENT_TYPE_DISCONNECT :
    if( event.data.reason == CY_MQTT_DISCONN_TYPE_BROKER_DOWN )
    {
    printf( "\nCY_MQTT_DISCONN_TYPE_BROKER_DOWN .....\n" );
    }
    else
    {
    printf( "\nCY_MQTT_DISCONN_REASON_NETWORK_DISCONNECTION .....\n" );
    }
    break;
    case CY_MQTT_EVENT_TYPE_PUBLISH_RECEIVE :
    received_msg = &(event.data.pub_msg.received_message);
    printf( "Incoming Publish Topic Name: %.*s\n", received_msg->topic_len, received_msg->topic );
    printf( "Incoming Publish message Packet Id is %u.\n", event.data.pub_msg.packet_id );
    printf( "Incoming Publish Message : %.*s.\n\n", ( int )received_msg->payload_len, ( const char * )received_msg->payload );
    if(memcmp(received_msg->topic, CLOUD_MQTT_TOPIC, strlen(CLOUD_MQTT_TOPIC)) == 0) /* Topic matches the motor speed topic */
    {
    cy_JSON_parser(received_msg->payload, received_msg->payload_len);
    }
    break;
    default :
    printf( "\nUNKNOWN EVENT .....\n" );
    break;
    }
    }

    If the JSON parser matches, you call the motor_update function with the new requested speed.

    /* This is the callback from the cy_JSON_parser function. It is called whenever
    * the parser finds a JSON object. */
    static cy_rslt_t json_cb(cy_JSON_object_t *json_object, void *arg)
    {
    int motorSpeed;
    if(memcmp(json_object->object_string, MOTOR_KEY, json_object->object_string_length) == 0)
    {
    if(json_object->value_type == JSON_NUMBER_TYPE)
    {
    /* Add null termination to the value and then convert to a number */
    char resultString[json_object->value_length + 1];
    memcpy(resultString, json_object->value, json_object->value_length);
    resultString[json_object->value_length] = 0;
    motorSpeed = (uint8_t) atoi(resultString);
    printf("Received speed value from cloud: %d\n", motorSpeed);
    motor_update(motorSpeed);
    }
    }
    return CY_RSLT_SUCCESS;
    }
    

    8. Fix main.c to start the motor task

    In main.c you need to “do the needful” starting with an include.

    #include "motor_task.h"

    Then starting the motor task.

    xTaskCreate(motor_task,    "Motor"     ,configMINIMAL_STACK_SIZE*8  , NULL, 1, 0);
    

    9. Test

    Resources for Project

    You can find the completed project in your project creator dialog by filtering for “IoT Expert Embedded”.  This is lesson6

    You can also clone this project at git@github.com:iotexpert/ew21-lesson6.git or https://github.com/iotexpert/ew21-lesson6

    EW21: Lesson 7: WS2812 LED Strip

    Embedded World 2021 - Infineon ModusToolbox PSoC 6 Drone 

     

    Summary

    In lesson 7 we will finish this Frankenstein drone setup by adding WS2812 Strip LEDs.  At the end of lesson one you will have the “WS2812 LED” block colored green.  Here is the architecture:

    Learning Objectives

    1. Have fun by adding another cool library – WS2812

    Procedure

    1. Create a new project
    2. Add the IoT Expert WS2812 Library
    3. Modify motor_task.c
    4. Test

    1. Create a new project

    Start from the CY8CKIT-062S2-43012

    Give the project a sensible name.  In this case IoT_Expert_Embedded_World_2021_Lesson7

    2. Add the IoT Expert WS2812 Library

    You need to find the IoT Expert WS2812 library and include it into your project.

    3. Modify motor_task.c

    Include the header file for the library.

    #include "ws2812.h"

    The number of LEDs in our system

    #define		NUM_LEDS				(61)

    Define some colors to use and initialize the LED strip

    /* LED color array - 7 different colors each with RGB values */
    uint8_t ledColors[7][3] = {
    { 0,  0,  0},	// Off
    {20,  0, 30},	// Violet
    { 0,  0, 50},	// Blue
    { 0, 50,  0},	// Green
    {30, 20,  0},	// Yellow
    {42,  8,  0},	// Orange
    {50,  0,  0},	// Red
    };
    uint8_t ledColorRow = 0;
    uint8_t ledColorRowPrev = 0;
    /* Initialize LED strips */
    ws2812_init(NUM_LEDS, P10_0, P10_1, P10_2);
    

    A bit of code to send updates to the LED strip based on the status of the motor.

    /* Calculate LED color and update if it has changed */
    if(motorState == false)
    {
    /* Turn off LEDs */
    ws2812_setMultiRGB(0, NUM_LEDS-1, 0, 0, 0);
    ws2812_update();
    ledColorRowPrev = 0;
    }
    else
    {
    ledColorRow = 1 + (uint8_t)((( (uint16_t)motorSpeed - (uint16_t)RPM_MIN ) * 5) / ((uint16_t)RPM_MAX - (uint16_t)RPM_MIN)); /* Determine row to use */
    if(ledColorRowPrev != ledColorRow)
    {
    ws2812_setMultiRGB(0, NUM_LEDS-1, ledColors[ledColorRow][0], ledColors[ledColorRow][1], ledColors[ledColorRow][2]);
    ws2812_update();
    ledColorRowPrev = ledColorRow;
    }
    }

    4. Test

    Let it rip tater chip

    Here it is with the LED strips on…

    Resources for Project

    You can find the completed project in your project creator dialog by filtering for “IoT Expert Embedded”.  This is lesson 7

    You can also clone this project at git@github.com:iotexpert/ew21-lesson2.git or https://github.com/iotexpert/ew21-lesson2

    Beer Smith Water Volume Calculator

    Summary

    A discussion of the algorithms to calculate water volume in Beer Smith 3.0 including a spreadsheet showing the calculations.  What does this have to do with IoT?  Nothing.

    Story

    I am a new brewer. I started at the beginning of COVID in March 2020.  At this point I have only done 26 batches so I am a long way from expert.  For sure there is no danger of BrewExpert.com from me any time soon.  But, I am also an engineer so I like to “know” (and if there are any IoT Expert people still reading, you will already know that)

    I have been struggling to understand how much strike and sparge water to use during the making process. I own and use a Grainfather, so I have been using their software, but honestly Im not a huge fan.  Nathan at BrewerDude, my local home brew store, told me that using Beer Smith was the best way to improve my results.  And, as you know figuring things out is more than half the fun for me.  Game on.

    Beer Smith 3.0 is a interesting piece of software written by Brad Smith that was made to design and help you implement beer recipes.  The software was clearly built with years of “experience” in making beer and is also clearly a reflection of his workflow.  The tool is seems to have a substantial amount of empirical knowledge built into the tool – Ohm’s laws for beer?

    Now to the problem.  I started trying to reconcile the water volume information coming out of Beer Smith with what was coming out of the Grainfather software.  The numbers didn’t really add up and there were a bunch of things which I didn’t really know. Google here we come. Turns out there are a boatload hits that are some variant of “how do I match what the Grainfather and BeerSmith think for water volumes”.  I am sure that there is a beautiful document in the Beer Smith documentation library which I haven’t found which explains the answer to the question of how the water volumes are calculated in Beer Smith, but I couldn’t find it.

    So I decided to reverse engineer the calculations, which I mostly have done (with a few issues).  They are all in a spreadsheet which you can download from the IoT Expert Github respository which you can find at https://github.com/iotexpert/Beer.git That repository contains a spreadsheet called “beer-smith-water.xlsx” which has my version of the formulas which seem to match BeerSmith 3.1.08

    I am quite sure that this water volume issue is obvious to everyone, except me.  But, as I said, I’m only 26 batches into this brewing adventure.  For the rest of this article I will focus only on the parameters that impact water volume in Beer Smith.

    First I will walk through the screens that impact water then I will show the “Vols” tab and my spreadsheet which mimics the calculations.

    Getting Started

    I will use the recipe “Abbey of the Blonds” from BrewerDude and my setup which is a Grainfather G30 and a Grainfather Conical Fermenter.  Here is a picture of the front cover of the book that came with the recipe.  Notice my handwritten notes that are the strike and sparge water… in both metric and imperial… and my note that the water values that I used to get a good result.

    Before you can really look at the water volume numbers you need to do several things:

    1. Enter the recipe (which I copied from the nice document that came from BrewerDude along with the kit)
    2. Add your equipment (in the picture you can see that I added Grainfather G30 110V ARH).  “ARH” are my initials and is my copy of the default configuration.
    3. Add your mash profile (in the picture you can see that I picked Single Infusion Medium Body, Batch Sparge)
    4. Set the boil time to 90 minutes (which over-rides the default of this equipment of 60 minutes)
    5. Notice the Batch Size of 5.5 gallons (which was copied from the equipment profile)

    In the picture you can see that I changed the fields in the middle right to display the three water volume numbers that I am interested in, specifically:

    • Tot Mash Water
    • Sparge Water
    • Total Volume

    I do not believe that BH Efficiency impacts the water volume calculations.

    Batch Size & A GUI Comment

    The starting value for Batch Size is copied from Equipment Setup configuration.  This number was intensely confusing to me for a good while until I got it sorted out that this is the total volume of cold wort going into the fermenter after the mash and boil.  One thing that you should watch for is that this value is linked onto multiple menus/screens and a change in one place will effectively change it multiple places.  When you change this value it appears to propagate throughout your recipe but it does not change the original source equipment profile.

    Equipment Setup

    As I stated above, when you create the recipe you effectively COPY the equipment profile into your recipe.  In other words the values in this tab become the default values for your recipe when you configure this equipment profile to be used for your recipe.

    In the equipment profile the following values impact water:

    • Batch Volume – How much wort you want to go into the fermenter
    • Fermenter Loss – Dead space at bottom of the fermenter i.e. below the line where you bottle from
    • Mash Tun Volume – Used as a check to make sure you don’t overflow during the mash.  This doesn’t change the calculations, just a warning.
    • Mash Tun Weight – No impact on water? (pretty sure)
    • Mash Tun Specific Heat – No impact on water? (pretty sure)
    • Boil Time – The default length of time to boil.  This is copied into your reciepe.  This parameter is used in the boil off calculation
    • Boil Off – The input variable to the total boil off calculation
    • Use boil as an hourly rate (if you click this boil off will vary with the boil time)
    • Total Boil Off: Calculated boil off = Batch Size * boil time / 60 * Boil Off
    • Recoverable Mash Deadspace – volume of water below the false bottom which WILL be part of the final wort
    • Mash Deadspace Losses – the volume of water below the false bottom which willl be LOST and not part of the final wort
    • Top Up Water for Kettle: How much water you add pre-boil
    • Post Boil Vol: This is a calculated estimate based on the input parameters (more details later)
    • Cooling Shrinkage: how much volume you loose as the wort cools
    • Loss to Trub and Chiller – what it says
    • Top Up Water – How much water you add into the fermenter

    You will find that most of these values are copied onto the “Vols” screen later on.

    Mash Profile

    When you choose the mash profile it is the major input to the strike water calculations.

    On the Mash screen the parameters:

    1. Grain Weight Basis – used for the calculation of strike water (derived from your inputs on the recipe)
    2. Mash In – will take you to the next screen where the strike water is calculated

    I was lost trying to figure out where the strike water came from for quite a while until I realized that you could change the values on the “Mash In” step screen.  To do this click on it and press “Edit Step” (where the water volume action happens).  Really what is happening here is that you can create multiple steps in your mash process.  Each step can/will impact the water used by that step.  When you configure your reciepe to use a mash profile, you are essentially copying those steps into your recipe and they drive the values for strike water in your recipe.

    As I work to write this article I notice for the first time “Batch Sparge using batches that fill 90%” but I have no idea what that impacts.

    The Mash Step screen calculates the mash water for this step.  The key metric is the Water/Grain Ratio which is used to calculate the “Water to add”.  Where did the 1.250 qt/lb come from?  I suspect that this is empirical.  Notice that the water to add does NOT include the Mash Tun Deadspace Addition.  This number was copied from the Equipment profile (which recall was 0.92 gallon = 3.68qt).  Im sure that there was a good reason to add a place to enter this value here, but damn if I can figure it out.  I’m also sure there was some good reason to change units, but Im also not sure why, but probably convenience.

    The Advanced Options

    The advanced options screen (which can be accessed from the preferences button) has two numbers which are relevant to the water calculations

    1. Grain Absorption (how much water is absorbed by the grain) = 0.96 Fluid Oz per Oz of Grain
    2. Grain Volume (how much physical space is taken by the wet grain) = 0.6520 l/kg

    Im quite sure that both of these numbers were arrived at empirically.  These numbers are used in the upcoming “Vols” calculations.

    The “Vols” Tab

    This tab is where the action happens.  You can see and edit most of what you might want from a water volume point of view.  This screen is broken up into four sections which I will walk through one by one.

    1. Water Needed
    2. Mash
    3. Boil and Fermentation
    4. Fermentation/Bottling

    The first thing you should know on the “Vols” tab is that the Total Volume = Tot Mash Water + Sparge Vol and is calculated for you.

    The next section is Mash

    The Mash Grain Wt (weight) comes from your recipe.

    Grain Absorption = Mash Grain Wt * Grain Absorption (from the advanced options)

    The Tot Mash Water comes from the Mash Profile which you set earlier plus the Mash Tun Addition.  In this specific case it was calculated with

    • 10.88 pounds * 1.25 qts/pound * 1 gallon/4 quarts = 3.4 Gallons + 0.92 Gallons = 4.32 Gallons

    The Tun Deadspace is a parameter for you to enter and is the unrecoverable amount of fluid in the mash tun (things below the pickup in the bottom of the Grainfather)

    The Mash Volume Needed = Total Mash Water + Volume of the Wet Grain (which isn’t shown on this screen but is on my spreadsheet)

    The Volume of the Wet Grain = Grain weight * gallons/pound  Remember from the advanced tab which had Grain Volume in litres/pound.  This is how much space is taken up inside of your mash tun for wet grain.

    Water Available from the Mash = Total Mash Waster – Grain Absorption (this is how much wort can move to the next step)

    The Sparge Vol comes from a long chain of calculations on the next sections…

    Here is the section of my spreadsheet which represents these calculations.

    Now I am going to skip the Boil and Fermentation section and come back because the Fermentation/Bottling section uses data from the this section as an Input.

    Top Up Water is how much water you add to the boiled wort as you add it into the Fermenter.  I am not sure why you would do this, perhaps to lower the gravity?

    The Batch Size is a parameter that came from the recipe page and is all over this software.  This value is how much wort you are going to put into the fermenter.

    The bottling/fermentation loss is how much you loose at the bottom of your fermenter and by taking samples or draining crap during the fermentation process.

    I don’t know what “Starter Size Used” is, but I believe that it was put there for a Yeast starter (I have been using Propper Starter).  This would add 32 Ozs (aren’t the units fun here) or 0.94 of a liter.  On a semi-related note I wonder why they addition of Wort doesn’t change the gravity enough to add into their calculations … I suppose adding 16oz at 1.040 isn’t material.  (late edit: I did the math and adding 16oz of 1.040 to a 5.5 gallon batch of 1.060 lower the gravity by 0.04%)

    Here is the Fermentation section from my spreadsheet.  Notice that in the picture there are two yellow fields for you to override the calculations from that step.

    Back to the  “Boil and Fermentation” section.

    Kettle Top Up is how much water you add before you boil after the mash (I have no idea why you would do this)

    The best way to think about the rest of this is to

    1. Remember that we are trying to back calculate Sparge Water
    2. Start from the bottom and work back to the top

    Trub loss is what it say it is.  With this you can now calculate a value which isn’t shown on this screen.  I call that value “Post Cool Volume”

    Post Cool Volume = Batch Size + Trub Loss – Kettle Top Up

    In order to achieve the “Post Cool Volume” you need to increase the work volume by the amount of Cooling Shrinkage.

    Pre Cool Volume = Post Boil Volume / (1-Pct Cool Loss)

    Cooling Shrinkage = Pre Cool Vol – Post Cool Volume

    I suppose that I was surprised that the number was anything other than 0 as I thought that liquids dont change their volume very much.  Shows what an electrical engineer knows eh.

    Est Pre-Boil Vol = Pre Cool Volume / ( 1 – (Evaporation Rate * Boil Time / 60 Minutes))

    With that you can now calculate Boil Off = Pre  boil – Post Boil

    Finally Sparge Volume = Est Pre Boil – Kettle Top Up + Grain Absorption + Tun Deadspace – Tot Mash Water

    Here is my spreadsheet:

    Final Thoughts

    I have not answered (at least) the following questions

    1. Where did the Grainfather numbers come from?
    2. Given (1) why is there a difference between Beer Smith and Grainfather
    3. What other calculators are out there?
    4. What is “Batch Sparge Using Batches that fill %” used for?

    I am pretty sure that the right thing for me to do next is to design a set of experiments that will arrive at the configuration parameters that best match my setup.  I think that it would be useful to describe this procedure.

    There may very well be an error in here.  In fact I would say with certainty that I made some error.  If you have made it this far and you spotted the error I would very much like to know what it is so that I can fix it.

    Kentucky Inspired Me … Thanks Erich

    I follow Erich’s blog.  In the last few years he has been posting pictures taken near his home in Switzerland.  Remarkable.

    In the last few days Kentucky has inspired to take some fall pictures.  I will not pretend they are great works of photography.  But I think that they are pretty.

    How are they IoT?  They aren’t.


    The Wires Are Out Of Control

    You have to start by admitting you have a problem

    OK, it looks like I’m on the right track.

    But, that made things worse in other ways.

    Really?

    Let’s make some progress on the power.

    Having a lab assistant is awesome

    Finally…

    You can see:

    1. My new work Windows laptop
    2. My personal Mac
    3. A 4K display that is switched with a KVM under the desk
    4. The Jetson Nano that I am currently working on
    5. The Thunderbolt hub connected to my Mac
    6. A beautiful antique magnifying glass
    7. A Weller soldering iron
    8. An amazing Leica microscope
    9. My Linux box (under the desk).  It is attached via display port to the monitor – but it is mostly logged into via the network
    10. A paper protractor that my daughter made when she was little
    11. A paper Go Dad flag made by my daughter when she was little
    12. UK (BSEE) & Georgia Tech (MSEE) Diplomas
    13. Part of my photojournalism portfolio
    14.  My new work phone on the wireless charger
    15. A pack of post-it-notes
    16. A paper tracing of my Katana done with the NBTHK Hozon

    Mouser PSoC 6 WiFi/BT MBED: L1 Developer Resources

    IoT Design with Cypress PSoC® 6 MCUs and Wi-Fi/Bluetooth using Arm® Mbed™

    # Lesson GitHub Project
    0 Introduction
    1 Developer Resources
    2 Your First Project & The Blinking Thread https://github.com/iotexpert/mouser-mbed-02.git
    3 Display Thread https://github.com/iotexpert/mouser-mbed-03.git
    4 Temperature Thread https://github.com/iotexpert/mouser-mbed-04.git
    5 CapSense Thread https://github.com/iotexpert/mouser-mbed-05.git
    6 WiFi & NTP Thread https://github.com/iotexpert/mouser-mbed-06.git
    7 The CY8CKIT-062-WiFi-BT https://github.com/iotexpert/mouser-mbed-07.git 
    8 Amazon AWS MQTT Thread - Part1 https://github.com/iotexpert/mouser-mbed-08.git
    9 Amazon AWS MQTT Thread - Part2 https://github.com/iotexpert/mouser-mbed-09.git

    You can “mbed import https://github.com/iotexpert/mouser-mbed-09.git“ to make a copy of the project in your workspace.

    The final architecture of the thermostat looks like this.

     

    Summary

    This is an index of links to all of the PSoC 6 MCU, CYW4343W & Mbed OS learning resources.  You can click the links to go the website or see screen captures of the resources.

    1. Mbed OS Overview (screen capture only)
    2. Mbed OS Landing Page
    3. Mbed Studio
    4. CY8CPROTO-062-4343W
    5. ModusToolbox Software Environment
    6. PSoC 6 Product Page
    7. WiFi + Bluetooth Combo Product Page
    8. PSoC 6 Documentation
    9. PSoC 6 Community
    10. Wireless Combo Community
    11. CY8CKIT-062-BT-WiFi Development Kit Product Page
    12. CY8CKIT-062-BT-WiFi Development Kit Guide
    13. PSoC 6 Datasheet
    14. CYW4343W Datasheet
    15. PSoC 6 Technical Reference Manuals
    16. PSoC 6 Application Notes
    17. WiFi + Bluetooth Combo Application Notes
    18. PSoC 6 Code Examples
    19. Video Tutorials
    20. PSoC 6 Knowledge Base
    21. Peripheral Driver Library Documentation (Doxygen – screen capture only)
    22. IoT Expert Website

    Mbed OS Overview

    Mbed OS Landing Page

    The link to the Arm Mbed landing page can be found here.

    Mbed Studio

    The link to Mbed Studio can be found here.

    CY8CPROTO-062-4343W Landing Page

    The link to the PSoC 6 WiFi-BT Prototyping Kit (CY8CPROTO-062-4343W) can be found here.

    ModusToolbox Software Environment

    The link to the ModusToolbox Software Environment landing page can be found here.

    PSoC 6 Product Page

    You can find the PSoC 6 Product landing page for PSoC 6 here

    WiFi + Bluetooth Combo Page

    You can find the landing page for all Cypress WiFi + Bluetooth combo radios here.

    PSoC 6 Documentation

    On the PSoC 6 Product Landing page there is a documentation tab that has links to all of the current documentation.

    PSoC 6 Community

    Cypress has an active development community and forum.  It can be found here.

    Wireless WiFi + Bluetooth Combo Community

    The forum on the Cypress Developer Community for WiFi + Bluetooth combo radios can be found here.

    CY8CKIT-062-WiFi-BT Development Kit Web Page

    Every Cypress development kit has a web page that contains all of the information about it, including links to the documentation and store.  The CY8CKIT-062-WiFi-BT kit page is here.

    CY8CKIT-062-WiFi-BT Development Kit Guide

    You can find the development kit guide here.

    PSoC 6 Datasheet

    The PSoC 6 Datasheet is available on Cypress.com here.

    CYW4343W Datasheet

    The CYW4343W datasheet can be found here.

    PSoC 6 Technical Reference Manual

    Each of the PSoC 6 devices has a lengthy discussion of the Technical Resources.  These documents can be found here

    PSoC 6 Application Notes

    You can get them all on our website. Here is a link to the filtered list of PSoC 6 Application Notes.

    The best application note is always the “Getting Started”.  In this case it is AN210781 “Getting Started with PSoC 6 MCU with Bluetooth Low Energy (BLE) Connectivity”

    WiFi + Bluetooth Combo Application Notes

    Here is a link to all of the WiFI Bluetooth Combo Application Notes.

    Video Tutorials

    Cypress has made a bunch of videos that take you step by step through an introduction to PSoC 6.  You can find them on the Cypress training website.

    PSoC 6 Knowledge Base

    The Cypress technical support team writes “Knowledge Base” articles when there are repeated issues reported by customers.  You can find them here.

    Peripheral Driver Library Documentation (Doxygen)

    All of the APIs in the PDL are documented in a Doxygen generated HTML document.  You can get there from

    • Help -> Peripheral Driver Library (this link is live only when you have a PSoC 6 project open)
    • Right click on a component -> Open PDL Documentation

    Mouser PSoC 6 WiFi/BT MBED: L2 The Blinking Thread

    IoT Design with Cypress PSoC® 6 MCUs and Wi-Fi/Bluetooth using Arm® Mbed™

    # Lesson GitHub Project
    0 Introduction
    1 Developer Resources
    2 Your First Project & The Blinking Thread https://github.com/iotexpert/mouser-mbed-02.git
    3 Display Thread https://github.com/iotexpert/mouser-mbed-03.git
    4 Temperature Thread https://github.com/iotexpert/mouser-mbed-04.git
    5 CapSense Thread https://github.com/iotexpert/mouser-mbed-05.git
    6 WiFi & NTP Thread https://github.com/iotexpert/mouser-mbed-06.git
    7 The CY8CKIT-062-WiFi-BT https://github.com/iotexpert/mouser-mbed-07.git 
    8 Amazon AWS MQTT Thread - Part1 https://github.com/iotexpert/mouser-mbed-08.git
    9 Amazon AWS MQTT Thread - Part2 https://github.com/iotexpert/mouser-mbed-09.git

    You can “mbed import https://github.com/iotexpert/mouser-mbed-09.git“ to make a copy of the project in your workspace.

    The final architecture of the thermostat looks like this.

     

    Summary

    Here we are again. The blinking LED.  I always start with a basic Blinking LED project just to make sure that everything is working.  What is everything?  The whole tool flow, from the editor, to the programmer, to the chip to the SDK.  The only thing that will be a little different than usual is that I will run the blinking LED in a thread by itself. This is useful for looking at the development kit and knowing that the RTOS is still running and the blinking LED thread is at least OK even if the rest of your program is trashed.

    In each of the following lessons I will add on a new block (or two) into the architecture.  The blocks colored “green” will be the ones that are done in that lesson.  In future lessons the blocks that are blue will be from the previous lessons.

    For all of these projects I will be using the Mbed Studio program to create, edit, build and program the development board.

    To implement the blinking LED thread I will:

    1. Make a new Mbed OS Program from the blank template
    2. Create & code “blinkThread.h”
    3. Create & code “blinkThread.cpp”
    4. Update main.cpp
    5. Give a Tour of Targets
    6. Compile and Program

    Make a new Mbed OS Program from the blank template

    Start Mbed Studio.  Then run “File->New Program…”

    Mbed Studio will then give you the ability to start with a template project.  To get started use “empty Mbed OS program”.  For each of the projects I will give it a name that corresponds to the lesson number, in this case “mouser-mbed-02”.  When you have all that selected click “Add Program”.

    This will create a brand new project for you with Mbed OS setup and a blank main.cpp.

    Create & code “blinkThread.h”

    In each of the following lessons I will be creating different threads to perform the different pieces of system functionality.  I will put each thread in a separate set of files with the name “functionThread.h” and “functionThread.cpp”.  As I am sure everyone knows, the “dot h” file is supposed to be the public interface to the “dot cpp” file.  In other words that file has all of the functions, objects and variables that other files are allowed to access.

    To create a file you “right click” on the project and select “New File”

    And then give the file a name.  In this case it will be “blinkThread.h”.

    This file will have the guards, to keep it from being included more than once, and just the function prototype for the thread.  In all of the coming lessons I will name the thread “functionThread”.  In this case the function is “blink” so the thread is called “blinkThread”

    #ifndef BLINK_THREAD_H
    #define BLINK_THREAD_H
    void blinkThread();
    #endif
    

    Create & code “blinkThread.cpp”

    Now I am ready to write the actual function which acts as the blinking LED thread.  Right click on the project and select “New File”

    Then give it the name “blinkThread.cpp”.  Just like the “dot h” files, the “dot cpp” files will be named “functionThread.cpp”.  In this case the function is blink so the file will be called “blinkThread.cpp”.

    This is a really simple thread.  It wants to talk to the LED on the board.  All Mbed OS development boards are required to have at least one LED and that LED must be named “LED1”.  In reality the LED1 is just a map to the PSoC pin name of “P1_1” (this happens in the BSP/Target – more on this later).

    By creating an object of type “DigitalOut” with an initializer argument of “LED1” I will have access to writing 1’s and 0’s to that pin.  Notice that I give the object definition the keyword “static” to limits its scope to this file.

    The next thing that I need is the actual function which serves as the thread.  Notice that it is an infinite loop, which just does the following:

    1. Inverts led1 by reading the pin, inverting it with the “!”, and then writing it back to the pin.

    2. Doing a delay of 500 ms using the Mbed OS library function “sleep_for”.  For those of you who aren’t CPP people the “ThisThread::” just tells the compiler that the sleep_for function is in the namespace “ThisThread”.

    #include "mbed.h"
    #include "blinkThread.h"
    static DigitalOut led1(LED1);
    void blinkThread()
    {
    while (true) 
    {
    led1 = !led1;
    ThisThread::sleep_for(500);
    }
    }
    

    Update main.cpp

    In each of the following lessons, each time I create a new file, I will be making the same basic change to main.cpp.  Specifically I will startup the thread when the main function starts.  To do this:

    1. Include the new “functionThread.h” (in this case “blinkThread.h”).
    2. Declare an object to hold the new thread (in this case “blinkThreadHandle”).
    3. Start the thread by calling the start method with a function pointer to the actual thread function (in this case “blinkThread”).
    #include "mbed.h"
    #include "blinkThread.h"
    Thread blinkThreadHandle;
    int main()
    {
    printf("Started System\n");
    blinkThreadHandle.start(blinkThread);
    }
    

    Targets Tour

    Mbed OS abstracts all information about a development board into a set of files called the Target. These files reside in the mbed-os/targets directory.  This files are classically called the board support package (BSP).  This including information about which chip, startup code, libraries, and peripherals exist on the board.

    In order to build a project you need to tell Mbed OS which target you are using.  This can be done in the Mbed CLI with  “mbed config target CY8CPROTO_062_4343W” or by selecting the correct target in the Mbed Studio IDE.

    You should look in the following directories/files:

    1. mbed-os
    2. targets directory
    3. targets.json
    4. TARGET_Cypress directory
    5. One directory per Target

    Compile and Program

    There are several things that you should notice on the panel to the left of your code.

    1. The active program (this is a drop down menu with each project in your workspace).
    2. Which “target”.  When you plug in the development kit, Mbed Studio will recognize that you have attached a target that it recognizes.
    3. The “hammer” button runs the Build process.
    4. The “play” button runs a “Build” then programs the development kit.

    Press the “Play” button to Compile and Program.  And with any luck you should have a blinking LED.

    Notice that it compiles a bunch of different files… actually all of the files in mbed-os.  This will take a while on a PC, but is much faster on a Mac or Linux.  This full compile step only needs to happen the first time for a project – each subsequent time will only compile your changes.

    Mouser PSoC 6 WiFi/BT MBED: L3 Display Thread

    IoT Design with Cypress PSoC® 6 MCUs and Wi-Fi/Bluetooth using Arm® Mbed™

    # Lesson GitHub Project
    0 Introduction
    1 Developer Resources
    2 Your First Project & The Blinking Thread https://github.com/iotexpert/mouser-mbed-02.git
    3 Display Thread https://github.com/iotexpert/mouser-mbed-03.git
    4 Temperature Thread https://github.com/iotexpert/mouser-mbed-04.git
    5 CapSense Thread https://github.com/iotexpert/mouser-mbed-05.git
    6 WiFi & NTP Thread https://github.com/iotexpert/mouser-mbed-06.git
    7 The CY8CKIT-062-WiFi-BT https://github.com/iotexpert/mouser-mbed-07.git 
    8 Amazon AWS MQTT Thread - Part1 https://github.com/iotexpert/mouser-mbed-08.git
    9 Amazon AWS MQTT Thread - Part2 https://github.com/iotexpert/mouser-mbed-09.git

    You can “mbed import https://github.com/iotexpert/mouser-mbed-09.git“ to make a copy of the project in your workspace.

    The final architecture of the thermostat looks like this.

     

    Summary

    In this lesson we will add an output capability to our project.  But what output device?  I am going old school with a serial terminal using VT100 graphics commands.

    Here is the update to the architecture.  Notice the new green boxes.

    1. A new thread (called Display Thread)
    2. The Serial terminal
    3. An RTC (the one built into the PSoC 6)
    4. An RTOS Queue called “Display Command Queue”
    5. A function called displaySendUpdateTime (to tell the display to show the time)

    The way this will work is a new thread called “displayThread” will be created.  I will have an RTOS queue associated with it.  Tasks that want to display something will push a message into the queue.  The display thread will wait around until some other task pushes a message into the queue.  When another task pushes a message, the displayThread will process the message and “do the needful”.

    To implement this I will:

    1. Import from GitHub lesson02
    2. Discuss VT100 Commands
    3. Create & code displayThread.h
    4. Create & code displayThread.cpp
    5. Create & code mbed_app.json
    6. Update blinkThread.cpp
    7. Update main.cpp
    8. Build, Program and Test

    Import from GitHub Lesson 02

    Mbed Studio can start a new project by “importing” an old project and giving it a new name.  This is a super convenient way for me to build each project for this class on the base of the previous one.  To make this easier for you I save each of these lesson projects on GitHub so you can start from there.

    In Mbed Studio use “File->Import Program…”

    Give it the path to my github site for the project you want to start from:

    https://github.com/iotexpert/mouser-mbed-02.git

    And then give it the new name you want for the new project – in this case mouser-mbed-03

    After clicking “Add program” you should have a new program called “mouser-mbed-03” and it should be set as the active program.

    At this point you could press the play button and get exactly the same behavior as the previous lesson 02.

    VT100 Commands

    Back in the dark ages when I started programming, most work was done on a “main frame” of some kind or the other.  The two that I used were the PDP-11 and the Prime.  To access these systems you typically used a “Dumb Terminal” attached to the mainframe via a long serial cable.  One of the most used terminals was the VT100 made by Digital Equipment Corporation (DEC).

    These terminals could output a fixed width font in about 80 columns and 24 rows.  Basically, the mainframe sent ASCII characters and the terminal would display them.

    An interesting feature that was put into these terminals was the ability to take commands and move the cursor around to screen, or clear it, or draw a line or ….

    What does that have to do with today?  Simple, the VT100 command set is still used by almost all serial programs on your PC (Putty, etc).  And I am going to use these commands to create a graphical user interface for the thermostat that looks like this:

    • Line 1 will be the current temperature
    • Line 2 will be the setPoint of the thermostat
    • Line 3 will be the time
    • Line 4 will be the status of the system (Heat, Cool or Off)

    To use the VT100 commands you just use printf to send them to the serial terminal.  The commands I will use are:

    Command ESC Sequence printf
    Home ESC [ H printf("\033[H");
    Clear Screen ESC [ J printf("\033[2J");
    Turn Cursor Off ESC [ ? 25l printf("\033[?25l");
    Goto column & row ESC [ column ; row H printf("\033[%d;%dH%s",y,x,buffer);

    Add displayThread.h

    As in lesson 02 I will create a new thread.  This time the header file will be called “displayThread.h”  Right click and select “New File”

    Give it the name “displayThread.h”

    This file will:

    1. Guard against multiple include
    2. Provide the function prototype of the thread
    3. Provide four functions to tell the displayThread to write onto the four different lines of the GUI
    #ifndef DISPLAY_THREAD_H
    #define DISPLAY_THREAD_H
    void displayThread();
    void displaySendUpdateTemp(float temperature);
    void displaySendUpdateTime();
    void displaySendUpdateSetPoint(float setPoint);
    void displaySendUpdateMode(float mode);
    #endif

    Add displayThread.cpp

    From now on I will just show the file creation.  In this case “displayThread.cpp”

    The first part of the file is to specify the queue.  To do this I create a new type called msg_t which will be inserted into the queue (actually a pointer to the msg_t).  The message structure will have two members the “command_t” and a generic floating point variable called “value”.

    The Queue and Memory pool will only be accessible inside of this file.  The queue will hold up to 16 “msg_t”.  The MemoryPool is just an easy  way to do memory allocation.

    #include "mbed.h"
    #include "displayThread.h"
    typedef enum {
    CMD_temperature,
    CMD_setPoint,
    CMD_time,
    CMD_mode,
    } command_t;
    typedef struct {
    command_t cmd;
    float    value;
    } msg_t;
    static Queue<msg_t, 16> queue;
    static MemoryPool<msg_t, 16> mpool;

    In order to insert msg_t into the queue I create one function per msg_t that:

    • allocates a new message
    • sets the command
    • sets the value
    • puts the message in the queue

    Notice if something goes wrong, I just ignore the problem.  In a real system you would probably want to do something else.

    These four functions will actually run inside of the thread that calls them, but they will insert messages into the queue that the displayThread is processing.  This queue mechanism is a safe way for processes to talk to each other.

    // Function called by other threads to queue a temperature change onto the display
    void displaySendUpdateTemp(float temperature)
    {
    msg_t *message = mpool.alloc();
    if(message)
    {
    message->cmd = CMD_temperature;
    message->value = temperature;
    queue.put(message);
    }
    }
    // Function called by other threads to queue display to update time
    void displaySendUpdateTime()
    {
    msg_t *message = mpool.alloc();
    if(message)
    {
    message->cmd = CMD_time;
    message->value = 0;
    queue.put(message);
    }
    }
    // Function called by other threads to queue a setPoint change onto the display
    void displaySendUpdateSetPoint(float setPoint)
    {
    msg_t *message = mpool.alloc();
    if(message)
    {
    message->cmd = CMD_setPoint;
    message->value = setPoint;
    queue.put(message);
    }
    }
    // Function called by other threads to queue a setPoint change onto the display
    void displaySendUpdateMode(float mode)
    {
    msg_t *message = mpool.alloc();
    if(message)
    {
    message->cmd = CMD_mode;
    message->value = mode;
    queue.put(message);
    }
    }

    The function displayAtXY creates a string with the VT100 Goto command plus the string that was send.

    static void displayAtXY(int x, int y,char *buffer)
    {
    // row column
    printf("\033[%d;%dH%s",y,x,buffer);
    fflush(stdout);
    }
    

    The actual display thread:

    • Clears the screen and turns the cursor off
    • Goes into an infinite loop waiting for messages to be put into the queue
    • When a new message comes in, look at the command
    • sprintf to create the display message
    • print it using the displayAtXY function
    void displayThread()
    {
    char buffer[128];
    printf("\033[2J\033[H"); // Clear Screen and go Home
    printf("\033[?25l"); // Turn the cursor off
    fflush(stdout);
    while(1)
    {
    osEvent evt = queue.get();
    if (evt.status == osEventMessage) {
    msg_t *message = (msg_t*)evt.value.p;
    switch(message->cmd)
    {
    case CMD_temperature:
    sprintf(buffer,"Temperature = %2.1fF",message->value);
    displayAtXY(1, 1, buffer);
    break;
    case CMD_setPoint:
    sprintf(buffer,"Set Point = %2.1fF",message->value);
    displayAtXY(1, 2, buffer);
    break;
    case CMD_time:
    time_t rawtime;
    struct tm * timeinfo;
    time (&rawtime);
    rawtime = rawtime - (5*60*60); // UTC - 4hours ... serious hack which only works in winter
    timeinfo = localtime (&rawtime);
    strftime (buffer,sizeof(buffer),"%r",timeinfo);
    displayAtXY(1,3, buffer);
    break;
    case CMD_mode:
    if(message->value == 0.0)
    sprintf(buffer,"Mode = Off ");
    else if (message->value < 0.0)
    sprintf(buffer,"Mode = Heat");
    else
    sprintf(buffer,"Mode = Cool");
    displayAtXY(1, 4, buffer);
    break;
    }
    mpool.free(message);
    }
    }
    }
    

    Here is the whole file displayThread.cpp (to make a copy/paste easier):

    #include "mbed.h"
    #include "displayThread.h"
    typedef enum {
    CMD_temperature,
    CMD_setPoint,
    CMD_time,
    CMD_mode,
    } command_t;
    typedef struct {
    command_t cmd;
    float    value;
    } msg_t;
    static Queue<msg_t, 32> queue;
    static MemoryPool<msg_t, 16> mpool;
    // Function called by other threads to queue a temperature change onto the display
    void displaySendUpdateTemp(float temperature)
    {
    msg_t *message = mpool.alloc();
    if(message)
    {
    message->cmd = CMD_temperature;
    message->value = temperature;
    queue.put(message);
    }
    }
    // Function called by other threads to queue display to update time
    void displaySendUpdateTime()
    {
    msg_t *message = mpool.alloc();
    if(message)
    {
    message->cmd = CMD_time;
    message->value = 0;
    queue.put(message);
    }
    }
    // Function called by other threads to queue a setPoint change onto the display
    void displaySendUpdateSetPoint(float setPoint)
    {
    msg_t *message = mpool.alloc();
    if(message)
    {
    message->cmd = CMD_setPoint;
    message->value = setPoint;
    queue.put(message);
    }
    }
    // Function called by other threads to queue a setPoint change onto the display
    void displaySendUpdateMode(float mode)
    {
    msg_t *message = mpool.alloc();
    if(message)
    {
    message->cmd = CMD_mode;
    message->value = mode;
    queue.put(message);
    }
    }
    static void displayAtXY(int x, int y,char *buffer)
    {
    // row column
    printf("\033[%d;%dH%s",y,x,buffer);
    fflush(stdout);
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    void displayThread()
    {
    char buffer[128];
    printf("\033[2J\033[H"); // Clear Screen and go Home
    printf("\033[?25l"); // Turn the cursor off
    fflush(stdout);
    while(1)
    {
    osEvent evt = queue.get();
    if (evt.status == osEventMessage) {
    msg_t *message = (msg_t*)evt.value.p;
    switch(message->cmd)
    {
    case CMD_temperature:
    sprintf(buffer,"Temperature = %2.1fF",message->value);
    displayAtXY(1, 1, buffer);
    break;
    case CMD_setPoint:
    sprintf(buffer,"Set Point = %2.1fF",message->value);
    displayAtXY(1, 2, buffer);
    break;
    case CMD_time:
    time_t rawtime;
    struct tm * timeinfo;
    time (&rawtime);
    rawtime = rawtime - (5*60*60); // UTC - 4hours ... serious hack which only works in winter
    timeinfo = localtime (&rawtime);
    strftime (buffer,sizeof(buffer),"%r",timeinfo);
    displayAtXY(1,3, buffer);
    break;
    case CMD_mode:
    if(message->value == 0.0)
    sprintf(buffer,"Mode = Off ");
    else if (message->value < 0.0)
    sprintf(buffer,"Mode = Heat");
    else
    sprintf(buffer,"Mode = Cool");
    displayAtXY(1, 4, buffer);
    break;
    }
    mpool.free(message);
    }
    }
    }
    

    Create mbed_app.json

    By default the serial terminal on Mbed OS is 9600 baud or approximately the same speed as back in VT100 days.  Fortunately you can change that by setting up a global configuration.  To do this you need to create a file called “mbed_app.json”.

    In that file, I tell the system that I want 115200 baud for all targets.

    {
    "target_overrides": {
    "*": {
    "platform.stdio-baud-rate": 115200
    }
    }
    }
    

    Update blinkThread.cpp

    In order to test the new displayThread, I will fix up the blinkThread to temporarily send the four messages.

    #include "mbed.h"
    #include "blinkThread.h"
    #include "displayThread.h"
    static DigitalOut led1(LED1);
    void blinkThread()
    {
    while (true) 
    {
    led1 = !led1;
    displaySendUpdateTime();
    displaySendUpdateTemp(69.2);
    displaySendUpdateSetPoint(23.5);
    displaySendUpdateMode(-1.0);
    ThisThread::sleep_for(500);
    }
    }

    Update main.cpp

    The last step is to update main to start the new displayThread.

    #include "mbed.h"
    #include "blinkThread.h"
    #include "displayThread.h"
    Thread blinkThreadHandle;
    Thread displayThreadHandle;
    int main()
    {
    printf("Started System\n");
    blinkThreadHandle.start(blinkThread);
    displayThreadHandle.start(displayThread);
    }
    

    Build, Program and Test

    Now press the play button.

    One nice thing in Mbed Studio is a built-in serial port viewer.  You can use it by clicking “View -> Serial Monitor”

    Change the baud rate to 115200 and you should see something like this: