Example #1
0
void timer_cb()
{
	static uint8_t triggerUp = 0;
	static uint8_t triggerDown = 0;
	static uint8_t closeCount = 0;

	//Up gate open count
	if(!(PINA & (1 << 3)))
	{
		//gate open longer than 2s
		if(triggerUp++ == 2)
		{
			led_setState(onUp);
			triggerUp = 3;
		}
	}
	else
	{
		triggerUp = 0;
	}
	//Down gate open count
	if(!(PINA & (1 << 4)))
	{
		//gate open longer than 2s
		if(triggerDown++ == 2)
		{
			led_setState(onDown);
			triggerDown = 3;
		}
	}
	else
	{
		triggerDown = 0;
	}

	//both gates closed longer than 10 seconds, switch off lights
	if((PINA & (1 << 4)) && (PINA & (1 << 3)))
	{
		if(closeCount++ > 9)
		{
			led_setState(off);
		}
	}
	else
	{
		closeCount = 0;
	}
}
Example #2
0
/**@brief Start advertising.
 */
void bleApp_advertisingStart() {
    uint32_t             err_code;
    ble_gap_adv_params_t adv_params;

    // Start advertising
    memset(&adv_params, 0, sizeof(adv_params));

    adv_params.type        = BLE_GAP_ADV_TYPE_ADV_IND;
    adv_params.p_peer_addr = NULL;
    adv_params.fp          = BLE_GAP_ADV_FP_ANY;
    adv_params.interval    = APP_ADV_INTERVAL;
    adv_params.timeout     = APP_ADV_TIMEOUT_IN_SECONDS;

    err_code = sd_ble_gap_adv_start(&adv_params);
    /* Allow invalid state errors, trap everything else.  We do this because we
     * may already be advertising when we try to start advertising again, and
     * that's okay. */
    if (err_code == NRF_ERROR_INVALID_STATE) {
    	;
    } else {
    	APP_ERROR_CHECK(err_code);
    }

    led_setState(LED_BLUE, LED_STATE_SLOW_FLASH);
}
Example #3
0
void bleApp_advertisingStop() {
    uint32_t err_code;

    err_code = sd_ble_gap_adv_stop();
    /* Allow invalid state errors, trap everything else.  We do this because we
     * may not be advertising at the moment when we try to stop advertising,
     * and even though that generates an error, it's okay. */
    if (err_code == NRF_ERROR_INVALID_STATE) {
    	;
    } else {
    	APP_ERROR_CHECK(err_code);
    }

    led_setState(LED_BLUE, LED_STATE_OFF);
}
Example #4
0
/**@brief Application's BLE Stack event handler.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 */
void bleApp_onEvt(ble_evt_t * p_ble_evt) {
    uint32_t                         err_code = NRF_SUCCESS;
    static ble_gap_evt_auth_status_t m_auth_status;
    ble_gap_enc_info_t *             p_enc_info;

    switch (p_ble_evt->header.evt_id)
    {
    case BLE_GAP_EVT_CONNECTED:
	m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
	led_setState(LED_BLUE, LED_STATE_ON);
	break;

    case BLE_GAP_EVT_DISCONNECTED:
	m_conn_handle = BLE_CONN_HANDLE_INVALID;
	led_setState(LED_BLUE, LED_STATE_OFF);
	if (advertisingEnabled) {
	    bleApp_advertisingStart();
	}
	break;

    case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
	err_code = sd_ble_gap_sec_params_reply(m_conn_handle,
					       BLE_GAP_SEC_STATUS_SUCCESS,
					       &m_sec_params);
	break;

    case BLE_GATTS_EVT_SYS_ATTR_MISSING:
	err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0);
	break;

    case BLE_GAP_EVT_AUTH_STATUS:
	m_auth_status = p_ble_evt->evt.gap_evt.params.auth_status;
	break;

    case BLE_GAP_EVT_SEC_INFO_REQUEST:
	p_enc_info = &m_auth_status.periph_keys.enc_info;
	if (p_enc_info->div == p_ble_evt->evt.gap_evt.params.sec_info_request.div)
	{
	    err_code = sd_ble_gap_sec_info_reply(m_conn_handle, p_enc_info, NULL);
	}
	else
	{
	    // No keys found for this device
	    err_code = sd_ble_gap_sec_info_reply(m_conn_handle, NULL, NULL);
	}
	break;

    case BLE_GAP_EVT_TIMEOUT:
	if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) {
	    led_setState(LED_BLUE, LED_STATE_OFF);

	    // Go to system-off mode (this function will not return; wakeup will cause a reset)
	    //GPIO_WAKEUP_BUTTON_CONFIG(WAKEUP_BUTTON_PIN);
	    //err_code = sd_power_system_off();
	}
	break;

    case BLE_GAP_EVT_RSSI_CHANGED:
	break;

    default:
	break;
    }

    APP_ERROR_CHECK(err_code);
}