Beispiel #1
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;
}
Beispiel #2
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;
}
Beispiel #3
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;
}