/**@brief Timer callback used for periodic changing of LED state. */ static void iot_timer_client_one_callback(iot_timer_time_in_ms_t elapsed_time) { UNUSED_PARAMETER(elapsed_time); LEDS_INVERT(DISPLAY_LED_0); printf("---- %4d ms\r\n", IOT_TIMER_CLIENT_ONE_CB_INTERVAL); }
/**@brief Configure leds to indicate required state. * @param[in] indicate State to be indicated. */ static uint32_t bsp_led_indication(bsp_indication_t indicate) { uint32_t err_code = NRF_SUCCESS; uint32_t next_delay = 0; switch (indicate) { case BSP_INDICATE_IDLE: LEDS_OFF(LEDS_MASK & ~m_alert_mask); m_stable_state = indicate; break; case BSP_INDICATE_SCANNING: case BSP_INDICATE_ADVERTISING: LEDS_OFF(LEDS_MASK & ~BSP_LED_0_MASK & ~m_alert_mask); // in advertising blink LED_0 if (LED_IS_ON(BSP_LED_0_MASK)) { LEDS_OFF(BSP_LED_0_MASK); next_delay = indicate == BSP_INDICATE_ADVERTISING ? ADVERTISING_LED_OFF_INTERVAL : ADVERTISING_SLOW_LED_OFF_INTERVAL; } else { LEDS_ON(BSP_LED_0_MASK); next_delay = indicate == BSP_INDICATE_ADVERTISING ? ADVERTISING_LED_ON_INTERVAL : ADVERTISING_SLOW_LED_ON_INTERVAL; } m_stable_state = indicate; err_code = app_timer_start(m_leds_timer_id, BSP_MS_TO_TICK(next_delay), NULL); break; case BSP_INDICATE_ADVERTISING_WHITELIST: LEDS_OFF(LEDS_MASK & ~BSP_LED_0_MASK & ~m_alert_mask); // in advertising quickly blink LED_0 if (LED_IS_ON(BSP_LED_0_MASK)) { LEDS_OFF(BSP_LED_0_MASK); next_delay = indicate == BSP_INDICATE_ADVERTISING_WHITELIST ? ADVERTISING_WHITELIST_LED_OFF_INTERVAL : ADVERTISING_SLOW_LED_OFF_INTERVAL; } else { LEDS_ON(BSP_LED_0_MASK); next_delay = indicate == BSP_INDICATE_ADVERTISING_WHITELIST ? ADVERTISING_WHITELIST_LED_ON_INTERVAL : ADVERTISING_SLOW_LED_ON_INTERVAL; } m_stable_state = indicate; err_code = app_timer_start(m_leds_timer_id, BSP_MS_TO_TICK(next_delay), NULL); break; case BSP_INDICATE_ADVERTISING_SLOW: LEDS_OFF(LEDS_MASK & ~BSP_LED_0_MASK & ~m_alert_mask); // in advertising slowly blink LED_0 if (LED_IS_ON(BSP_LED_0_MASK)) { LEDS_OFF(BSP_LED_0_MASK); next_delay = indicate == BSP_INDICATE_ADVERTISING_SLOW ? ADVERTISING_SLOW_LED_OFF_INTERVAL : ADVERTISING_SLOW_LED_OFF_INTERVAL; } else { LEDS_ON(BSP_LED_0_MASK); next_delay = indicate == BSP_INDICATE_ADVERTISING_SLOW ? ADVERTISING_SLOW_LED_ON_INTERVAL : ADVERTISING_SLOW_LED_ON_INTERVAL; } m_stable_state = indicate; err_code = app_timer_start(m_leds_timer_id, BSP_MS_TO_TICK(next_delay), NULL); break; case BSP_INDICATE_ADVERTISING_DIRECTED: LEDS_OFF(LEDS_MASK & ~BSP_LED_0_MASK & ~m_alert_mask); // in advertising very quickly blink LED_0 if (LED_IS_ON(BSP_LED_0_MASK)) { LEDS_OFF(BSP_LED_0_MASK); next_delay = indicate == BSP_INDICATE_ADVERTISING_DIRECTED ? ADVERTISING_DIRECTED_LED_OFF_INTERVAL : ADVERTISING_SLOW_LED_OFF_INTERVAL; } else { LEDS_ON(BSP_LED_0_MASK); next_delay = indicate == BSP_INDICATE_ADVERTISING_DIRECTED ? ADVERTISING_DIRECTED_LED_ON_INTERVAL : ADVERTISING_SLOW_LED_ON_INTERVAL; } m_stable_state = indicate; err_code = app_timer_start(m_leds_timer_id, BSP_MS_TO_TICK(next_delay), NULL); break; case BSP_INDICATE_BONDING: LEDS_OFF(LEDS_MASK & ~BSP_LED_0_MASK & ~m_alert_mask); // in bonding fast blink LED_0 if (LED_IS_ON(BSP_LED_0_MASK)) { LEDS_OFF(BSP_LED_0_MASK); } else { LEDS_ON(BSP_LED_0_MASK); } m_stable_state = indicate; err_code = app_timer_start(m_leds_timer_id, BSP_MS_TO_TICK(BONDING_INTERVAL), NULL); break; case BSP_INDICATE_CONNECTED: LEDS_OFF(LEDS_MASK & ~BSP_LED_0_MASK & ~m_alert_mask); LEDS_ON(BSP_LED_0_MASK); m_stable_state = indicate; break; case BSP_INDICATE_SENT_OK: // when sending shortly invert LED_1 LEDS_INVERT(BSP_LED_1_MASK); err_code = app_timer_start(m_leds_timer_id, BSP_MS_TO_TICK(SENT_OK_INTERVAL), NULL); break; case BSP_INDICATE_SEND_ERROR: // on receving error invert LED_1 for long time LEDS_INVERT(BSP_LED_1_MASK); err_code = app_timer_start(m_leds_timer_id, BSP_MS_TO_TICK(SEND_ERROR_INTERVAL), NULL); break; case BSP_INDICATE_RCV_OK: // when receving shortly invert LED_1 LEDS_INVERT(BSP_LED_1_MASK); err_code = app_timer_start(m_leds_timer_id, BSP_MS_TO_TICK(RCV_OK_INTERVAL), NULL); break; case BSP_INDICATE_RCV_ERROR: // on receving error invert LED_1 for long time LEDS_INVERT(BSP_LED_1_MASK); err_code = app_timer_start(m_leds_timer_id, BSP_MS_TO_TICK(RCV_ERROR_INTERVAL), NULL); break; case BSP_INDICATE_FATAL_ERROR: // on fatal error turn on all leds LEDS_ON(LEDS_MASK); m_stable_state = indicate; break; case BSP_INDICATE_ALERT_0: case BSP_INDICATE_ALERT_1: case BSP_INDICATE_ALERT_2: case BSP_INDICATE_ALERT_3: case BSP_INDICATE_ALERT_OFF: err_code = app_timer_stop(m_alert_timer_id); next_delay = (uint32_t)BSP_INDICATE_ALERT_OFF - (uint32_t)indicate; // a little trick to find out that if it did not fall through ALERT_OFF if (next_delay && (err_code == NRF_SUCCESS)) { m_alert_mask = ALERT_LED_MASK; if (next_delay > 1) { err_code = app_timer_start(m_alert_timer_id, BSP_MS_TO_TICK( (next_delay * ALERT_INTERVAL)), NULL); } LEDS_ON(m_alert_mask); } else { LEDS_OFF(m_alert_mask); m_alert_mask = 0; } break; case BSP_INDICATE_USER_STATE_OFF: LEDS_OFF(LEDS_MASK); m_stable_state = indicate; break; case BSP_INDICATE_USER_STATE_0: LEDS_OFF(LEDS_MASK & ~BSP_LED_0_MASK); LEDS_ON(BSP_LED_0_MASK); m_stable_state = indicate; break; case BSP_INDICATE_USER_STATE_1: LEDS_OFF(LEDS_MASK & ~BSP_LED_1_MASK); LEDS_ON(BSP_LED_1_MASK); m_stable_state = indicate; break; case BSP_INDICATE_USER_STATE_2: LEDS_OFF(LEDS_MASK & ~(BSP_LED_0_MASK | BSP_LED_1_MASK)); LEDS_ON(BSP_LED_0_MASK | BSP_LED_1_MASK); m_stable_state = indicate; break; case BSP_INDICATE_USER_STATE_3: case BSP_INDICATE_USER_STATE_ON: LEDS_ON(LEDS_MASK); m_stable_state = indicate; break; default: break; } return err_code; }
/**@brief Handle events from alert timer. * * @param[in] p_context parameter registered in timer start function. */ static void alert_timer_handler(void * p_context) { UNUSED_PARAMETER(p_context); LEDS_INVERT(ALERT_LED_MASK); }
/**@brief Timer callback used for controlling board LEDs to represent application state. * */ static void blink_timeout_handler(iot_timer_time_in_ms_t wall_clock_value) { UNUSED_PARAMETER(wall_clock_value); #ifdef COMMISSIONING_ENABLED static bool id_mode_previously_enabled; if (m_identity_mode_active == false) { #endif // COMMISSIONING_ENABLED switch (m_display_state) { case LEDS_INACTIVE: { LEDS_OFF(ALL_APP_LED); break; } case LEDS_CONNECTABLE_MODE: { LEDS_INVERT(LED_ONE); LEDS_OFF((LED_TWO | LED_THREE | LED_FOUR)); break; } case LEDS_IPV6_IF_DOWN: { LEDS_INVERT(LED_TWO); LEDS_OFF((LED_ONE | LED_THREE | LED_FOUR)); break; } case LEDS_IPV6_IF_UP: { LEDS_ON(LED_ONE); LEDS_OFF((LED_TWO | LED_THREE | LED_FOUR)); break; } case LEDS_CONNECTED_TO_BROKER: { LEDS_ON(LED_TWO); LEDS_OFF((LED_ONE | LED_THREE | LED_FOUR)); m_display_state = LEDS_PUBLISHING; break; } case LEDS_PUBLISHING: { LEDS_OFF(LED_ONE); LEDS_ON(LED_TWO); break; } default: { break; } } #ifdef COMMISSIONING_ENABLED } #endif // COMMISSIONING_ENABLED #ifdef COMMISSIONING_ENABLED if ((id_mode_previously_enabled == false) && (m_identity_mode_active == true)) { LEDS_OFF(LED_THREE | LED_FOUR); } if ((id_mode_previously_enabled == true) && (m_identity_mode_active == true)) { LEDS_INVERT(LED_THREE | LED_FOUR); } if ((id_mode_previously_enabled == true) && (m_identity_mode_active == false)) { LEDS_OFF(LED_THREE | LED_FOUR); } id_mode_previously_enabled = m_identity_mode_active; #endif // COMMISSIONING_ENABLED }
/**@brief Timer callback used for controlling board LEDs to represent application state. * */ static void blink_timeout_handler(iot_timer_time_in_ms_t wall_clock_value) { UNUSED_PARAMETER(wall_clock_value); #ifdef COMMISSIONING_ENABLED static bool id_mode_previously_enabled; if (m_identity_mode_active == false) { #endif // COMMISSIONING_ENABLED switch (m_display_state) { case LEDS_INACTIVE: { LEDS_OFF((LED_ONE | LED_TWO)); LEDS_OFF((LED_THREE | LED_FOUR)); break; } case LEDS_CONNECTABLE_MODE: { LEDS_INVERT(LED_ONE); LEDS_OFF(LED_TWO); LEDS_OFF((LED_THREE | LED_FOUR)); break; } case LEDS_IPV6_IF_DOWN: { LEDS_ON(LED_ONE); LEDS_INVERT(LED_TWO); LEDS_OFF((LED_THREE | LED_FOUR)); break; } case LEDS_IPV6_IF_UP: { LEDS_OFF(LED_ONE); LEDS_ON(LED_TWO); // If m_blink_led_three is set, keep LED_THREE on for 1 period. if LED_IS_ON(LED_THREE) { LEDS_OFF(LED_THREE); } else if (m_blink_led_three == true) { LEDS_ON(LED_THREE); m_blink_led_three = false; } // If m_blink_led_four is set, keep LED_FOUR on for 1 period. if LED_IS_ON(LED_FOUR) { LEDS_OFF(LED_FOUR); } else if (m_blink_led_four == true) { LEDS_ON(LED_FOUR); m_blink_led_four = false; } break; } default: { break; } }
static void handle_led_request(uint32_t led_mask, coap_message_t * p_request, coap_resource_t * p_resource) { coap_message_conf_t response_config; memset (&response_config, 0, sizeof(coap_message_conf_t)); if (p_request->header.type == COAP_TYPE_NON) { response_config.type = COAP_TYPE_NON; } else if (p_request->header.type == COAP_TYPE_CON) { response_config.type = COAP_TYPE_ACK; } // PIGGY BACKED RESPONSE response_config.code = COAP_CODE_405_METHOD_NOT_ALLOWED; // Copy message ID. response_config.id = p_request->header.id; // Copy token. memcpy(&response_config.token[0], &p_request->token[0], p_request->header.token_len); // Copy token length. response_config.token_len = p_request->header.token_len; coap_message_t * p_response; uint32_t err_code = coap_message_new(&p_response, &response_config); APP_ERROR_CHECK(err_code); err_code = coap_message_remote_addr_set(p_response, &p_request->remote); APP_ERROR_CHECK(err_code); // Handle request. switch (p_request->header.code) { case COAP_CODE_GET: { p_response->header.code = COAP_CODE_205_CONTENT; // Select the first common content-type between the resource and the CoAP client. coap_content_type_t ct_to_use; err_code = coap_message_ct_match_select(&ct_to_use, p_request, p_resource); if (err_code != NRF_SUCCESS) { // None of the accept content formats are supported in this resource endpoint. p_response->header.code = COAP_CODE_415_UNSUPPORTED_CONTENT_FORMAT; p_response->header.type = COAP_TYPE_RST; } else { // Set response payload to actual LED state. char * response_str; led_value_get(led_mask, ct_to_use, &response_str); err_code = coap_message_payload_set(p_response, response_str, strlen(response_str)); APP_ERROR_CHECK(err_code); } break; } case COAP_CODE_PUT: { p_response->header.code = COAP_CODE_204_CHANGED; // Change LED state according to request. switch (p_request->p_payload[0]) { case COMMAND_ON: { LEDS_ON(led_mask); break; } case COMMAND_OFF: { LEDS_OFF(led_mask); break; } case COMMAND_TOGGLE: { LEDS_INVERT(led_mask); break; } default: { p_response->header.code = COAP_CODE_400_BAD_REQUEST; break; } } break; } default: { p_response->header.code = COAP_CODE_405_METHOD_NOT_ALLOWED; break; } } uint32_t msg_handle; err_code = coap_message_send(&msg_handle, p_response); APP_ERROR_CHECK(err_code); err_code = coap_message_delete(p_response); APP_ERROR_CHECK(err_code); }
void toggle_led(int i) { if (i < LEDS_NUMBER) { LEDS_INVERT(1 << leds_list[i]); } }
/** @brief Function for main application entry. */ int main(void) { spi_buffer_t spi_buffer[NUM_SPI_BUS]; rgb_led_t led_array[NUM_LEDS]; uint32_t current_limit; float dim; // Configure on-board LED-pins as outputs. LEDS_CONFIGURE(LEDS_MASK); // Initialize spi I/F for(uint8_t i=0;i<NUM_SPI_BUS;i++) { ws2812b_driver_spi_init(i, &spi[i]); } for(uint8_t i=0;i<NUM_SPI_BUS;i++) { alloc_spi_buffer(&spi_buffer[i], NUM_LEDS); } LEDS_ON(1 << leds_list[0]); LEDS_ON(1 << leds_list[1]); LEDS_ON(1 << leds_list[2]); LEDS_ON(1 << leds_list[3]); for(;;) { LEDS_INVERT(1 << leds_list[2]); for(int8_t idemo=0;idemo<size_of_list;idemo++) { LEDS_INVERT(1 << leds_list[1]); demo_list[idemo].function_init(); int32_t rest = demo_list[idemo].demo_period; int32_t rap = 0; int32_t step = demo_list[idemo].wait_ms + demo_list[idemo].process_time; while( rest > 0 ) { LEDS_INVERT(1 << leds_list[0]); // animate and set up led_array_work demo_list[idemo].function_update(led_array,rap); // dim LEDs until current limit current_limit = CURRENT_LIMIT; ws2812b_driver_current_cap(led_array, NUM_LEDS, current_limit); // fade in/out effect if ( (demo_list[idemo].demo_period - rest) < FADE_IN_MS ) { dim = (float)0.01+((float)0.99 * ((demo_list[idemo].demo_period - rest)/(float)FADE_IN_MS)); } else if ( rest < FADE_IN_MS) { dim = (float)0.01+((float)0.99 * (rest/(float)FADE_IN_MS)); } if ( dim > (float)1.0 ) { dim = 1.0; } ws2812b_driver_dim(led_array, NUM_LEDS, dim); // LED update ws2812b_driver_xfer(led_array, spi_buffer[0], spi[0]); // delay (LED will be updated this period) nrf_delay_ms(demo_list[idemo].wait_ms); // rest -= step; rap += step; } // blank 3sec. between demos set_blank(led_array,NUM_LEDS); ws2812b_driver_xfer(led_array, spi_buffer[0], spi[0]); // delay (LED will be updated this period) nrf_delay_ms(3000); } // idemo } // end-less loop }