Example #1
0
/*---------------------------------------------------------------------------*/
int main()
{
    uint8_t i;
    uint16_t temp;    
    uint16_t rval;
    uint16_t getDataCounter;

    init_hardware();    

    while(1)
    {                                
        /* power up the temperature sensor */
        pinMode(A,2,OUTPUT);
        digitalWrite(A,2,HIGH);

        /* prepare reading the 1st ADC channel */
        init_adc(0x01);
        _delay_us(750);

        /* take multiple readings and divide later */
        rval = 0;
        for (i = 0; i < 8; ++i)
        {
            temp = read_adc_sleepy();
            
            /* store each sample seperatly */
            data_array[(2*i)+2] = temp >> 8;
            data_array[(2*i)+3] = temp & 0xFF;        
            
            rval += temp;
        }
        rval = rval >> 3;

        /* Store the averages */
        data_array[0] = rval >> 8;
        data_array[1] = rval & 0xFF;

        /* reset ADC registers */
        ADCSRA = 0x00;
        ADMUX = 0x00;

        /* power down the temperature sensor */
        digitalWrite(A,2,LOW);
        pinMode(A,2,INPUT);                                          

        /* automatically goes to TX mode */
        nrf24_send(data_array);        

        /* be wise ... */
        sleep_during_transmisson();        

        /* parameter for this function determines how long does it wait for a message */
        check_bootloader_message(10);

        /* parameter for this function is WDT overflow count */
        sleep_for(25);
    }

    return 0;
}
Example #2
0
/* Send data using the nRF radio */
void radioTask(void *pvParameters)
{
	(void) pvParameters;
	uint8_t rfData[NRF24_PAYLOAD_LEN];
	uint8_t rcvData[NRF24_PAYLOAD_LEN];
	uint8_t pktCounter = 0;
	uint8_t blinkCounter = 0;
	const portTickType blinkDelay = 250 / portTICK_RATE_MS;
	const portTickType sendDelay = 5000 / portTICK_RATE_MS;

	rfData[0] = 0xB1;
	rfData[1] = 0x3A;
	rfData[2] = 0x23;
	while(1)
	{
		if(nrf24_dataReady())
		{
			nrf24_getData(rcvData);
			// Format message for debugging
			char debugRcvString[3*6] = {0};
			for (uint8_t i=0; i < NRF24_PAYLOAD_LEN; i++)
			{
				sprintf(debugRcvString+(3*i), "%02X ", rcvData[i]);
			}
			printf("Received: %s\n", debugRcvString);
		}


		rfData[3] = pktCounter++;
		nrf24_send(rfData);
		while(nrf24_isSending());
		char debugSendString[3*6] = {0};
		for (uint8_t i = 0; i < NRF24_PAYLOAD_LEN; i++)
		{
			sprintf(debugSendString+(3*i), "%02X ", rfData[i]);
		}
		printf("Sent: %s\n", debugSendString);

		if(nrf24_lastMessageStatus())
		{
			//there was a send problem
		}else{
			//transmission was ok
		}
		for(blinkCounter = 1 + nrf24_retransmissionCount(); blinkCounter > 0; --blinkCounter)
		{
			PORTC |= _BV(PORTC3);
			vTaskDelay(blinkDelay);
			PORTC &= ~_BV(PORTC3);
			vTaskDelay(blinkDelay);

		}

		nrf24_powerUpRx();
		vTaskDelay(sendDelay);

	}
}
Example #3
0
void copy_paste()
{

	int temp;
	nrf24_config(2, 4);
	nrf24_tx_address(tx_address11);
	nrf24_rx_address(rx_address11);
	while (1) {
		//LED0 = LED_ON;

		//LED1 = LED_ON;
		//LED2 = LED_ON;
		//LED3 = LED_ON;
		//delay_loop1();
		//LED0 = LED_OFF;
		//LED1 = LED_OFF;
		//LED2 = LED_OFF;
		//LED3 = LED_OFF;
		//delay_loop1();
		//nrf24_tx_address((uint8_t*) (uartBuffer + 1));
		data_array[0] = 0x00;
		data_array[1] = 0xAA;
		data_array[2] = 0x55;
		data_array[3] = 0x22;
		/* Automatically goes to TX mode */
		nrf24_send(data_array,4);
		/* Wait for transmission to end */
		while (nrf24_isSending());
		/* Make analysis on last tranmission attempt */
		temp = nrf24_lastMessageStatus();
		if (temp == NRF24_TRANSMISSON_OK)
		{
			//Do something
		}
		else if (temp == NRF24_MESSAGE_LOST)
		{
			//Do something else
		}
		/* Retranmission count indicates the tranmission quality */
		/* Optionally, go back to RX mode ... */
		//nrf24_powerUpRx();
		/* Or you might want to power down after TX */
		// nrf24_powerDown();
		/* Wait a little ... */
		//_delay_ms(10);

	}


}
Example #4
0
static int
nrf24_prepare(const void *payload, unsigned short payload_len)
{
	//This function is first called
	//For now lets assume that we transfer smaller byte chunks
	//And remove fragmentation
	int i=0;
	length_arr=payload_len;
	for(i=0;i<127;i++)
		{
			data[i]=0;
		}

	for(i=0;i<payload_len;i++)
	{
		data[i]=((uint8_t *)payload)[i];
	}
	asm("nop");
	nrf24_send(payload,payload_len);
	return 1;
}
Example #5
0
int main() {
 DDRB |= 1<<PORTB3;
 PORTB |= 1<<PORTB3;

 ledPort |= _BV(ledPin);

 /* init hardware pins */
 nrf24_init();

 /* Channel #2 , payload length: 4 */
 nrf24_config(2,4);

 /* Set the device addresses */
 nrf24_tx_address(tx_address);
 nrf24_rx_address(rx_address);

 pwm_init();   // initialize timer in PWM mode
 interupt_init();  //initalize Interrupt 0

 /* Fill the data buffer */
 data_array[0] = deviceID;
 data_array[1] = 0xAA;

 while(1) {
  data_array[3] = events;
  data_array[2] = events >> 8;
  nrf24_send(data_array);

  /* Wait for transmission to end */
  while(nrf24_isSending()) {
  }

  ledPort ^= _BV(ledPin);
  _delay_ms(80000);
 }
}
Example #6
0
/* ------------------------------------------------------------------------- */
int main()
{
    /* init the software uart */
    uart_init();

    /* init the xprintf library */
    xdev_out(uart_put_char);

    /* simple greeting message */
    xprintf("\r\n> TX device ready\r\n");
    
    /* init hardware pins */
    nrf24_init();
    
    /* Channel #2 , payload length: 4 */
    nrf24_config(2,4);

    /* Set the device addresses */
    nrf24_tx_address(tx_address);
    nrf24_rx_address(rx_address);    

    while(1)
    {                
        /* Fill the data buffer */
        data_array[0] = 0x00;
        data_array[1] = 0xAA;
        data_array[2] = 0x55;
        data_array[3] = q++;                                    

        /* Automatically goes to TX mode */
        nrf24_send(data_array);        
        
        /* Wait for transmission to end */
        while(nrf24_isSending());

        /* Make analysis on last tranmission attempt */
        temp = nrf24_lastMessageStatus();

        if(temp == NRF24_TRANSMISSON_OK)
        {                    
            xprintf("> Tranmission went OK\r\n");
        }
        else if(temp == NRF24_MESSAGE_LOST)
        {                    
            xprintf("> Message is lost ...\r\n");    
        }
        
		/* Retranmission count indicates the tranmission quality */
		temp = nrf24_retransmissionCount();
		xprintf("> Retranmission count: %d\r\n",temp);

		/* Optionally, go back to RX mode ... */
		nrf24_powerUpRx();

		/* Or you might want to power down after TX */
		// nrf24_powerDown();            

		/* Wait a little ... */
		_delay_ms(10);
    }
}
Example #7
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
  static u8 status;
  bool flag_display_retrans_cnt;
  disableInterrupts();
  Config();
  Errors_Init();
  enableInterrupts();
  Goto_HALT();
  
  while (1)
  {
    if((btn_pressed != BUTTON1) && (btn_pressed != BUTTON2))
    {
      DELAY_STOP;
      goto sleep;
    }
    
    PWR_PVDCmd(ENABLE);                    /* Power voltage detector and brownout Reset unit supply current 2,6uA */
    PWR_PVDLevelConfig(PWR_PVDLevel_2V26); /* Set Programmable voltage detector threshold to 2,26V */
    if(DELAY_EXPIRED)
      flag_display_retrans_cnt = TRUE;
    else 
      flag_display_retrans_cnt = FALSE;
    while(!DELAY_EXPIRED);           /* wait for power-up delay to expire (199.68ms) - needed for nRF24L01+ power up */
    DELAY_STOP;
    
    CLK_PeripheralClockConfig(CLK_Peripheral_SPI1, ENABLE);
    nrf24_init();                          /* init hardware pins */
    nrf24_config(0, 6);                    /* Channel #0 , payload length: 6 */
    nrf24_tx_address(tx_address);          /* Set the device addresses */
    nrf24_rx_address(rx_address);
    
    /* Check NRF24 Init error */
    if(Errors_CheckError(ERROR_NRF24_INIT))
    {
      LED_Red_Blink(2);
      goto sleep;
    }    

    TXmsg.M.status = 0;                    /* Reset status byte */
    
    /* Check Programmable voltage detector (PVD) output flag, if battery low send this information */
    /* Internal reference voltage should be stable at this point */
    if(PWR->CSR1 & PWR_FLAG_PVDOF)
    {
      /* VDD/VDDA is below the VPVD threshold (2,26V) */
      TXmsg.M.status |= STATUS_LOWBATT;
    }
    PWR_PVDCmd(DISABLE);
    
    if(btn_pressed == BUTTON1)
    {
      TXmsg.M.ownID = (u32)ownID_btn1;
      TXmsg.M.txcnt = tx_cnt++;
      nrf24_send((u8*)&TXmsg.B.b[0]);
    }
    else if(btn_pressed = BUTTON2)
    {
      TXmsg.M.ownID = (u32)ownID_btn2;
      TXmsg.M.txcnt = tx_cnt++;
      nrf24_send((u8*)&TXmsg.B.b[0]);
    }
    btn_pressed = 0;

    do
    {
      status = nrf24_getStatus();
    }
    while(!(status & ((1 << TX_DS)  | (1 << MAX_RT))));

    if(status & (1 << TX_DS))  /* send successful */
    {
      if(flag_display_retrans_cnt)
      {
        u8 i;
        u8 retrans_cnt = nrf24_retransmissionCount();
        LED_Green_Blink(retrans_cnt);
      }
      else
      {
        LED_Green_Blink(1);
      }
    }
    else if(status & (1 << MAX_RT))  /* send failed, max retries exceeded */
    {
      LED_Red_Blink(1);
    }
    
    sleep:
    Goto_HALT();
  }
}
Example #8
0
File: main.c Project: ksrm/redox
int main() {

    uint8_t i, change;
    uint8_t hand;
    uint32_t timeout = 0;

    uart_init();
    xdev_out(uart_putchar);

    // Determine which hand this is from PE2
    // Left is hand 0, right is hand 1
    PORTE = (1 << 2);
    DDRE = 0;
    hand = (PINE & 0x04) ? 0 : 1;
    xprintf("\r\nHand %d\r\n", hand);
    
    // Initialise NRF24
    // Set the last byte of the address to the hand ID
    rx_address[4] = hand;
    nrf24_init();
    nrf24_config(CHANNEL, sizeof msg);
    nrf24_tx_address(tx_address);
    nrf24_rx_address(rx_address);

    matrix_init();

    msg[0] = hand & 0x01;
    msg[1] = 0;
    msg[2] = 0;

    // Set up LED and flash it briefly
    DDRE |= 1<<6;
    PORTE = 1<<6;
    _delay_ms(500);
    PORTE = 0;

    get_voltage();
    check_voltage();

    // Scan the matrix and detect any changes.
    // Modified rows are sent to the receiver.
    while (1) {  
        timeout++;
        matrix_scan();

        for (i=0; i<ROWS; i++) {
            change = matrix_prev[i] ^ matrix[i];

            // If this row has changed, send the row number and its current state
            if (change) {
                if (DEBUG) xprintf("%d %08b -> %08b   %ld\r\n", i, matrix_prev[i], matrix[i], timeout);
                msg[1] = i;
                msg[2] = matrix[i];

                nrf24_send(msg);
                while (nrf24_isSending());
                timeout = 0;
            }

            matrix_prev[i] = matrix[i];
        }

        // Sleep if there has been no activity for a while
        if (timeout > SLEEP_TIMEOUT) {
            timeout = 0;
            enter_sleep_mode();
        }
    }

}
Example #9
0
int main(void)
{
  /* Initialise the GPIO block */
  gpioInit();

  /* Initialise the UART0 block for printf output */
  //uart0Init(115200);
  uart0Init(9600);
  /* Configure the multi-rate timer for 1ms ticks */
  mrtInit(__SYSTEM_CLOCK/1000);

  /* Configure the switch matrix (setup pins for UART0 and GPIO) */
  //configurePins();
  spiInit(LPC_SPI0, 6, 0);
  SwitchMatrix_Init(); //uart & spi
  LPC_GPIO_PORT->DIR0 |= (1 << CSN);
  LPC_GPIO_PORT->DIR0 |= (1 << CE);
  uint8_t temp = 0;

  nrf24_init();




  /* Set the LED pin to output (1 = output, 0 = input) */
  #if !defined(USE_SWD)
    LPC_GPIO_PORT->DIR0 |= (1 << LED_LOCATION);
  #endif

    //printf("write");
    mrtDelay(500);

    nrf24_config(2,16);

	#if TX_NODE
    	nrf24_tx_address(tx_address);
    	nrf24_rx_address(rx_address);
	#else
    	nrf24_tx_address(rx_address); //backwards looking but is fine
    	nrf24_rx_address(tx_address);
	#endif

    uint8_t i = 0;
  while(1)
  {

      /* Turn LED On by setting the GPIO pin high */
      LPC_GPIO_PORT->SET0 = 1 << LED_LOCATION;
      mrtDelay(500);

      /* Turn LED Off by setting the GPIO pin low */
      LPC_GPIO_PORT->CLR0 = 1 << LED_LOCATION;
      mrtDelay(500);

	  /*
      printf("Send\r\n");
      for(i=0; i<16; i++){
    	  printf("%d: %d\n\r", i, tx_data_array[i]);
      }
	   */
      nrf24_send(tx_data_array);
      while(nrf24_isSending());
      nrf24_powerUpRx();
      //done transmitting, set back to rx mode

      for(i=0; i<16; i++){
    	  tx_data_array[i]++;
      }

/*
      if(nrf24_dataReady()){
    	  printf("\n\rGot data\n\r");

          nrf24_getData(rx_data_array);
          for(i=0; i<16; i++){
          printf("%d: %d\n\r", i, rx_data_array[i]);
          }
      }
*/
  }
}
Example #10
0
void UpdateManualMode(volatile InputState *input) {
    // Get joystick X/Y
    // NB: Joystick is rotated on board!
    uint16_t x = input->joystick.x;
    uint16_t y = input->joystick.y;
    // Determine direction to go
    // Set motor values.
    // NB: Make sure not to override the motors!

    const uint16_t deadzone = 20;

    // How to determine direction and speed:
    // X axis determines direction
    //   --> Rotate control vector
    // Y axis determines speed

    int32_t vec_x = ((int32_t)x - 0x3FF/2);
    int32_t vec_y = ((int32_t)y - 0x3FF/2);

    vec_y *= -1;

    int16_t motor_left = 0;
    int16_t motor_right = 0;

    const int32_t vec_max = 0x3FF / 2;
    const int32_t motor_max = 0xFF;

    int16_t vec_x_abs = (uint16_t)abs(vec_x);
    if (vec_x_abs >= deadzone) {
        if (vec_x < 0) {
            // Turn left
            motor_left = (int16_t)(-vec_x_abs * motor_max / vec_max);
            motor_right = (int16_t)(vec_x_abs * motor_max / vec_max);
        } else {
            // Turn right
            motor_left = (int16_t)(vec_x_abs * motor_max / vec_max);
            motor_right = (int16_t)(-vec_x_abs * motor_max / vec_max);
        }
    }

    int16_t motor_left_f = 0;
    int16_t motor_right_f = 0;
    int16_t vec_y_abs = (int16_t)abs(vec_y);
    if (vec_y_abs >= deadzone) {
        motor_left_f = (vec_y_abs * motor_max / vec_max);
        motor_right_f = (vec_y_abs * motor_max / vec_max);
        if (vec_y > 0) {
            // Go backwards
            motor_left_f *= -1;
            motor_right_f *= -1;
        }
    }

    motor_left += motor_left_f;
    motor_right += motor_right_f;

    if (motor_left > 0xFF) {
        motor_left = 0xFF;
    } else if  (motor_left < -0xFF) {
        motor_left = -0xFF;
    }

    if (motor_right > 0xFF) {
        motor_right = 0xFF;
    } else if (motor_right < -0xFF) {
        motor_right = -0xFF;
    }

    if (manual_prev.initialized && (manual_prev.motor_left != motor_left || manual_prev.motor_right != motor_right)) {
        // Send updated signal
        RemoteCommand toSend;
        if (motor_left == 0 && motor_right == 0) {
            toSend.cmd = RADIO_CMD_STOP;
            printf("R: STOP\n");
        } else {
            toSend.cmd = RADIO_CMD_SET_MOTOR;
            toSend.data[0] = motor_left >= 0 ? FORWARD : BACKWARD;
            toSend.data[1] = (uint8_t)abs(motor_left);
            toSend.data[2] = motor_right >= 0 ? FORWARD : BACKWARD;
            toSend.data[3] = (uint8_t)abs(motor_right);

            printf("D: %d, %d\n", motor_left, motor_right);
        }
        nrf24_send(&toSend, sizeof(RemoteCommand));
    }

    // Handle light switch cmd
    if (!(input->buttons & BUTTON_LEFT)) {
        printf("LIGHT ON\n");
        RemoteCommand c;
        c.cmd = RADIO_CMD_LIGHT_ON;
        nrf24_send(&c, sizeof(RemoteCommand));
    }
    if (!(input->buttons & BUTTON_RIGHT)) {
        printf("LIGHT OFF\n");
        RemoteCommand c;
        c.cmd = RADIO_CMD_LIGHT_OFF;
        nrf24_send(&c, sizeof(RemoteCommand));
    }

    manual_prev.initialized = 1;
    manual_prev.motor_left = motor_left;
    manual_prev.motor_right = motor_right;
    // Debug output:
    // printf("D: %d %d\n", motor_left, motor_right);

}