Esempio n. 1
0
int main(void)
{
	CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_1_1_gc );


	// CLKOUT on PC7
	PORTC.DIRSET = (1<<7);
	PORTCFG.CLKEVOUT = PORTCFG_CLKOUT_PC7_gc;


	tbar_init();

	xb_init();

	PORTB.DIRSET = 1; // set PB0 as output

	DEBUG_PORT.DIRSET = DEBUG_RED + DEBUG_GREEN;


	while(1)
	{
		//uint8_t i;
		// wait for incoming transmission from Zigbee
		// TODO : make this into interrupt-driven stuffs

		_delay_ms(600);

		// signal that we're ready
		debug_blink(DEBUG_GREEN);

		while(1)
		{
			//debug_blink(DEBUG_GREEN);
			char byte = 0;
			XB_GetChar(byte);
			if(byte == 0x7e)
			{
				uint8_t length = xb_decode_packet(packet);

				if(length>0)
				{
					uint8_t i;
					for(i=0;i<length; i++)
					{
						TB_PutChar(packet[i]);
					}
					debug_blink(DEBUG_GREEN);
				}
				else
					debug_blink(DEBUG_RED);
			}
		}

		// signal that we've started receiving
		debug_blink(DEBUG_RED);

		


		// decode packet

		// send data out T port
	/*
		for (i=0; i<sizeof(Coord.name)/sizeof(char); i++)
		{
			while( (USARTT.STATUS & USART_DREIF_bm) == 0);
			USARTT.DATA = Coord.name[i];
			
		}
		*/

	/*	xb_send_array_new(blah, sizeof(blah)/sizeof(char), &Coord);
		xb_send_array_new(blah, sizeof(blah)/sizeof(char), &Gizmo1);*/
		// flash green led
		
		//USARTT_PORT.OUTTGL = (1<<USARTT_TX);
	}
}
Esempio n. 2
0
// Take input from a computer and load it onto the cube buffer
void rs232()
{
	char tempval;
	int x = 0;
	int y = 0;
    int escape = 0;
	
	while (current_mode == MODE_BINARY)
	{
		// Switch state on red LED for debugging
		// Should switch state every time the code
		// is waiting for a byte to be received.
		debug_blink(LED_RED);

		// Wait until a byte has been received
		while(!HasChars());
        
		// Load the received byte from rs232 into a buffer.
		receive_char(&tempval);

		// Uncommet this to echo data back to the computer
		// for debugging purposes.
		//SendChar(tempval);

		// Every time the cube receives a 0xff byte,
		// it goes into sync escape mode.
		// if a 0x00 byte is then received, the x and y counters
		// are reset to 0. This way the x and y counters are
		// always the same on the computer and in the cube.
		// To send an 0xff byte, you have to send it twice!

		// Go into sync escape mode
		if (tempval == 0xff)
		{
            // Wait for the next byte
            while(!HasChars());
            // Get the next byte
            receive_char(&tempval);

            // Sync signal is received.
            // Reset x and y counters to 0.
            if (tempval == 0x00)
            {
                x = 0;
				y = 0;
                escape = 1;
            }
            // if no 0x00 byte is received, proceed with
            // the byte we just received.
		}

        if (escape == 0)
        {
			// Load data into the current position in the buffer
			fb[x][y] = tempval;

    		// Check if we have reached the limits of the buffer array.
    		if (y == 7)
    		{
    			if (x == 7)
    			{
    				// All data is loaded. Reset both counters
    				y = 0;
    				x = 0;
                    // Copy the data onto the cube.
    				tmp2cube();
    			} 
				else
    			{
    				// A layer is loaded, reset y and increment x.
    				x++;
    				y = 0;
    			}
    		} 
			else
    		{
    			// We are in the middle of loading a layer. increment y.
    			y++;
    		}
	
	    } 
		else
        {
            escape = 0;
        }
    }
}