Beispiel #1
0
CAL_MAIN()
{
	wdt_enable(WDTO_4S);

	LCD_UpdateSOC(1);

	stdout = &mystdout;

	DDRD |= (1 << PD4); PORTD &= ~(1 << PD4); // Turn on RS232.

	USART_Init();

	uint8_t ch;

	LCD_UpdateSOC(2);
        
	// Initialize everything.
	InitMemory();

	LCD_UpdateSOC(3);

	LCD_Init();

	LCD_UpdateSOC(4);

	TIMING_Init();

	LCD_UpdateSOC(5);

	RTC_Init();

	LCD_UpdateSOC(6);

	RTC_SetTickHandler( TIMING_TickHandler );

	LCD_UpdateSOC(7);

	JOYSTICK_Init( 100, 50 );

	//DDRD |= (1 << PD4); PORTD &= ~(1 << PD4); // Turn on RS232.

	//USART_Init();

//	CAL_enable_interrupt();

	TIMING_AddRepCallbackEvent( TIMING_INFINITE_REPEAT, 1, JOYSTICK_PollingHandler, &joystickCallbackEvent );

	// happy startup sound
	LCD_UpdateSOC(8);

	// BATTERY ICON SOC-LEVEL TEST
	int soc = 0;

	// interrupt enable
	SREG |= 1<<7;

	LCD_UpdateSOC(9);

	// Init CAN-adapter
	// few returns to wake up the device
	/*
	printf("\r\r");
	
	// version query
	printf("V\r");
	
	// auto poll/send = ON
	printf("X1\r");
	
	printf("S6\r");	// (S)peed 500 kBit
	
	// Open the CAN channel
	printf("O\r");	// (O)pen the CAN-bus
	*/

	LCD_UpdateSOC(10);

	PlaySound(11);

	// Display splash screen, wait for joystick.

	//1 POWER_EnterIdleSleepMode();

	//PICTURE_CopyFullscreenFlashToLcd( FLASHPICS_excellenceThroughTechnology );
	//PICTURE_CopyFullscreenFlashToLcd( FLASHPICS_eCarsLogo );
//	PICTURE_CopyFullscreenFlashToLcd( FLASHPICS_PalonenLABS_128x64px );

	// init backlight
	BACKLIGHT_Init();
	Contrast = eeprom_read_word((uint16_t*)8);
	Red = eeprom_read_word((uint16_t*)10);
	Green = eeprom_read_word((uint16_t*)12);
	Blue = eeprom_read_word((uint16_t*)14);
	Intensity = eeprom_read_word((uint16_t*)16);

	BACKLIGHT_SetRGB( Red, Green, Blue );
	BACKLIGHT_SetIntensity(Intensity);
/*
	TIMING_event_t * splashScreenEvent = MEM_ALLOC( TIMING_event_t );
	if (splashScreenEvent == NULL) { UnknownError(); }
	TIMING_counter_t volatile splashCounter = 0;
	TIMING_AddCounterEventAfter( RTC_TICKS_PER_SECOND * SPLASH_SCREEN_SECONDS,
			&splashCounter, splashScreenEvent );
	bool exit = false;
	do {
		POWER_EnterIdleSleepMode();
		if (JOYSTICK_GetState() != 0x00) { exit = true; };
		if (splashCounter != 0) { exit = true; }
	} while (exit == false);


	TIMING_RemoveEvent( splashScreenEvent );
	MEM_FREE( splashScreenEvent );

	DELAY_MS(500);
*/
	LCD_ClrBox(0,0,128,64);

//	exit = false;	
	/*
	do {
		uint8_t joystickState = JOYSTICK_GetState();
		while ((FIFO_HasData( &rxFifo, FIFO_data_t ) == false) && (joystickState == 0x00)) {
			POWER_EnterIdleSleepMode();
			joystickState = JOYSTICK_GetState();
		}
		
		if (joystickState != 0x00) {
			exit = true;
		}
		else if (FIFO_HasData( &rxFifo, FIFO_data_t ) == true) {
			FIFO_data_t charInput;
			FIFO_GetData( &rxFifo, &charInput );
			//TERM_ProcessChar( term, charInput );
		}
	} while (exit == false);
	*/

//	MEM_FREE( rxBuffer );
//	MEM_FREE( txBuffer );
//	LcdContrast();

//if 	(JOYSTICK_GetState() & (JOYSTICK_ENTER) == 0x00) {
//	LcdContrast();
//}

	while (1)
 	{
		if (urx_recv) 
		{	
			cli();
            urx_recv = 0;
            ch = urx;
            sei();
            /* build a command line and execute commands when complete */
            recv_input(ch);
		}
	}

	CAL_MAIN_LAST();
}
Beispiel #2
0
/*
 * Play on @sock.
 * The session must be in the playing phase.
 * Return 0 when the session ended, -1 on error.
 */
