Example #1
0
void tests_bt2(void) {
  /*int i;
   */

  hello();

  /* Configuring the BT */

  nx_bt_init();

  nx_display_clear();
  nx_display_string("Setting friendly name ...");
  nx_display_end_line();

  nx_bt_set_friendly_name("tulipe");

  nx_display_string("Setting as discoverable ...");
  nx_display_end_line();

  nx_bt_set_discoverable(TRUE);

  /* Listing known devices */

  tests_bt_list_known_devices();

  /* Scanning & adding */

  tests_bt_scan_and_add();

  /* Listing known devices */

  tests_bt_list_known_devices();


  /* Scanning & removing */

  tests_bt_scan_and_remove();


  /* Listing known devices */

  tests_bt_list_known_devices();


  /*
  for (i = 0 ; i < 10 ; i++)
    {
      nx_display_clear();
      nx_bt_debug();

      nx_systick_wait_ms(1000);
    }
  */

  goodbye();
}
Example #2
0
void main (void) {
	UWORD tbc_length 	= 0;
	BYTE *tbc_data 		= NULL;
	tbc_t *tbc 		= NULL;
		
	nx_systick_wait_ms (1000);
	nx__avr_init ();
	nx_systick_wait_ms (1000);
	
	nx_bt_init ();

	tvm_init (&tvm);

	for (;;) {
		U8 buffer[NX_USB_PACKET_SIZE];
		WORD usb 	= 0;
		U32 pos		= 0;
		U32 i;
		int running 	= 1;

		nx_display_clear ();
		nx_display_string ("I am the TVM.");
		nx_display_end_line ();

		if (tbc != NULL) {
			nx_display_string ("OK to reload.");
			nx_display_end_line ();
		}

		nx_usb_read (buffer, NX_USB_PACKET_SIZE);
		while (usb == 0) {
			if ((pos = nx_usb_data_read ()) >= 8) {
				if ((tbc_length = valid_tbc_header (buffer))) {
					tbc_length 	+= 8;
					tbc_data 	= (BYTE *) mem_pool;
					tbc		= NULL;
					for (i = 0; i < pos; ++i) {
						tbc_data[i] = buffer[i];
					}
					usb = tbc_length - pos;
				}
			} else {
				switch (nx_avr_get_button ()) {
					case BUTTON_OK:
						if (tbc != NULL) {
							usb = -1;
						}
						break;
					case BUTTON_CANCEL:
						nx__avr_power_down ();
						break;
					default:
						nx_systick_wait_ms (100);
						break;
				}
			}
		}

		if (usb > 0) {
			nx_display_cursor_set_pos (0, 1);
			nx_display_string ("Got header (");
			nx_display_uint (tbc_length);
			nx_display_string (")");
			nx_display_end_line ();
			
			nx_display_cursor_set_pos (0, 2);
			nx_display_uint (usb);
			nx_display_string ("      ");
		} else {
			nx_display_string ("Reload TBC (");
			nx_display_uint (tbc_length);
			nx_display_string (")");
			nx_display_end_line ();
		}

		if (usb > 0) {
			nx_usb_write ((U8 *) &pos, 4);
		}

		while (usb > 0) {
			U32 tmp;
			
			if (usb >= NX_USB_PACKET_SIZE) {
				nx_usb_read (&(tbc_data[pos]), NX_USB_PACKET_SIZE);
			} else {
				nx_usb_read (&(tbc_data[pos]), usb);
			}

			while (!(tmp = nx_usb_data_read ()))
				continue;

			pos += tmp;
			usb -= tmp;
			
			nx_display_cursor_set_pos (0, 2);
			nx_display_uint (usb);
			nx_display_string ("      ");
			
			nx_usb_write ((U8 *) &pos, 4);
		}
		
		nx_display_cursor_set_pos (0, 2);
		nx_display_string ("Got TBC.    ");
		nx_display_end_line ();
		nx_systick_wait_ms (200);

		tvm_ectx_init (&tvm, &context);
		context.mem_pool = mem_pool + tbc_length;
		context.get_time = nxt_get_time;
		context.modify_sync_flags = nxt_modify_sync_flags;
		context.sffi_table = sffi_table;
		context.sffi_table_length = sffi_table_length;
		
		tlsf_init_memory_pool (NX_USERSPACE_SIZE - tbc_length, 
				(void *) context.mem_pool);
		
		if ((tbc = load_context_with_tbc (&context, tbc, tbc_data, tbc_length)) == NULL) {
			nx_display_string ("Decode failed!");
			nx_systick_wait_ms (3000);
			continue;
		}

		nx_display_string ("Running...");
		//nx_display_end_line ();
		nx_systick_wait_ms (1000);
		//nx_display_clear ();
		
		while (running) {
			int ret = tvm_run (&context);
			
			switch (ret) {
				case ECTX_PREEMPT:
				case ECTX_TIME_SLICE: {
					/* Safe to continue. */
					break;
				}
				case ECTX_SLEEP: {
					WORD next = context.tnext;
					WORD now = nxt_get_time (&context);
					while (TIME_AFTER (next, now)) {
						nx_systick_wait_ms (next - now);
						now = nxt_get_time (&context);
					}
					break;
				}
				case ECTX_INTERRUPT: {
					//clear_pending_interrupts ();
					break;
				}
				case ECTX_EMPTY: {
					//if (!waiting_on_interrupts ()) {
						//terminate("deadlock", NULL);
					//}
					nx_display_end_line ();
					nx_display_string ("Deadlock.");
					break;
				}
				case ECTX_SHUTDOWN: {
					nx_display_end_line ();
					nx_display_string ("End of program.");
					running = 0;
					break;
				}
				default: {
					nx_display_end_line ();
					nx_display_string ("Error = ");
					nx_display_uint (ret);
					running = 0;
					break;
				}
			}
		}
		
		for (i = 0; i < 3; ++i)
			nx_motors_stop (i, FALSE);
		for (i = 0; i < 4; ++i)
			nx__sensors_disable (i);
		nx_systick_wait_ms (3000);
	}

	/* NOTREACHED */
}
Example #3
0
void tests_bt(void) {
  U16 i;
  U16 lng = 0;
  int port_handle __attribute__ ((unused));
  int connection_handle = -1;
  int bleh = -1;

  char buffer[BT_PACKET_SIZE];

  port_handle = -1;

  for (i = 0 ; i < BT_PACKET_SIZE ; i++)
    buffer[i] = 0;
  lng = 0;

  nx_bt_init();

  hello();

  nx_display_cursor_set_pos(0, 0);
  nx_display_string("Setting discoverable ...");
  nx_bt_set_friendly_name("Tulipe");
  nx_bt_set_discoverable(TRUE);

  port_handle = nx_bt_open_port();

  nx_display_clear();
  nx_display_cursor_set_pos(0, 0);
  nx_display_string("Waiting connection ...");

  while(TRUE) {

    for (i = 0 ;
         i < 800 && (!nx_bt_stream_opened() || nx_bt_stream_data_read() < 2);
         i++)
      {
        if (connection_handle >= 0) {

          nx_display_cursor_set_pos(0, 2);
          nx_display_string("Waiting command ...");

        }

        if (nx_bt_has_dev_waiting_for_pin()) {

          nx_bt_send_pin("1234");

        } else if (nx_bt_connection_pending()) {

          nx_bt_accept_connection(TRUE);

          while ( (bleh = nx_bt_connection_established()) < 0)
            nx_systick_wait_ms(100);

          connection_handle = bleh;

          nx_bt_debug();

          nx_bt_stream_open(connection_handle);

          nx_bt_debug();

          nx_bt_stream_read((U8 *)&buffer, 2); /* we read the packet size first */

          nx_display_cursor_set_pos(0, 4);

        }

        nx_bt_debug();
        nx_systick_wait_ms(100);
      }

    if (!nx_bt_stream_opened() || i >= 800)
      break;

    lng = buffer[0] + (buffer[1] << 8);

    nx_display_clear();
    nx_display_cursor_set_pos(0, 0);
    nx_display_string("Reading ...");
    nx_display_cursor_set_pos(0, 1);
    nx_display_uint(lng);

    nx_bt_stream_read((U8 *)&buffer, lng);

    for(i = 0;
        nx_bt_stream_opened() && nx_bt_stream_data_read() < lng && i < 100;
        i++)
      nx_systick_wait_ms(100);

    if (!nx_bt_stream_opened() || i >= 100)
      break;

    /* Start interpreting */

    i = tests_command(buffer);

    nx_bt_stream_read((U8 *)&buffer, 2);

    if (i == 2) {
      break;
    }

    /*
    if (i == 1) {
      nx_bt_stream_write((U8 *)CMD_UNKNOWN, sizeof(CMD_UNKNOWN)-1);
    }

    if (i == 0) {
      nx_bt_stream_write((U8 *)CMD_OK, sizeof(CMD_OK)-1);
    }
    */

    nx_systick_wait_ms(100);
    nx_display_clear();

  }

  if (nx_bt_stream_opened())
    nx_bt_stream_close();

  nx_systick_wait_ms(1000);

  nx_bt_debug();

  goodbye();
}