Example #1
0
/**
 * @brief Function for application main entry.
 */
int main(void)
{
    timer0_init(); // Timer used to blink the LEDs.
    timer1_init(); // Timer to generate events on even number of seconds.
    timer2_init(); // Timer to generate events on odd number of seconds.
    ppi_init();    // PPI to redirect the event to timer start/stop tasks.

    uint32_t err_code;
    const app_uart_comm_params_t comm_params =
     {
         RX_PIN_NUMBER,
         TX_PIN_NUMBER,
         RTS_PIN_NUMBER,
         CTS_PIN_NUMBER,
         APP_UART_FLOW_CONTROL_ENABLED,
         false,
         UART_BAUDRATE_BAUDRATE_Baud115200
     };

    APP_UART_FIFO_INIT(&comm_params,
                    UART_RX_BUF_SIZE,
                    UART_TX_BUF_SIZE,
                    uart_error_handle,
                    APP_IRQ_PRIORITY_LOW,
                    err_code);

    APP_ERROR_CHECK(err_code);

    // Enabling constant latency as indicated by PAN 11 "HFCLK: Base current with HFCLK 
    // running is too high" found at Product Anomaly document found at
    // https://www.nordicsemi.com/eng/Products/Bluetooth-R-low-energy/nRF51822/#Downloads
    //
    // @note This example does not go to low power mode therefore constant latency is not needed.
    //       However this setting will ensure correct behaviour when routing TIMER events through 
    //       PPI (shown in this example) and low power mode simultaneously.
    NRF_POWER->TASKS_CONSTLAT = 1;
    
    // Start clock.
    nrf_drv_timer_enable(&timer0);
    nrf_drv_timer_enable(&timer1);
    nrf_drv_timer_enable(&timer2);

    // Loop and increment the timer count value and capture value into LEDs. @note counter is only incremented between TASK_START and TASK_STOP.
    while (true)
    {

        printf("Current count: %d\n\r", (int)nrf_drv_timer_capture(&timer0,NRF_TIMER_CC_CHANNEL0));

        /* increment the counter */
        nrf_drv_timer_increment(&timer0);

        nrf_delay_ms(100);
    }
}
// Function to decode IR signals and message to web if the car is hit
void ir_in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
      static uint8_t decoded = 0;
      static uint8_t offset = 0;
      static uint32_t prev = 0;

      if(offset >= 8)
          offset = 0;

      uint32_t usec = nrf_drv_timer_capture(&ir_timer, NRF_TIMER_CC_CHANNEL0); 
      if(offset) 
          SEGGER_RTT_printf(0, "%d\r\n", usec - prev);
      
      if((usec - prev) > 2000)
      {
          decoded += (1 << (offset - 1));
      }
      
      if(offset == 7) 
      {
          SEGGER_RTT_printf(0, "\r\n\n%d\n\r\n", decoded);
          new_hit_value();
          if(decoded != unique_car_ID && decoded >= 1 && decoded <= 16)
          {
              ble_lbs_on_button_change(&m_lbs, hit_counter, 0);            
          
              playNote(1516);
              nrf_delay_ms(50);
              playNote(1607);
          }

          decoded = 0;
      }
            

      prev = usec;
      offset++;
}