Close

More Software Examples!

A project log for Blinky 101

A step-by-step guide to blinky Hexabitz modules!

hexabitzHexabitz 04/05/2019 at 11:080 Comments

After you wrote you first program, here are more examples to get you busy!

Example 2: 

Make some dancing lights using broadcasted dim Message. Since the dim message enables separate internal timers on each module they end up out-of-sync in a beautiful way :D Note how message parameters with 32-bit size are cast into byte-wide parameters.

You can copy the code below directly into a main.c file in User folder or rename the main_ex2.c file to main.c and replace the older one.

/* FrontEndTask function */
void FrontEndTask(void * argument)
{
    // Random dancing lights
    
#if _module == 1
    Delay_ms(500);
    BOS.response = BOS_RESPONSE_NONE;
#endif        

  /* Infinite loop */
  for(;;)
  {
    #if _module == 1
        messageParams[0] = CYAN;            // color
        messageParams[1] = RGB_DIM_UP_DOWN_WAIT;    // mode
        messageParams[2] = (uint8_t)(10000>>24);    // period
        messageParams[3] = (uint8_t)(10000>>16);     
        messageParams[4] = (uint8_t)(10000>>8); 
        messageParams[5] = (uint8_t)(10000); 
        messageParams[6] = (uint8_t)(500>>24);        // wait
        messageParams[7] = (uint8_t)(500>>16);     
        messageParams[8] = (uint8_t)(500>>8); 
        messageParams[9] = (uint8_t)(500); 
        messageParams[10] = 0;                // repeat
        messageParams[111] = 0;     
        messageParams[12] = 0; 
        messageParams[13] = 1; 
        
        SendMessageToModule(BOS_BROADCAST, CODE_H01R0_DIM, 14);
        
        RGB_LED_dim(CYAN, RGB_DIM_UP_DOWN_WAIT, 10000, 500, 1);
        
        Delay_ms(500);
    #endif    
    
        
  }    

}

Example 3: 

Aroadcast dim (exactly as ex 2) but fully-synchronized. Module 1 emulated the dim functionality and broadcasts color Messages with varying intensity. This way you keep the timer on a single module and keep them all synced. The 500ms delay before the loop is to ensure all modules finished booting before sending first Message.

/* FrontEndTask function */
void FrontEndTask(void * argument)
{
    // Synchronized dancing lights
    
    uint8_t i;
    
#if _module == 1
    Delay_ms(500);
    BOS.response = BOS_RESPONSE_NONE;
#endif        

  /* Infinite loop */
  for(;;)
  {
    #if _module == 1
        // Up phase
        for (i=1 ; i<=100 ; i++)
        {
            messageParams[0] = 0;                        // Use color name
            messageParams[1] = MAGENTA;                    // color
            messageParams[2] = i;                        // intensity
            SendMessageToModule(BOS_BROADCAST, CODE_H01R0_COLOR, 3);            
            RGB_LED_setColor(MAGENTA, i);        
            Delay_ms(50);
        }
        
        // Down phase
        for (i=100 ; i>=1 ; i--)
        {
            messageParams[0] = 0;                        // Use color name
            messageParams[1] = MAGENTA;                    // color
            messageParams[2] = i;                        // intensity
            SendMessageToModule(BOS_BROADCAST, CODE_H01R0_COLOR, 3);            
            RGB_LED_setColor(MAGENTA, i);        
            Delay_ms(50);
        }
        
        // Off
        SendMessageToModule(BOS_BROADCAST, CODE_H01R0_OFF, 0);
        RGB_LED_off();
        
        // Wait
        Delay_ms(1000);        
    #endif        
        
  }    

}

Example 4: 

Defining a  button on Module 1 and enabling its Click event. This event is used to set a flag in the event callback. The flag is used to generate some dancing lights.

