Esempio n. 1
0
// Receive a string from the USB serial port.  The string is stored
// in the buffer and this function will not exceed the buffer size.
// A carriage return or newline completes the string, and is not
// stored into the buffer.
// The return value is the number of characters received, or 255 if
// the virtual serial connection was closed while waiting.
//
uint8_t recv_str(char *buf, uint8_t size)
{
	int16_t r;
	uint8_t count=0;

	while (count < size) {
		r = usb_serial_getchar();
		if (r != -1) {
			if (r == '\r' || r == '\n') return count;
			if (r >= ' ' && r <= '~') {
				*buf++ = r;
				usb_serial_putchar(r);
				count++;
			}
		} else {
			if (!usb_configured() ||
			  !(usb_serial_get_control() & USB_SERIAL_DTR)) {
				// user no longer connected
				return 255;
			}
			// just a normal timeout, keep waiting
		}
	}
	return count;
}
int16_t VncServerGetData(uint8_t * buffer, uint16_t maxsize)
{
    uint16_t size = 0;

    if(lastControlState != usb_serial_get_control())
    {
        lastControlState = usb_serial_get_control();

        if(usb_serial_get_control() & USB_SERIAL_DTR)
            usb_serial_set_control(USB_SERIAL_DSR);
        else
            usb_serial_set_control(0);

        // return error if a disconnect from the VNC server is detected
        if(lastControlState == 0)
            return -1;
    }

    while(debugcounter > 64)
    {
        DDRF |= (1 << 0);
        PORTF |= (1 << 0);
        debugcounter -= 64;
        PORTF &= ~(1 << 0);
    }

    while ( size < maxsize )
    {
        int n = usb_serial_getchar();

        if(n < 0)
            break;

        buffer[size++] = (uint8_t)n;
        debugcounter++;
    }

    return size;
}
Esempio n. 3
0
// Basic command interpreter for controlling port pins
int main(void)
{
	char buf[32];
	uint8_t n;

	// set for 16 MHz clock, and turn on the LED
	CPU_PRESCALE(0);
	LED_CONFIG;
	LED_ON;

	// initialize the USB, and then wait for the host
	// to set configuration.  If the Teensy is powered
	// without a PC connected to the USB port, this 
	// will wait forever.
	usb_init();
	while (!usb_configured()) /* wait */ ;
	_delay_ms(1000);

	while (1) {
		// wait for the user to run their terminal emulator program
		// which sets DTR to indicate it is ready to receive.
		while (!(usb_serial_get_control() & USB_SERIAL_DTR)) /* wait */ ;

		// discard anything that was received prior.  Sometimes the
		// operating system or other software will send a modem
		// "AT command", which can still be buffered.
		usb_serial_flush_input();

		// print a nice welcome message
		send_str(PSTR("\r\nTeensy USB Serial Example, "
			"Simple Pin Control Shell\r\n\r\n"
			"Example Commands\r\n"
			"  B0?   Read Port B, pin 0\r\n"
			"  C2=0  Write Port C, pin 1 LOW\r\n"
			"  D6=1  Write Port D, pin 6 HIGH  (D6 is LED pin)\r\n\r\n"));

		// and then listen for commands and process them
		while (1) {
			usb_serial
			send_str(PSTR("> "));
			n = recv_str(buf, sizeof(buf));
			if (n == 255) break;
			send_str(PSTR("\r\n"));
			parse_and_execute_command(buf, n);
		}
	}
}
Esempio n. 4
0
//function call from main, 
void usb_terminal(uint16_t *events, uint8_t *state)
{
	if (usb_configured() &
	    usb_serial_get_control() &
	    USB_SERIAL_DTR){ 	//only initiate usage of serial terminal if  
							//PC is connected and ready to receive.
		
		usb_serial_flush_input(); //discard anything received prior
		get_inputs(events); //get snapshot of events
		
		send_str(PSTR("\r\nSupervisory Computer Serial"
					  "Terminal Interface\r\n\r\n"
					  "state : print current vehicle state\r\n"
					  "event=HV_UP : set event to HV_UP\r\n"));
		
	}
}
Esempio n. 5
0
/**
 * Main - Run the main program which prints the system time and flashes the LEDs
 * if certain conditions are met
 */
