Example #1
0
int connect()
{
    unsigned char answer = 0;

    SerialTransmit(HELLO_HOST);
    Sleep(50);
    answer = SerialReceive();

    if(answer == HELLO_CLIENT) {
        return 1;
    }

    return 0;
}
Example #2
0
void checkKeys()
{
    if(IsKeyDown(KEY_CTRL_EXIT) || IsKeyDown(KEY_CTRL_MENU)) {
        SerialTransmit(EXIT);
        Stop_SerialPort();
        Reset_Calc();
    }
    if((IsKeyDown(KEY_CTRL_UP) || IsKeyDown(KEY_CHAR_5) || IsKeyDown(KEY_CHAR_8)) && direction != 2) {
        direction = 0;
    }
    if((IsKeyDown(KEY_CTRL_RIGHT) || IsKeyDown(KEY_CHAR_3) || IsKeyDown(KEY_CHAR_6)) && direction != 3) {
        direction = 1;
    }
    if((IsKeyDown(KEY_CTRL_DOWN) || IsKeyDown(KEY_CHAR_2)) && direction != 0) {
        direction = 2;
    }
    if((IsKeyDown(KEY_CTRL_LEFT) || IsKeyDown(KEY_CHAR_1) || IsKeyDown(KEY_CHAR_4)) && direction != 1) {
        direction = 3;
    }
}
bool CommSendPacket(CommInterface * inf, CommPacket * pkt) {
	unsigned char length = pkt->length;
	
	if (SerialTransferInProgress(&inf->serData)) {
		return false;
	}
	
	if (length > SERIAL_TRANSMIT_BUFFER_SIZE) // TODO: Dynamically size SerialProtocol.transmit_data so we don't have this mysterious 20 byte max packet size
		length = SERIAL_TRANSMIT_BUFFER_SIZE;
	
	memcpy((void*)inf->serData.transmit_data, (void*)pkt->data, length);
	
	USART_TransmitMode(inf->port, true);
	if (SerialTransmit(&inf->serData, pkt->target, length)==0) {
		return true;
	}
	else {
		sei();
		return false;
	}
}
Example #4
0
int AddIn_main(int isAppli, unsigned short OptionNum)
{
    int collided = 0;
    unsigned char answer;

    Bdisp_AllClr_DDVRAM();

    Init_SerialPort();
    Start_SerialPort();

    if(connect() == 0) {
        isHost = 1;
    }

    while(1) {
        startScreen();

        memset(videoBuffer, 0, 1024);
        DrawBox(0, 0, 127, 63, videoBuffer, BLACK);
        DrawBox(48, 27, 79, 37, videoBuffer, BLACK);
        CoolText(51, 29, FONT_FIVE, "Ready?", videoBuffer);
        DrawAll(videoBuffer);

        Sleep(1000);

        SerialTransmit(READY);
        while(SerialReceive() != READY) {
            ;
        }

        reset();

        while(1) {
            answer = SerialReceive();

            if(answer == CRASH) {
                score++;
                wonLast = ME;
                break;
            }
            if(answer == EXIT) {
                Reset_Calc();
            }
            if(answer == COORDS) {
                receivedX = SerialReceive();
                receivedY = SerialReceive();

                setBit(videoBuffer, 128, receivedX, receivedY, 1);
            }

            checkKeys();
            move();

            collided = getBit(videoBuffer, 128, playerX, playerY);
            if(collided) {
                otherScore++;
                wonLast = OTHER;
                SerialTransmit(CRASH);
                break;
            }

            setBit(videoBuffer, 128, playerX, playerY, 1);

            SerialTransmit(COORDS);
            SerialTransmit(playerX);
            SerialTransmit(playerY);

            DrawAll(videoBuffer);

            Sleep(delays[speed]);
        }
    }

    return 1;
}
Example #5
0
void startScreen()
{
    unsigned char answer;
    char scoreText[4], otherScoreText[4], statusText[22], speedText[22], levelText[22];
    int isReady = 0, is1Pressed = 0, is2Pressed = 0, is3Pressed = 0, isLeftPressed = 0, isRightPressed = 0;

    while(!isReady) {
        if(isHost) {
            if(isConnected) {
                strcpy(statusText, "Hosting");
            } else {
                strcpy(statusText, "Waiting");
            }
        } else {
            strcpy(statusText, "Connected");
        }

        sprintf(scoreText, "%d", score);
        sprintf(otherScoreText, "%d", otherScore);
        sprintf(speedText, "SPEED:  %d", speed + 1);
        sprintf(levelText, "LEVEL:  %d", level + 1);

        memset(videoBuffer, 0, 1024);

        DrawBox(0, 0, 127, 63, videoBuffer, BLACK);
        DrawLine(0, 8, 127, 8, videoBuffer, BLACK);
        DrawLine(0, 16, 127, 16, videoBuffer, BLACK);
        DrawLine(0, 24, 127, 24, videoBuffer, BLACK);
        DrawLine(64, 16, 64, 24, videoBuffer, BLACK);
        DrawLine(0, 32, 127, 32, videoBuffer, BLACK);
        DrawLine(0, 40, 127, 40, videoBuffer, BLACK);

        CoolText(32, 1, FONT_FIVE, "MTRON by rojet601", videoBuffer);
        CoolText(48, 9, FONT_FIVE, statusText, videoBuffer);
        CoolText(27, 17, FONT_FIVE, scoreText, videoBuffer);
        CoolText(91, 17, FONT_FIVE, otherScoreText, videoBuffer);
        if(wonLast == ME) {
            CoolText(48, 25, FONT_FIVE, "You won", videoBuffer);
        }
        if(wonLast == OTHER) {
            CoolText(48, 25, FONT_FIVE, "You lost", videoBuffer);   
        }
        if(isHost) {
            CoolText(2, 33, FONT_FIVE, speedText, videoBuffer);
            CoolText(65, 33, FONT_FIVE, levelText, videoBuffer);
            CoolText(2, 52, FONT_FIVE, "[1]-[3] SPEED", videoBuffer);
            CoolText(65, 52, FONT_FIVE, "         LEVEL", videoBuffer);
            DrawLine(65, 55, 71, 55, videoBuffer, BLACK);
            DrawLine(66, 55, 68, 53, videoBuffer, BLACK);
            DrawLine(66, 55, 68, 57, videoBuffer, BLACK);

            DrawLine(73, 55, 79, 55, videoBuffer, BLACK);
            DrawLine(79, 55, 77, 53, videoBuffer, BLACK);
            DrawLine(79, 55, 77, 57, videoBuffer, BLACK);
            if(isConnected) {
                CoolText(2, 43, FONT_FIVE, "[SHIFT] START", videoBuffer);
            }
        } else {
            CoolText(2, 43, FONT_FIVE, "Only the host can change", videoBuffer);
            CoolText(2, 52, FONT_FIVE, "the settings", videoBuffer);
        }

        DrawAll(videoBuffer);

        if(IsKeyDown(KEY_CTRL_EXIT) || IsKeyDown(KEY_CTRL_MENU)) {
            SerialTransmit(EXIT);
            Stop_SerialPort();
            Reset_Calc();
        }

        answer = SerialReceive();

        if(answer == EXIT) {
            Stop_SerialPort();
            Reset_Calc();
        }
        if(answer == START) {
            Sleep(50);
            speed = SerialReceive();
            level = SerialReceive();
            isReady = 1;
        }

        if(isHost) {
            if(!isConnected) {
                if(answer == HELLO_HOST) {
                    SerialTransmit(HELLO_CLIENT);
                    isConnected = 1;
                }
            } else {
                if(IsKeyDown(KEY_CTRL_SHIFT)) {
                    SerialTransmit(START);
                    SerialTransmit(speed);
                    SerialTransmit(level);
                    Sleep(50);
                    isReady = 1;
                }
            }
            if(!IsKeyDown(KEY_CHAR_1)) {
                is1Pressed = 0;
            }
            if(IsKeyDown(KEY_CHAR_1) && !is1Pressed) {
                is1Pressed = 1;
                speed = 0;
            }
            if(!IsKeyDown(KEY_CHAR_2)) {
                is2Pressed = 0;
            }
            if(IsKeyDown(KEY_CHAR_2) && !is2Pressed) {
                is2Pressed = 1;
                speed = 1;
            }
            if(!IsKeyDown(KEY_CHAR_3)) {
                is3Pressed = 0;
            }
            if(IsKeyDown(KEY_CHAR_3) && !is3Pressed) {
                is3Pressed = 1;
                speed = 2;
            }
            if(!IsKeyDown(KEY_CTRL_LEFT)) {
                isLeftPressed = 0;
            }
            if(IsKeyDown(KEY_CTRL_LEFT) && !isLeftPressed) {
                isLeftPressed = 1;
                level--;
                if(level == 255) {
                    level = levels - 1;
                }
            }
            if(!IsKeyDown(KEY_CTRL_RIGHT)) {
                isRightPressed = 0;
            }
            if(IsKeyDown(KEY_CTRL_RIGHT) && !isRightPressed) {
                isRightPressed = 1;
                level++;
                if(level > levels - 1) {
                    level = 0;
                }
            }
        }
    }
}
Example #6
0
void sendAck(void)
{
    SerialTransmit("ACK\n");

}
Example #7
0
int main(void)
{

    ind = 0;
	// Setup functions for operation
	CP0Init();
	INTInitialize();
	ADCSetup();
	SetupButtons();
	SetupIO();
	SetupT1CountINT();
	SetupTimer2_3PWM();
        //SetupUART(115200);
        NU32_EnableUART2Interrupt();


	// Wait until LCD screen comes up
	wait(1000);	wait(1000);	wait(1000); wait(1000);
	LCDConfig();
	LCDSetup();


        ClearLCDScreen();

        char msg[32];
        sprintf(msg,"UART demo. Press A to start");
	WriteLCD(1,msg);

        int button = 1;
        while(button == 1) {
            button = BUTTON_A;
        }
        wait(1000);
        while(button == 0)
        {
            button = BUTTON_A;
        }
        wait(1000);
        
        // send dollar signs to enter command mode
        char send1[] = "$$$";
        SerialTransmit(send1);

        wait(1000);wait(1000);wait(1000);
        // print message and wait for press
/*        ClearLCDScreen();
        wait(1000);wait(1000);wait(1000);
        sprintf(msg,"command mode. press A");
	WriteLCD(1,msg);
        wait(1000);wait(1000);wait(1000);
        button = 1;
        while(button == 1) {
            button = BUTTON_A;
        }
        wait(1000);
        while(button == 0)
        {
            button = BUTTON_A;
        }
        wait(1000);
        wait(1000);
*/
        while(next == 0);
        next = 0;

        char send2[] = "GB\n";
        SerialTransmit(send2);

        while(1)
        {
            ClearLCDScreen();
            WriteLCD(0,string);
            wait(10000);
            wait(10000);
            wait(10000);
        }

        // receive response from chip
        char temp[4];
        SerialReceive(temp, 4, CHAR_MODE);
        
//        // print response to lcd
//        ClearLCDScreen();
//        WriteLCD(1,temp);
        

        
        char temp2[32];
        SerialReceive(temp2, 32, HEX_MODE);

        ClearLCDScreen();
        WriteLCD(1,temp2);


 /*
  * Software State machine
  *     POR:
  *         Try to connect
  *
  *
  */

        return 0;
}
Example #8
0
int main(void)
{
	InitClock();
	SerialInit();
	
	RingBufferInit(&location_buffer, 4, locations);
	
	PORTC.DIRSET = 0xF3;
	PORTE.DIRSET = 0xFF;
	PORTD.DIRSET = 0x33;
	PORTB.DIRCLR = 0xFF; /* PortB all inputs */
	
	/* Timers */
	//x_axis.step_timer = &TCC1;
	x_axis.pwm_timer = &TCC0;
	
	/* Limit Switches */
	x_axis.limit_switch_port = &PORTB;
	x_axis.limit_switch_mask = (1<<0); /* PORTB.0 */
	
	/* DIR ports (for inverting the stepper motor driver polarity) */
	x_axis.sign_select_port = &PORTC;
	x_axis.sign_switch_mask1 = 0x20; /* PORTC.5 */
	x_axis.sign_switch_mask2 = 0x10; /* PORTC.4 */
	
	/* PWM outputs: outputs will be 90 degrees out of phase */
	x_axis.phase_pwm_cmp1 = &(TCC0.CCB);
	x_axis.phase_pwm_cmp2 = &(TCC0.CCA);
	x_axis.compare_mask = TC0_CCBEN_bm | TC0_CCAEN_bm;
	
	/* Power Controls change the period: a longer period means longer off time and lower duty cycle */
	//x_axis.axis_idle_power = 60;
	x_axis.axis_run_power = 31;
	
	/* The minimum period of the PWM update timer/counter */
	/* Stepper motor tick period = 32 * min_period / 16000000 */
	//x_axis.min_period = 15;
	AxisInit (&x_axis);
	
	//y_axis.step_timer = &TCD1;
	y_axis.pwm_timer = &TCD0;
	y_axis.limit_switch_port = &PORTB;
	y_axis.limit_switch_mask  = (1<<1); /* PORTB.1 */
	y_axis.sign_select_port = &PORTD;
	y_axis.sign_switch_mask1 = 0x20;
	y_axis.sign_switch_mask2 = 0x10;
	y_axis.phase_pwm_cmp1 = &(TCD0.CCB);
	y_axis.phase_pwm_cmp2 = &(TCD0.CCA);
	y_axis.compare_mask = TC0_CCBEN_bm | TC0_CCAEN_bm;
	//y_axis.axis_idle_power = 60;
	y_axis.axis_run_power = 31;
	//y_axis.min_period = 15;
	AxisInit (&y_axis);
	
	//z_axis.step_timer = &TCE1;
	z_axis.pwm_timer = &TCE0;
	z_axis.limit_switch_port = &PORTB;
	z_axis.limit_switch_mask = (1<<2); /* PORTB.2 */
	z_axis.sign_select_port = &PORTE;
	z_axis.sign_switch_mask1 = 0x20; /* PORTE.5 */
	z_axis.sign_switch_mask2 = 0x10; /* PORTE.4 */
	z_axis.phase_pwm_cmp1 = &(TCE0.CCD);
	z_axis.phase_pwm_cmp2 = &(TCE0.CCC);
	z_axis.compare_mask = TC0_CCDEN_bm | TC0_CCCEN_bm;
	//z_axis.axis_idle_power = 60;
	z_axis.axis_run_power = 31; /* 33 unique waveform values: 0 (ground) to 33 (3.3v) */
	//z_axis.min_period = 15;
	AxisInit (&z_axis);

	PMIC.CTRL |= PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
	
	// Fire up the timer for incrementing/decrementing steps
	TC0_t * step_timer = &TCC1;
	
	step_timer->CTRLB = TC_WGMODE_SS_gc;
	
	/* Overflow every 1 ms: 16Mhz / (64 * 250) = 1ms */						
	step_timer->PER = 250;
	step_timer->CTRLA = TC_CLKSEL_DIV64_gc;
	step_timer->CTRLFSET = TC_CMD_RESTART_gc;
	
	/* Enable the step overflow interrupt */
	step_timer->INTCTRLA |= TC_OVFINTLVL_LO_gc;
	// To Disable: axis->step_timer->INTCTRLA &= ~TC1_OVFINTLVL_gm;	
	
	
	char task = 0;
	sei();
	while(1)
    {
		//PORTE.OUTTGL = 0x02;
		
		//_delay_ms (10);
		
		//SerialData * s = SerialDataTransmitStruct();
		//if (s != 0)
		//{
		//	s->transmit_data[0] = 'a';
		//	s->transmit_data[1] = 'b';
		//	s->transmit_data[2] = 'c';
		//	SerialTransmit(s, 0x00, 3);
		//}
		
		SerialData * s = SerialDataAvailable();
		if (s != 0)
		{
			switch (s->receive_data[0])
			{
				case 0x88: // Reset everything (all axis back to limit switches)
				{
					task = TASK_LIMIT_SWITCH_Y;
					x_axis.state = 1;
					y_axis.state = 1;
					z_axis.state = 1;
					s->transmit_data[0] = 0x88;
					SerialTransmit (s, 0x00, 1); // Transmit to master device
					break;
				}
				case 0x77: // Ping (and location/status information)
				{
					s->transmit_data[0] = 0x77;
					
					// TODO: find a better way to do this
					step_timer->INTCTRLA &= ~TC1_OVFINTLVL_gm; // Disable the step timer
					int32_t x = AxisGetCurrentPosition(&x_axis);
					int32_t y = AxisGetCurrentPosition(&y_axis);
					int32_t z = AxisGetCurrentPosition(&z_axis);
					step_timer->INTCTRLA |= TC_OVFINTLVL_LO_gc; // Enable the step timer
					
					s->transmit_data[1] = x & 0xFF; x = x >> 8;
					s->transmit_data[2] = x & 0xFF; x = x >> 8;
					s->transmit_data[3] = x & 0xFF; x = x >> 8;
					s->transmit_data[4] = x & 0xFF;
					
					s->transmit_data[5] = y & 0xFF; y = y >> 8;
					s->transmit_data[6] = y & 0xFF; y = y >> 8;
					s->transmit_data[7] = y & 0xFF; y = y >> 8;
					s->transmit_data[8] = y & 0xFF;
					
					s->transmit_data[9] = z & 0xFF; z = z >> 8;
					s->transmit_data[10] = z & 0xFF; z = z >> 8;
					s->transmit_data[11] = z & 0xFF; z = z >> 8;
					s->transmit_data[12] = z & 0xFF;
					
					uint8_t status_bits = 0;
					if (IsMoving (&x_axis))
					{
						status_bits |= 0x01;
					}
					if (IsMoving (&y_axis))
					{
						status_bits |= 0x02;
					}
					if (IsMoving (&z_axis))
					{
						status_bits |= 0x04;
					}
					s->transmit_data[13] = status_bits;
					
					//step_timer->INTCTRLA &= ~TC1_OVFINTLVL_gm; // Disable the step timer
					buffer_lock = 1;
					s->transmit_data[14] = RingBufferCount(&location_buffer);
					buffer_lock = 0;
					//step_timer->INTCTRLA |= TC_OVFINTLVL_LO_gc; // Enable the step timer
					
					//s->transmit_data[14] = location_buffer_size;
					
					SerialTransmit (s, 0x00, 15); // Transmit to master device
					break;
				}
				case 0x32: // Set Position + Speed
				{
					Location_t l;
					
					/* Bytes 1:2 are time in milliseconds */
					l.time = decode_uint16_t(&(s->receive_data[1]));
					
					/* Bytes 3:14 are position */
					l.x = decode_uint32_t(&(s->receive_data[3]));
					l.y = decode_uint32_t(&(s->receive_data[7]));
					l.z = decode_uint32_t(&(s->receive_data[11]));
					
					// Add the new location to the buffer
					//step_timer->INTCTRLA &= ~TC1_OVFINTLVL_gm; // Disable the step timer
					
					buffer_lock = 1;
					if (!RingBufferIsFull(&location_buffer))
					{
						RingBufferAdd(&location_buffer, l);
					}
					//if (location_buffer_size < 1)
					//{
					//	nextLocation = l;
					//	location_buffer_size++;
					//}
					s->transmit_data[1] = RingBufferCount(&location_buffer);
					buffer_lock = 0;
					//step_timer->INTCTRLA |= TC_OVFINTLVL_LO_gc; // Enable the step timer
					
					s->transmit_data[0] = 0x32;
					//s->transmit_data[1] = location_buffer_size;
					SerialTransmit(s, 0x00, 2);
					break;
				}
			}
		}
		else if (task == TASK_LIMIT_SWITCH_Y)
		{
			uint8_t v = LimitSwitchHelper(&x_axis);
			v &= LimitSwitchHelper(&y_axis);
			v &= LimitSwitchHelper(&z_axis);
			if (v)
			{
				task = 0;
			}
		}
	}