Rapid Prototyping a Bluetooth® Smart Appliance Control Application on a 32-bit MCU-based Curiosity Nano Evaluation Kit Using MPLAB® Harmony v3 Software Framework: Step 7

Last modified by Microchip on 2023/11/09 09:08

 

Add Application Code to the Project

The custom eink_bundle_image.c and eink_bundle_image.h files for this application are pre-developed and are available in <your unzip folder>/pic32cmmc_smart_appliance_control /dev_files/pic32cm_mc00_cnano folder.

The eink_bundle_image.c file contains the image array.

  • Go to the pic32cmmc_smart_appliance_control /dev_files/pic32cm_mc00_cnano folder and copy the pre-developed eink_bundle_image.c and eink_bundle_image.h files.
  • Replace the eink_bundle_image.c and eink_bundle_image.h file of your project available at <Your project folder>/ pic32cmmc_smart_appliance_control /firmware/click_routines/eink_bundle/ by over-writing it with the copied file.

The application is already partially developed and is available in the main.c file under <your unzip folder>/pic32cmmc_smart_appliance_control/dev_files/pic32cm_mc00_cnano. The main.c file contains the application logic. It also contains placeholders that you will populate with the necessary code in the next step.

  • Go to the pic32cmmc_smart_appliance_control /dev_files/pic32cm_mc00_cnano folder and copy the pre-developed main.c file.
  • Replace (over-write) the main.c file of your project available at <Your project folder>/pic32cmmc_smart_appliance_control /firmware/src with the copied file.
  • Open main.c in MPLAB® X IDE and add the application code by following the steps below:

Under the main.c file, in the main() function, notice the call to the SYS_Initialize function. The generated SYS_Initialize function initializes all the peripheral modules used in the application, configured through MPLAB Harmony Configurator (MHC).

generated SYS_Initialize function

In the int main (void) function, below the SYS_Initialize() function call, add the below lines of code to initialize Fan Click, eINK Click, and BM71 BLE

    CLICK_FAN_TimerStart();
    CLICK_FAN_DelayMs(1000);
    fan_init();    
    fan_switch_off();
    CLICK_FAN_DelayMs(1000);

    eink_init();

    eink_set_font( guiFont_Tahoma_14_Regular, EINK_COLOR_BLACK, FO_HORIZONTAL);
    eink_image_bmp(mchp_logo_fan);

    DRV_BM71_Initialize();
    bleInitialize(true);
    strcpy(BLE_Cmd_buf, (char *)"TEMP_CTRL");

code to initialize Fan Click, eINK Click, and BM71 BLE

Note:

a. The CLICK_FAN_TimerStart function call starts the systick timer necessary for the Fan Click.

b. The CLICK_FAN_DelayMs function call creates a blocking delay.

c. The fan_init function call initializes the Fan Click.
The fan_switch_off function call switches off the fan.

d. The eink_init function initializes the eINK Click display.
The eink_set_font function sets the font for the eINK Click display.
The eink_image_bmp function displays the image on the eINK Click display.

e. The DRV_BM71_Initialize and bleInitialize function initializes the BM71 driver.

Add this code inside the previous code to add tasks related to BLE.

DRV_BM71_Tasks();
bleTasks();

code related to BLE

Add the code related to the weather sensor. To read the environmental temperature from the Weather Click, the user will choose the temperature-based Fan Control mode and the application will read the temperature values from a weather sensor. It will then display them on the eINK display, control a DC fan, and update the fan speed on the eINK display.

if(!strncmp(BLE_Cmd_buf, "TEMP_CTRL", 9))
        {
           Weather_readSensors();
           temperature =(int16_t)Weather_getTemperatureDegC();

           if(temperature >=18 && temperature <=25 )
              {
               fan_set_speed(SPEED_LOW);
               sprintf(buffer1, "    LOW");
              }
             else if(temperature >=26 && temperature <=30 )
              {
               fan_set_speed(SPEED_MEDIUM);
               sprintf(buffer1, "MEDIUM");
              }
             else if(temperature > 30)
              {
               fan_set_speed(SPEED_HIGH);
               sprintf(buffer1, "    HIGH");
              }
             else if(temperature < 18)
              {
               fan_switch_off()
               sprintf(buffer1, "OFF");
              }
            sprintf(buffer, "%d C",temperature);
            if(Isble_adv_started() == true){
               if(((strcmp(temp_buffer,buffer1)) != 0)|(dummy_temperature != temperature ))
                {
                   strcpy(temp_buffer,buffer1);
                   dummy_temperature = temperature;
                   eink_image_bmp(mchp_logo_fan);
                   eink_text(buffer,18,73 );
                   eink_text("Tempr =",0,55 );
                   eink_text(buffer1,0,145 );
                   CLICK_FAN_DelayMs(1000);
                }    
            }
        }

image showing the if statement

Following the above if statement, add an else if statement where the user can control the fan from the BLE-based MBD app running on the connected smartphone by sending commands and update the fan speed on eINK display.

 else if(!strncmp(BLE_Cmd_buf, "BLE_CTRL", 8))
        {
           if(!strcmp((char *)&BLE_Cmd_buf[8 + 1], "FAN_ON"))
            {
               fan_switch_on();
                sprintf(buffer1, "    ON");
            }
           else if(!strcmp((char *)&BLE_Cmd_buf[8 + 1], "FAN_LOW"))
            {
               fan_set_speed(SPEED_LOW);
               sprintf(buffer1, "    LOW");
            }
           else if(!strcmp((char *)&BLE_Cmd_buf[8 + 1], "FAN_MID"))
            {
               fan_set_speed(SPEED_MEDIUM);
               sprintf(buffer1, "MEDIUM");
            }
           else if(!strcmp((char *)&BLE_Cmd_buf[8 + 1], "FAN_HIGH"))
            {
               fan_set_speed(SPEED_HIGH);
               sprintf(buffer1, "    HIGH");
            }
           else if(!strcmp((char *)&BLE_Cmd_buf[8 + 1], "FAN_OFF"))
            {
               fan_switch_off()
                sprintf(buffer1, "    OFF");
            }
           else
            {
               ;
            }
           memset(BLE_Cmd_buf, 0x00, 100);
           if((strcmp(temp_buffer,buffer1)) != 0)
            {
               strcpy(temp_buffer,buffer1);
               eink_image_bmp(mchp_logo_fan);
               eink_text(buffer1,0,145 );
            }
        }
       else
        {
           ;
        }

 image showing the if else statement

You are now ready to build the code and observe the results!