/* FrontEndTask function */
void FrontEndTask(void * argument)
{
    // Dancing lights enabled by button on Module 1
    
    BOS.response = BOS_RESPONSE_NONE;
    
#if _module == 1
    // Setup the button
    AddPortButton(MOMENTARY_NO, P1);
    SetButtonEvents(P1,1,0,0,0,0,0,0,0);
#endif        

  /* Infinite loop */
  for(;;)
  {
        
#if _module == 1
        // Perform the dance
        if (buttonPressed)
        {
            RGB_LED_pulseColor(RED, 200, 100, 1);    
            Delay_ms(100);    
            for (uint8_t i=2 ; i<=5 ; i++)
            {
                messageParams[0] = 0;                // Use color name
                messageParams[1] = RED;                // color
                // period
                messageParams[2] = (uint8_t)(200>>24);
                messageParams[3] = (uint8_t)(200>>16);
                messageParams[4] = (uint8_t)(200>>8);
                messageParams[5] = (uint8_t)(200);
                // duty
                messageParams[6] = (uint8_t)(100>>24);
                messageParams[7] = (uint8_t)(100>>16);
                messageParams[8] = (uint8_t)(100>>8);
                messageParams[9] = (uint8_t)(100);
                // repeat
                messageParams[10] = 0;
                messageParams[11] = 0;
                messageParams[12] = 0;
                messageParams[13] = 1;
                SendMessageToModule(i, CODE_H01R0_PULSE, 14);            
                Delay_ms(100);    
            }    
            for (uint8_t i=4 ; i>=2 ; i--)
            {
                messageParams[0] = 0;                // Use color name
                messageParams[1] = RED;                // color
                // period
                messageParams[2] = (uint8_t)(200>>24);
                messageParams[3] = (uint8_t)(200>>16);
                messageParams[4] = (uint8_t)(200>>8);
                messageParams[5] = (uint8_t)(200);
                // duty
                messageParams[6] = (uint8_t)(100>>24);
                messageParams[7] = (uint8_t)(100>>16);
                messageParams[8] = (uint8_t)(100>>8);
                messageParams[9] = (uint8_t)(100);
                // repeat
                messageParams[10] = 0;
                messageParams[11] = 0;
                messageParams[12] = 0;
                messageParams[13] = 1;
                SendMessageToModule(i, CODE_H01R0_PULSE, 14);            
                Delay_ms(100);    
            }                
            RGB_LED_pulseColor(RED, 200, 100, 1);
            buttonPressed = 0;        // Reset button flag
        }
#endif        
        
  }    

}

// Button click callback
void buttonClickedCallback(uint8_t port)
{    
    buttonPressed = 1;
}

Example 5: 

Same concept as ex 4 but with a different dancing pattern on each button. The code looks long and scary but it's all the same code repeated.! As shown there, you can use compiler defines: 

#if _module == 1
xxxxxxxxxx 
#endif 

to execute specific code inside a specific module. This saves you the hassle of creating separate projects if all or some of your modules have the same part number.

Example 6:

This example demonstrates the use of remote write API.

Button 1 (connected to Module 1) is used as on/off switch for all LEDs. Button 2 is used to cycle through basic colors (WHITE, RED, BLUE, YELLOW, CYAN, MAGENTA, GREEN). Button 4 is used to increase intensity and Button 5 to decrease intensity (each click is 10%).

All modules define three BOS variables: A bool describing LED status and two UINT8 describing intensity and color. They are initialized to ON, 50(%) and WHITE respectively.

First of all the global variables are linked to BOS variables so that we can use addresses 1, 2 and 3 to address them across the array. Then all four buttons are defined and click event is enabled. Inside the click event, each module performs its task locally (e.g., cycling through colors) then broadcasts a RemoteWrite to the appropriate BOS variable. The indicator LED is also blinked to show the click. Inside the infinite loop, a setColor API is used to control the local LED based on the value of BOS variables shared across the array. This example shows how to use RemoteWrite to control basically everything in the array.

Discussions