int
play(int sock)
{
    /*
     * Player input flows from INPUT_FD through recv_input() into ring
     * buffer INBUF, which drains into SOCK.  This must not block.
     * Server output flows from SOCK into recv_output().  Reading SOCK
     * must not block.
     */
    struct sigaction sa;
    struct ring inbuf;		/* input buffer, draining to SOCK */
    int eof_fd0;		/* read fd 0 hit EOF? */
    int partial_line_sent;	/* partial input line sent? */
    fd_set rdfd, wrfd;
    int n;

    sa.sa_flags = 0;
    sigemptyset(&sa.sa_mask);
    sa.sa_handler = intr;
    sigaction(SIGINT, &sa, NULL);
    sa.sa_handler = SIG_IGN;
    sigaction(SIGPIPE, &sa, NULL);

    ring_init(&inbuf);
    eof_fd0 = partial_line_sent = send_eof = send_intr = 0;
    input_fd = 0;
    sysdep_stdin_init();

    for (;;) {
	FD_ZERO(&rdfd);
	FD_ZERO(&wrfd);

	/*
	 * Want to read player input only when we don't need to send
	 * cookies, and INPUT_FD is still open, and INBUF can accept
	 * some.
	 */
	if (!send_intr && !send_eof && input_fd >= 0 && ring_space(&inbuf))
	    FD_SET(input_fd, &rdfd);
	/* Want to send player input only when we have something */
	if (send_intr || send_eof || ring_len(&inbuf))
	    FD_SET(sock, &wrfd);
	/* Always want to read server output */
	FD_SET(sock, &rdfd);

	n = select(MAX(input_fd, sock) + 1, &rdfd, &wrfd, NULL, NULL);
	if (n < 0) {
	    if (errno != EINTR) {
		perror("select");
		return -1;
	    }
	}

	if ((send_eof || send_intr) && partial_line_sent
	    && ring_putc(&inbuf, '\n') != EOF)
	    partial_line_sent = 0;
	if (send_eof && !partial_line_sent
	    && ring_putm(&inbuf, EOF_COOKIE, sizeof(EOF_COOKIE) - 1) >= 0)
	    send_eof--;
	if (send_intr && !partial_line_sent
	    && ring_putm(&inbuf, INTR_COOKIE, sizeof(INTR_COOKIE) - 1) >= 0) {
	    send_intr = 0;
	    if (input_fd) {
		/* execute aborted, switch back to fd 0 */
		close(input_fd);
		input_fd = eof_fd0 ? -1 : 0;
	    }
	}

	if (n < 0)
	    continue;

	/* read player input */
	if (input_fd >= 0 && FD_ISSET(input_fd, &rdfd)) {
	    n = recv_input(input_fd, &inbuf);
	    if (n < 0) {
		perror("read stdin"); /* FIXME stdin misleading, could be execing */
		n = 0;
	    }
	    if (n == 0) {
		/* EOF on input */
		send_eof++;
		if (input_fd) {
		    /* execute done, switch back to fd 0 */
		    close(input_fd);
		    input_fd = eof_fd0 ? -1 : 0;
		} else {
		    /* stop reading input, drain socket ring buffers */
		    eof_fd0 = 1;
		    input_fd = -1;
		    sa.sa_handler = SIG_DFL;
		    sigaction(SIGINT, &sa, NULL);
		}
	    } else
		partial_line_sent = ring_peek(&inbuf, -1) != '\n';
	}

	/* send it to the server */
	if (FD_ISSET(sock, &wrfd)) {
	    n = ring_to_file(&inbuf, sock);
	    if (n < 0) {
		perror("write socket");
		return -1;
	    }
	}

	/* read server output and print it */
	if (FD_ISSET(sock, &rdfd)) {
	    n = recv_output(sock);
	    if (n < 0) {
		perror("read socket");
		return -1;
	    }
	    if (n == 0)
		return 0;
	}
    }
}