int main() {
    // Setup the hardware
    set_clock_speed(CPU_8MHz);
    init_hardware();

    // Wait until the 'debugger' is attached...
    draw_centred(17, "Waiting for");
    draw_centred(24, "debugger...");
    show_screen();
    while(!usb_configured() || !usb_serial_get_control());
	send_debug_string("Debugger initialised. Debugging strings will appear below:");

    // Run the main loop displaying the system time @ ~10Hz...
    char buff[BUFF_LENGTH];
    unsigned long count = 0;
	send_debug_string("Entering main loop...");
    while (1) {
        // Draw the current system time on the screen
        clear_screen();
        sprintf(buff, "%7.4f", get_system_time());
        draw_centred(21, buff);
        if (count < 1) { send_debug_string("Calling show_screen()..."); }
		show_screen();
        if (count < 1) { send_debug_string("Finished show_screen()."); }
		_delay_ms(100);

        // Toggle LEDs if the conditions are met
        if ((count % 25) == 0) {
            PORTB ^= (1 << PB2);
			send_debug_string("LED0 was toggled.");
        }
        if ((count % 50) == 10) {
            PORTB ^= (1 << PB3);
			send_debug_string("LED1 was toggled.");
        }

        // Increment the loop count
        count++;
		
		enter_breakpoint(99);
    }

    // We'll never get here...
    return 0;
}
Esempio n. 6
0
int main(void)
{
	Randomizer randomizer;
	// set for 16 MHz clock, and turn on the LED
	CPU_PRESCALE(0);
	LED_CONFIG;
	LED_ON;

	// initialize the USB, and then wait for the host
	// to set configuration.  If the Teensy is powered
	// without a PC connected to the USB port, this 
	// will wait forever.
	usb_init();
	while (!usb_configured()) /* wait */ ;
	_delay_ms(1000);
	
	while (1) {
		// wait for the user to run their terminal emulator program
		// which sets DTR to indicate it is ready to receive.
		while (!(usb_serial_get_control() & USB_SERIAL_DTR)) /* wait */ ;

		// discard anything that was received prior.  Sometimes the
		// operating system or other software will send a modem
		// "AT command", which can still be buffered.
		usb_serial_flush_input();

		//Get 128 bits of debiased entropy from the ADCs (using Von Neumann debias)
		//and add that entropy to the randomizer (cryptographic entropy mixer).
		//Repeat 4 times.
        
		for(int i=0; i<4; i++){
			randomizer.add(Entropy::get_entropy()); 
		}
        
		//Get cryptographically mixed and decorrelated random data from the randomizer
		RandomData random_data = randomizer.get();
		for(int i=0; i<16; i++){
			//Send the random data over the USB serial device
			usb_serial_putchar(random_data.bytes[i]);
		}
		
		LED_TOGGLE;
	}
}
Esempio n. 7
0
/**
* Main - Run through the steps of configuring, greeting, getting a name, thanking,
* and then quitting
*/
int main(void) {
	char buff[BUFF_LENGTH];

	// Setup the hardware
	init_hardware();

	// Wait until the USB port is configured and ready to go
	draw_centred(17, "Waiting for");
	draw_centred(24, "computer...");
	show_screen();
	while (!usb_configured() || !usb_serial_get_control());

	// Prompt the user for their name, and wait until they enter it
	clear_screen();
	draw_centred(17, "Waiting for");
	draw_centred(24, "username...");
	show_screen();
	send_line("Hello!");
	send_line("Could you please tell me your name:");
	recv_line(buff, BUFF_LENGTH);
	usb_serial_putchar('\n');

	// Display their name on the Teensy and prompt them to exit
	char buff2[BUFF_LENGTH + 8];
	sprintf(buff2, "Thanks %s!", buff);
	clear_screen();
	draw_centred(21, buff2);
	show_screen();
	send_line("Press 'q' to exit...");
	while (usb_serial_getchar() != 'q');

	// Display the finished information
	clear_screen();
	draw_centred(21, "Goodbye!");
	show_screen();
	send_line("\r");
	send_line("Done! Goodbye!");
	while (1);

	// We'll never get here...
	return 0;
}
// Basic command interpreter for controlling port pins
int main(void)
{
	int16_t adc_result;

	char adc_result_str[10];

	// set for 16 MHz clock, and turn on the LED
	CPU_PRESCALE(0);
	LED_CONFIG;

	// Set PF0 as input for ADC
	DDRF &= ~(1 << 0);

	// initialize the USB, and then wait for the host
	// to set configuration.  If the Teensy is powered
	// without a PC connected to the USB port, this 
	// will wait forever.
	usb_init();
	while (!usb_configured()) /* wait */ ;
	_delay_ms(1000);

	while (1) {
		// wait for the user to run their terminal emulator program
		// which sets DTR to indicate it is ready to receive.
		while (!(usb_serial_get_control() & USB_SERIAL_DTR)) /* wait */ ;

		// discard anything that was received prior.  Sometimes the
		// operating system or other software will send a modem
		// "AT command", which can still be buffered.
		usb_serial_flush_input();

		adc_result = adc_read(0);	// Read ADC pin 0

		sprintf(adc_result_str, "ADC: %d  ", adc_result);

		usb_serial_write(adc_result_str, 9);

		_delay_ms(100);

	}
}
Esempio n. 9
0
// Basic command interpreter for controlling port pins
int main(void) {
	char buf[64];
	uint8_t n;

	// set for 16 MHz clock, and turn on the LED
	CPU_PRESCALE(0);
	LED_CONFIG;
	LED_ON;

	// initialize the USB, and then wait for the host
	// to set configuration.  If the Teensy is powered
	// without a PC connected to the USB port, this
	// will wait forever.
	usb_init();
	while (!usb_configured()) /* wait */ ;
	_delay_ms(1000);

	while (1) {
		// wait for the user to run their terminal emulator program
		// which sets DTR to indicate it is ready to receive.
		while (!(usb_serial_get_control() & USB_SERIAL_DTR)) /* wait */ ;

		// discard anything that was received prior.  Sometimes the
		// operating system or other software will send a modem
		// "AT command", which can still be buffered.
		// usb_serial_flush_input();

		// and then listen for commands and process them
		while (1) {
			// send_str(PSTR("> "));
			n = recv_str(buf, sizeof(buf));
			if (n == 255) break;
			// send_str(PSTR("\r\n"));
			parse(buf);
		}
	}
}
Esempio n. 10
0
// Basic command interpreter for controlling port pins
int main(void)
{
	char rx_buf[8];// the buffer to store received characters
	uint8_t n = 0;  // to store the number of bytes read from the serial port
    uint8_t counter = 0; //keep count of the number of cycles in the while loop
    // coordinates of the middle of the screen
    int x=LCD_X/2; 
    int y=LCD_Y/2;

	//set clock speed to 8MhZ
    set_clock_speed(CPU_8MHz);
    
    
    //initialise LCD and ports
	Init();

	// initialize the USB, and then wait for the host
	// to set configuration.  If the Teensy is powered
	// without a PC connected to the USB port, this 
	// will wait forever.
	usb_init();
	while (!usb_configured()) /* wait */ ;
	  _delay_ms(1000);


   //keep looping until the character 'q' is received
	while (rx_buf[0]!='q') {
        counter++;
		// wait for the user to run their terminal emulator program
		// which sets DTR to indicate it is ready to receive.
		while (!(usb_serial_get_control() & USB_SERIAL_DTR)) /* wait */ ;

		// discard anything that was received prior.  Sometimes the
		// operating system or other software will send a modem
		// "AT command", which can still be buffered.
		usb_serial_flush_input();


		// and then listen for commands and process them
		while (1) {
            
            //send some characters to the other side
             //send_str(PSTR("> \n"));
            
            if (usb_serial_available())
			  n = recv_str(rx_buf, sizeof(rx_buf)); //read serial port
			else
			  break;	 
            
            
            parse_and_execute_command(rx_buf, n);
            
            
            clear_screen();
            draw_string(x-40,y,"receiving: ");
            show_screen();
            draw_char(x+20,y,rx_buf[0]);
            show_screen();
            
		}
		
	}	 
	return 0;
}
Esempio n. 11
0
File: kitt.c Progetto: built/kitt
void wait_for_usb_connection()
{
    // wait for the user to run their terminal emulator program
    // which sets DTR to indicate it is ready to receive.
    while (!(usb_serial_get_control() & USB_SERIAL_DTR)) /* wait */ ;
}
Esempio n. 12
0
/** The MAIN loop. */
int main(void) {
  // Turn off the CPU prescale.
  CLKPR = 0x80;
  CLKPR = 0x00;

  // PORTA are general purpose inputs.
  DDRA = 0x00;
  PORTA = 0xff; // pull-ups all enabled

  PORTD = 0xff;
  DDRD = 0x00; // pull-ups all enabled

  DDRF = 0x00; // These are ADC lines.
  PORTF = 0x00;

  usb_init();

  // Set up Timer 0 to match compare every 1ms.
  OCR0A = 250;
  TCCR0A = 0x02; // CTC
  TCCR0B = 0x03; // CK/64 (64 * 250 == 16000)

  sei();
  char line_buf[128] = {};
  uint8_t line_len = 0;
  uint8_t usb_ready = 0;
  while (1) {
    wdt_reset();
    g_main_loop_count++;

    if (usb_configured() && (usb_serial_get_control() & USB_SERIAL_DTR)) {
      if (!usb_ready) {
        usb_ready = 1;
        strcpy_P(line_buf, PSTR("!GNR WELCOME " DEV_VERSION EOL));
        usb_serial_write((uint8_t*)line_buf, strlen(line_buf));
        line_len = 0;
      }
    } else {
      stream_stop_all();
      usb_serial_flush_input();
      usb_ready = 0;
      line_len = 0;
      g_arm_code = 0;
    }

    if (usb_serial_available()) {
      int16_t c = usb_serial_getchar();
      if (c == '\r' || c == '\n') {
        line_buf[line_len] = 0;
        handle_line(line_buf);
        if (g_arm_code) { g_arm_timer = ARM_TIMEOUT_MS; }
        line_len = 0;
      } else {
        line_buf[line_len++] = c & 0xff;

        if ((line_len + 1) >= sizeof(line_buf)) {
          /* Clobber the first byte so that this line will be reported
             as an error. */
          line_buf[0] = MAGIC_OVERRUN_CODE;
          line_len--;
        }
      }
    }

    if (TIFR0 & (1 << OCF0A)) {
      TIFR0 |= (1 << OCF0A);
      g_timer++;
      stream_timer_update();
      hit_timer_update();

      g_main_loop_count = (uint16_t) (((uint32_t) g_main_loop_count) * 7 / 8);
    }

    stream_poll();
    usb_poll();
    hit_poll();
  }
}
Esempio n. 13
0
//
//-----------------------------------------------------------------------------------------
// 			Setup Ports, timers, start the works and never return, unless reset
//								by the watchdog timer
//						then - do everything, all over again
//-----------------------------------------------------------------------------------------
//
int main(void)
{
	MCUSR &= ~(1 << WDRF);							// Disable watchdog if enabled by bootloader/fuses
	wdt_disable();

	clock_prescale_set(clock_div_1); 				// with 16MHz crystal this means CLK=16000000

	//------------------------------------------
	// 16-bit Timer1 Initialization
	TCCR1A = 0; //start the timer
	TCCR1B = (1 << CS12); // prescale Timer1 by CLK/256
	// 16000000 Hz / 256 = 62500 ticks per second
	// 16-bit = 2^16 = 65536 maximum ticks for Timer1
	// 65536 / 62500 = ~1.05 seconds
	// so Timer1 will overflow back to 0 about every 1 seconds
	// Timer1val = TCNT1; // get current Timer1 value

	//------------------------------------------
	// Init and set output for LEDS
	LED_DDR = LED;
	LED_PORT = 0;
	
	EXTLED_DDR = EXT_G_LED | EXT_R_LED;				// Init Green and Red LEDs
	EXTLED_PORT = 0;
	
	//------------------------------------------
	// Init Pushbutton input
	ENC_PUSHB_DDR = ENC_PUSHB_DDR & ~ENC_PUSHB_PIN;	// Set pin for input
	ENC_PUSHB_PORT= ENC_PUSHB_PORT | ENC_PUSHB_PIN;	// Set pull up

	//------------------------------------------
	// Set run time parameters to Factory default under certain conditions
	//
	// Enforce "Factory default settings" when firmware is run for the very first time after
	// a fresh firmware installation with a new "serial number" in the COLDSTART_REF #define
	// This may be necessary if there is garbage in the EEPROM, preventing startup
	// To activate, roll "COLDSTART_REF" Serial Number in the PM.h file
	if (eeprom_read_byte(&E.EEPROM_init_check) != R.EEPROM_init_check)
	{
		eeprom_write_block(&R, &E, sizeof(E));		// Initialize eeprom to "factory defaults".
	}
	else
	{
		eeprom_read_block(&R, &E, sizeof(E));		// Load the persistent data from eeprom
	}

   	uint8_t i2c_status = I2C_Init();				// Initialize I2C comms
   	
	lcd_Init();										// Init the LCD

	// Initialize the LCD bargraph, load the bargraph custom characters
	lcd_bargraph_Init();

	//------------------------------------------
	// LCD Print Version and I2C information (6 seconds in total during startup)
	lcdClear();
	lcdGotoXY(0,0);
	lcdPrintData(STARTUPDISPLAY1,strlen(STARTUPDISPLAY1));
	lcdGotoXY(0,1);
	lcdPrintData(STARTUPDISPLAY2,strlen(STARTUPDISPLAY2));
	_delay_ms(300);
	lcdGotoXY(20-strlen(STARTUPDISPLAY3),1);
	lcdPrintData(STARTUPDISPLAY3,strlen(STARTUPDISPLAY3));
	_delay_ms(200);
	lcdGotoXY(20-strlen(STARTUPDISPLAY4),2);
	lcdPrintData(STARTUPDISPLAY4,strlen(STARTUPDISPLAY4));
	_delay_ms(2500);

	lcdGotoXY(0,3);
	lcdPrintData(STARTUPDISPLAY5,strlen(STARTUPDISPLAY5));
	sprintf(lcd_buf,"V%s", VERSION);
	lcdGotoXY(20-strlen(lcd_buf),3);
	lcdPrintData(lcd_buf, strlen(lcd_buf));
	_delay_ms(2000);

	lcdGotoXY(0,3);
	if (i2c_status==1) lcdPrintData("AD7991-0 detected   ",20);
	else if (i2c_status==2) lcdPrintData("AD7991-1 detected   ",20);
	else lcdPrintData("Using built-in A/D  ",20);	// No I2C device detected, 
													// we will be using the builtin 10 bit ADs
													
	if (R.USB_data)									// Enumerate USB serial port, if USB Serial Data enabled
	{
		usb_init();									// Initialize USB communications
		Status&=~USB_AVAILABLE;						// Disable USB communications until checked if actually available
	}
	
	_delay_ms(1000);	
		
	//wdt_enable(WDTO_1S);							// Start the Watchdog Timer, 1 second
	
	encoder_Init();									// Init Rotary encoder

	Menu_Mode = DEFAULT_MODE;						// Power Meter Mode is normal default
	
	Status |= MODE_CHANGE | MODE_DISPLAY;			// Force a Display of Mode Intro when starting up
	
	// Start the works, we're in business
	while (1)
	{
		maintask();									// Do useful stuff
		
		if (R.USB_data)								// Do the below if USB Port has been enabled
		{
			// If USB port is available and not busy, then use it - otherwise mark it as blocked.
			if (usb_configured() && (usb_serial_get_control() & USB_SERIAL_DTR))
			{
				Status |= USB_AVAILABLE;			// Enable USB communications
				EXTLED_PORT |= EXT_G_LED;			// Turn Green LED On
				usb_read_serial();
			}
			else
			{
				Status&=~USB_AVAILABLE;				// Clear USB Available Flag to disable USB communications
				EXTLED_PORT &= ~EXT_G_LED;			// Turn Green LED off, if previously on
			}			
		}
	}
}
Esempio n. 14
0
/**
 * Main - Run the game which repeatedly loops through trying to find the gold
 */
int main() {
    // Setup the hardware
    set_clock_speed(CPU_8MHz);
    init_hardware();

    // Wait until the 'player' is attached...
    draw_centred(17, "Waiting for");
    draw_centred(24, "the player...");
    show_screen();
    while(!usb_configured() || !usb_serial_get_control());

    // Run the main game loop
    unsigned int seed = 0;
    while (1) {
        // Game start screen
        clear_screen();
        draw_centred(16, "--- LUCKY DIP ---");
        draw_centred(25, "'s' to start...");
        show_screen();
        send_line("--- LUCKY DIP ---");
        send_line("Press 's' to start...");

        // Wait until the key has been pressed (perform seeding only if first run)
        unsigned int seed_temp = 0;
        int16_t curr_char;
        do {
            curr_char = usb_serial_getchar();
            seed_temp++;
        } while (curr_char != 's');
        if (seed == 0) {
            seed = seed_temp;
            srand(seed);
        }
        usb_serial_write("\r\n", 2);

        // Set the gold location
        bury_gold();

        // Present the 9 closed boxes
        clear_screen();
        for (unsigned char i = 0; i<BOXES_W*BOXES_H; i++) {
            draw_box(i, 0);
        }
        show_screen();

        // Prompt the user to pick a box by pressing a number key until they find the gold
		send_line("Please enter a number between 1 and 9 to select a box...");
		int dinner = 0;
		while (!dinner) {
			// Get the users input.			
			int16_t input = usb_serial_getchar();
			
			// Convert the input to an intager to be used as an id.
			int box = (input == '1') ? 0 : (input == '2') ? 1 : (input == '3') ? 2 : (input == '4') ? 3 : (input == '5') ? 4 : (input == '6') ? 5 : (input == '7') ? 6 : (input == '8') ? 7 : (input == '9') ? 8 : -1;
			
			if (box != -1) {
				// Debugging - Display selected box (once it's converted to an id from the users input).
				char buff[20];
				sprintf(buff, "User has selected box #%d", box);
				(debug) ? send_debug_string(buff) : 0;
				
				// Is the selected box open? I should probably open it is it's not...
				if (is_open[box] == 1) {
					send_line("  - That box is already open!");
				} else {
					is_open[box] = 1;
					draw_box(box, 1);
				}
				
				// Did that box contain gold!?
				if (is_gold[box] == 1) {
					show_screen();
					dinner = 1;
				}
			}
			show_screen();
		}

        // Winner, winner, chicken dinner
        send_line("You found the $$$!");
        for (unsigned char i = 0; i<10; i++) {
            PORTB ^= (1 << PB2);
            PORTB ^= (1 << PB3);
            _delay_ms(250);
        }
    }

    // We'll never get here...
    return 0;
}
Esempio n. 15
0
// Basic command interpreter for controlling port pins
int main(void)
{
	char rx_buf[8];// the buffer to store received characters
    uint8_t n=0;  //number of bytes read


	//set clock speed to 8MhZ
    set_clock_speed(CPU_8MHz);
    
    
    //initialise LCD and ports
	Init();

	// initialize the USB, and then wait for the host
	// to set configuration.  If the Teensy is powered
	// without a PC connected to the USB port, this 
	// will wait forever.
	usb_init();
	while (!usb_configured()) /* wait */ ;
	  _delay_ms(1000);


   //keep looping until the character 'q' is received
	while (rx_buf[0]!='q') {
        
		// wait for the user to run their terminal emulator program
		// which sets DTR to indicate it is ready to receive.
		while (!(usb_serial_get_control() & USB_SERIAL_DTR)) /* wait */ ;

		// discard anything that was received prior.  Sometimes the
		// operating system or other software will send a modem
		// "AT command", which can still be buffered.
		usb_serial_flush_input();


		// and then listen for commands and process them
		while (1) {
            
            if (usb_serial_available())
            {
			   n = recv_str(rx_buf, sizeof(rx_buf)); //read serial port
			   //send a characters to the other side
               send_str(PSTR("> \n"));
			}
			else
			{
			  break;
			}	 
          	    
            //check first character of the buffer and perform an action
            if(rx_buf[0]=='a') //turn LED0 ON
                PORTB = 1<<2;
            if(rx_buf[0]=='d') //turn LED1 ON
            	PORTB = 1<<3;
            if(rx_buf[0]=='s') //turn LED0 OFF
            	PORTB = 0x00;
                         
		}
		
	}	 
	return 0;
}