示例#1
0
void CryptState_init(cryptState_t *cs)
{
	memset(cs->decrypt_history, 0, 0xff);
	memset(cs->raw_key, 0, AES_BLOCK_SIZE);
	memset(cs->encrypt_iv, 0, AES_BLOCK_SIZE);
	memset(cs->decrypt_iv, 0, AES_BLOCK_SIZE);
	cs->bInit = false;
	cs->uiGood = cs->uiLate = cs->uiLost = cs->uiResync = 0;
	cs->uiRemoteGood = cs->uiRemoteLate = cs->uiRemoteLost = cs->uiRemoteResync = 0;
	Timer_init(&cs->tLastGood);
	Timer_init(&cs->tLastRequest);
}
示例#2
0
文件: Xbee.c 项目: SamChenzx/sdp
int main(){
    Board_init();
    Serial_init();
    Timer_init();
    Xbee_init(XBEE_UART_ID);
    printf("Xbee Test 2\n");
    while(1){
        Xbee_runSM();
        //if(!UART_isTransmitEmpty(UART1_ID));
        //printf("%d\t",Mavlink_returnACKStatus(messageName_start_rescue));
        if(!UART_isReceiveEmpty(UART1_ID)){
            Serial_getChar();
            Mavlink_send_start_rescue(UART2_ID, TRUE, 0xFF, 0x34FD, 0xAB54);
            Timer_new(TIMER_TIMEOUT, DELAY_TIMEOUT);
            printf("\nSENT\n");
        }
        else if(Timer_isActive(TIMER_TIMEOUT) != TRUE && Mavlink_returnACKStatus(messageName_start_rescue) == ACK_STATUS_WAIT){
        //else if(Timer_isActive(TIMER_TIMEOUT) != TRUE){
            Mavlink_editACKStatus(messageName_start_rescue, ACK_STATUS_DEAD);
            printf("ACK DEAD\n");
        }
        else if(Mavlink_returnACKStatus(messageName_start_rescue) == ACK_STATUS_DEAD){
            Mavlink_send_start_rescue(UART2_ID, TRUE, 0xFF, 0x34FD, 0xAB54);
            Timer_new(TIMER_TIMEOUT, DELAY_TIMEOUT);
        }
        else if(Mavlink_returnACKStatus(messageName_start_rescue) == ACK_STATUS_RECIEVED){
            Mavlink_editACKStatus(messageName_start_rescue, ACK_STATUS_NO_ACK);
            printf("GPS SENT AND ACKOWLEGED\n");
        } 
    }
}
示例#3
0
int main(void)
{
  Timer_init();

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  GPIO_InitTypeDef  GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 |GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

  static uint32_t timerValue;
  uint32_t lastTime = TIM2->CNT;
  while (1)
  {
    timerValue = TIM2->CNT;

    if( timerValue - lastTime > 1000000 )
    {
      GPIO_ToggleBits(GPIOD, GPIO_Pin_12 |GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
      lastTime = timerValue;
    }

  }
}
示例#4
0
int Engine_init()
{
	if (Graphics_init() != 0)
		BREAK_ERROR;

	if (Timer_init() != 0)
		BREAK_ERROR;

	if (Logic_init() != 0)
		BREAK_ERROR;
		
	if (Physics_init() != 0)
		BREAK_ERROR;
		
	if (Input_init() != 0)
		BREAK_ERROR;
		
	theworld = WorldObject_new();
	World_setWorldObject(theworld);	
		
	done = 0;

	initialize = 1;
	return 0;
}
示例#5
0
文件: Thermal.c 项目: sdajani/sdp
int main(void){
// Initialize the UART,Timers, and I2C1
    int count = 0;
    Board_init();
    Timer_init();
    Serial_init();
    Thermal_init();
    while(1){
        if(count == 0){
            readChipTemp();
            calculateChipTemp();
        }
        count++;
        if(count >= 16){
            count = 0;
        }
        readPixelValue();
        readCPixelValue();
        calculateIRTemp();
        int i;
        for(i = 0; i <= 63; ++i){
        while(!Serial_isTransmitEmpty());
            printf("V_ir[%d]: %.2f\n",i,finalPixelTempF[i]);
        }
    }
}
示例#6
0
void Sharedmemory_update(void) 
{

  uint64_t now;
  unsigned int cc = 0;
  client_t *client_itr = NULL;

    memset( &shmptr->client[0], 0, shmptr->shmclient_size );
    shmptr->clientcount = Client_count();
    
      if( shmptr->clientcount )
      {
        Timer_init( &now );
          while( Client_iterate(&client_itr) != NULL )
          {                                                                                   
            if( client_itr->authenticated )
            {        
              channel_t *channel = client_itr->channel;
        
                strncpy( shmptr->client[cc].username, client_itr->username, 120 );
                strncpy( shmptr->client[cc].ipaddress, Util_clientAddressToString( client_itr ), INET6_ADDRSTRLEN - 1 );
                strncpy( shmptr->client[cc].channel, channel->name, 120 );
                
                strncpy( shmptr->client[cc].os, client_itr->os, 120 );
                strncpy( shmptr->client[cc].release, client_itr->release, 120 );
                strncpy( shmptr->client[cc].os_version, client_itr->os_version, 120 );
                
                shmptr->client[cc].tcp_port = Util_clientAddressToPortTCP( client_itr );
                shmptr->client[cc].udp_port = Util_clientAddressToPortUDP( client_itr );                
        
                shmptr->client[cc].online_secs = ( now - client_itr->connectTime ) / 1000000LL;
                shmptr->client[cc].idle_secs = ( now - client_itr->idleTime ) / 1000000LL;
                        
                shmptr->client[cc].bUDP  = client_itr->bUDP;
                shmptr->client[cc].deaf  = client_itr->deaf;
                shmptr->client[cc].mute  = client_itr->mute;
                shmptr->client[cc].bOpus  = client_itr->bOpus;
                shmptr->client[cc].self_deaf  = client_itr->self_deaf;
                shmptr->client[cc].self_mute  = client_itr->self_mute;
                shmptr->client[cc].recording  = client_itr->recording;
                shmptr->client[cc].authenticated  = client_itr->authenticated;
                
                shmptr->client[cc].availableBandwidth  = client_itr->availableBandwidth;
                
                shmptr->client[cc].UDPPingAvg = client_itr->UDPPingAvg;
                shmptr->client[cc].UDPPingVar = client_itr->UDPPingVar;
                shmptr->client[cc].TCPPingAvg = client_itr->TCPPingAvg;
                shmptr->client[cc].TCPPingVar = client_itr->TCPPingVar;
                
                shmptr->client[cc].isAdmin = client_itr->isAdmin;
                shmptr->client[cc].isSuppressed = client_itr->isSuppressed;
                
                shmptr->client[cc].UDPPackets = client_itr->UDPPackets;
                shmptr->client[cc].TCPPackets = client_itr->TCPPackets;
                
            }  
          cc++;     
        }
      } 
}
示例#7
0
文件: Lcd.c 项目: SamChenzx/sdp
int main(void)
{
    Board_init();
    Serial_init();
    LCD_init();
    Timer_init();
    printf("Testing the LCD display.\nYou should see: Hello World!\n");

    LCD_writeString("Hello World!\nLine2\nLine3\nLine4\n");
    Timer_new(TIMER_TEST,WRITE_DELAY);
    while (!Timer_isExpired(TIMER_TEST)) {
        asm("nop");
    }
    printf("Overwriting line 1\n");
    LCD_writeString("Testing overtop line1.\n");

    Timer_new(TIMER_TEST,WRITE_DELAY);
    while (!Timer_isExpired(TIMER_TEST)) {
        asm("nop");
    }
    printf("Clearing Display\n");
    LCD_clearDisplay();

    Timer_new(TIMER_TEST,WRITE_DELAY);
    while (!Timer_isExpired(TIMER_TEST)) {
        asm("nop");
    }
    LCD_writeString("Here.we.go\n");
    LCD_writeString("CLEARED!\n");
    LCD_writeString("CLEARED2!\n");
    LCD_writeString("CLEARED3!\n");

    Timer_new(TIMER_TEST,WRITE_DELAY);
    while (!Timer_isExpired(TIMER_TEST)) {
        asm("nop");
    }

    LCD_clearDisplay();
    
    LCD_writeString("N:        E:        \n");
    char tmp[10];
    LCD_setPosition(0,3);
    sprintf(tmp,"%.2f",12.5f);
    LCD_writeString(tmp);
    
    LCD_setPosition(0,13);
    sprintf(tmp,"%.2f",-0.32f);
    LCD_writeString(tmp);

    return SUCCESS;
}
示例#8
0
文件: Drive.c 项目: SamChenzx/sdp
int main(){
    //Initializations
    Board_init();
    Serial_init();
    Timer_init();
    Drive_init();
    ENABLE_OUT_TRIS = OUTPUT;  
    ENABLE_OUT_LAT = MICRO;

#ifdef RECIEVE_CONTROL
    ENABLE_OUT_LAT = RECIEVER;
    while(1){
        asm("nop");
    }
#endif

    printf("State machine test harness.\n\n");

    // Test forward state
    printf("Forward state. Driving at 5, 10, 20, 40, 60, 80, 100, 125 \%.\n");
    uint8_t speed[] = {5, 10, 20, 40, 60, 80, 100, 125};
    int i, len = sizeof(speed);
    delayMillisecond(STATE_DELAY);
    for (i = 0; i < len; i++) {
        Drive_forward(speed[i]);
        delayMillisecond(STATE_DELAY);
    }
    Drive_stop();
    delayMillisecond(STATE_DELAY);

    setRudder(STOP_PULSE);
    delayMillisecond(STATE_DELAY);


    // Forward with heading
    printf("Track state. Driving at 5, 10, 25, 50, 100 \% at 0 deg north.\n");
    uint8_t speed2[] = {5, 10, 25, 50, 100};
    len = sizeof(speed2);
    delayMillisecond(STATE_DELAY);
    for (i = 0; i < len; i++) {
        Drive_forwardHeading(speed2[i], 0);
        delayMillisecond(STATE_DELAY);
    }
    Drive_stop();

    printf("Finished state machine test.\n\n");


    return SUCCESS;
}
示例#9
0
int main(void)
{
	GPIO_init();
	Timer_init();
	PWM_init();
	ADC_init();
	NVIC_init();

	// infinite loop
	while (1)
	{
		
	}
}
示例#10
0
文件: Drive.c 项目: SamChenzx/sdp
int main(){
    //Initializations
    Board_init();
    Serial_init();
    Timer_init();
    if (Drive_init() != SUCCESS) {
        printf("Failed to initialize drive module.\n");
    }
    I2C_init(TILT_COMPASS_I2C_ID, I2C_CLOCK_FREQ);
    if (TiltCompass_init() != SUCCESS) {
        printf("Failed to initialize tilt compass module.\n");
    }
    ENABLE_OUT_TRIS = OUTPUT;

    while (1) {
        printf("Driving north at full speed.\n");
        Timer_new(TIMER_TEST, COMMAND_DELAY);
        Drive_forwardHeading(100, 0);

        while(!Timer_isExpired(TIMER_TEST)) {
            //wait for finish
            DELAY(1);
            Drive_runSM();
            TiltCompass_runSM();
        }
        Drive_stop();
        Drive_runSM();
        TiltCompass_runSM();

        printf("Driving south at full speed.\n");
        Timer_new(TIMER_TEST, COMMAND_DELAY);
        Drive_forwardHeading(100, 180);

        while(!Timer_isExpired(TIMER_TEST)) {
            //wait for finish
            DELAY(1);
            Drive_runSM();
            TiltCompass_runSM();
        }
        
        Drive_stop();
        Drive_runSM();
        TiltCompass_runSM();
        delayMillisecond(COMMAND_DELAY);
        printf("Waiting for retry...\n");

    }

    return SUCCESS;
}
示例#11
0
文件: Board.c 项目: SamChenzx/sdp
void Board_configure(uint8_t opts) {
    DELAY(STARTUP_DELAY );
    if (opts & USE_TIMER) {
        Timer_init();
    }
    if (opts & USE_SERIAL) {
        option.useSerial = TRUE;
        Serial_init();
    }
    if (opts & USE_LCD) {
        option.useLCD = TRUE;
        //LCD_init();
    }
    DELAY(STARTUP_DELAY );
}
示例#12
0
文件: Drive.c 项目: SamChenzx/sdp
int main(){
    //Initializations
    Board_init();
    Serial_init();
    Timer_init();
    Drive_init();
    ENABLE_OUT_TRIS = OUTPUT;
    ENABLE_OUT_LAT = MICRO;
setLeftMotor(100);
    setRightMotor(100);
    while (1)
        asm("nop");
    
    return SUCCESS;
}
示例#13
0
//=============================================================================================
int main(void)
{
	timer_flg=0;
	GPIO_port_init(PORT_0);
	GPIO_port_init(PORT_1);
	Timer1_port_init();
	Timer_init();	
	NVIC->ISER[0] =NVIC_INTR_EN_TIMER1_IRQ;   
  while(1)
	{	  
	  delay(600000);
	  delay(600000);
    led_add(PORT_0);
	}

}
示例#14
0
文件: main.c 项目: zhiyb/Il-Matto
void init(void)
{
	DDRB &= 0xF3;			// Disable USB Port
	PORTB &= 0xF3;
	DDRB |= 0x80;			// LED
	PORTB |= 0x80;
	Sound_init();
	ROE_init();
	TFT_init();
	Timer_init();
	Tetris_mem_init();
	eeprom_counter_increment();
	srand(eeprom_counter());
	eeprom_done();
	Print_frame();
	Tetris_init();
}
示例#15
0
文件: Drive.c 项目: SamChenzx/sdp
int main() {
    ENABLE_OUT_TRIS = OUTPUT;            //Set Enable output pin to be an output, fed to the AND gates
    ENABLE_OUT_LAT = MICRO;             //Initialize control to that of Microcontroller
    Board_init();
    Serial_init();
    Timer_init();
    Drive_init();
    I2C_init(I2C_BUS_ID, I2C_CLOCK_FREQ);
    TiltCompass_init();
    printf("Boat initialized.\n");

    Drive_stop();
    DELAY(5);
#ifdef MOTOR_TEST
    Drive_pivot(0);
#endif

#ifdef RUDDER_TEST
   Drive_forwardHeading(0.0, desiredHeading);
#endif
   int i = 0;
   int velocity[] = {1600, 1600, 1600, 1600, 1600};
   velocityPulse = velocity[i];
    Timer_new(TIMER_TEST,FINISH_DELAY);
    while (1) {
        if (Timer_isExpired(TIMER_TEST))
        {
            i++;
            if (i == 5){
                i = 0;
            }
            velocityPulse = velocity[i];
           printf("CURRENT VELOCITY: %d\n\n",velocityPulse);
            Timer_new(TIMER_TEST,FINISH_DELAY);

        }
         
        Drive_runSM();
        TiltCompass_runSM();
    }

    Drive_stop();

    return SUCCESS;
}
示例#16
0
int main(void) {
	
	DDRC = 0x0F; // PC0=LED0, PC1=LED1, PC2=DIR_right, PC3=DIR_left
	DDRD = 0x00; // PD0=INT0
	
	PORTC = 0x00;
	PORTD = 0xFF;

	Timer_init();

	while(1) {

		

	}

	return 0;
}
示例#17
0
文件: Compas.c 项目: sdajani/sdp
/**
 * Function: initMasterSM
 * @return None.
 * @remark Initializes the master state machine for the command canter.
 * @author David Goodman
 * @date 2013.03.10  */
void initMasterSM() {
    Board_init();
    Serial_init();
    Timer_init();

    // CC buttons
    LOCK_BUTTON_TRIS = 1;
    ZERO_BUTTON_TRIS = 1;

    Encoder_init();

    #ifdef USE_XBEE
    Xbee_init();
    #endif

    #ifdef USE_NAVIGATION
    Navigation_init();
    #endif

    #ifdef USE_ENCODERS
    I2C_init(I2C_BUS_ID, I2C_CLOCK_FREQ);
    #endif


    #ifdef USE_ACCELEROMETER
    //printf("Initializing accelerometer...\n");
    if (Accelerometer_init() != SUCCESS) {
        printf("Failed to initialize the accelerometer.\n");
        //return;
    }
    //printf("Initialized the accelerometer.\n");

    // Configure ports as outputs
    LED_N_TRIS = OUTPUT;
    LED_S_TRIS = OUTPUT;
    LED_E_TRIS = OUTPUT;
    LED_W_TRIS = OUTPUT;


    Timer_new(TIMER_ACCELEROMETER, LED_DELAY );
    #endif
}
示例#18
0
文件: Interface.c 项目: SamChenzx/sdp
int main(void) {
    //initializations
    Board_init();
    Serial_init();
    Timer_init();
    LCD_init();
    Interface_init();
    
    printf("INITIALIZATIONS COMPLETE\n");
    LCD_writeString("Interface online.\n");

    Timer_new(TIMER_TEST, 100);
    while (1) {
        if (Timer_isExpired(TIMER_TEST)) {
            printf("%X -- C:%X F:%X\n",Interface_isOkPressed(), buttonCount.okay, buttonPressed.flag.okay);
            Timer_new(TIMER_TEST, 100);
        }
        Interface_runSM();
    }
}
示例#19
0
文件: Atlas.c 项目: ddeo/sdp
/**********************************************************************
 * Function: initializeAtlas
 * @return None.
 * @remark Initializes the boat's master state machine and all modules.
 * @author David Goodman
 * @date 2013.04.24  
 **********************************************************************/
static void initializeAtlas() {
    Board_init();
    Serial_init();
    Timer_init();

    #ifdef USE_DRIVE
    Drive_init();
    #endif

    // I2C bus
    I2C_init(I2C_BUS_ID, I2C_CLOCK_FREQ);

    #ifdef USE_TILTCOMPASS
    TiltCompass_init();
    #endif

    #ifdef USE_GPS
    GPS_init();
    #endif

    #ifdef USE_NAVIGATION
    Navigation_init();

    #endif

    #ifdef USE_OVERRIDE
    Override_init();
    #endif

    #ifdef USE_BAROMETER
    Barometer_init();
    Timer_new(TIMER_BAROMETER_SEND, BAROMETER_SEND_DELAY);
    #endif
        
    // Start calibrating before use
    startSetOriginSM();

    Timer_new(TIMER_HEARTBEAT, HEARTBEAT_SEND_DELAY);

    Mavlink_sendStatus(MAVLINK_STATUS_ONLINE);
}
示例#20
0
文件: Xbee.c 项目: SamChenzx/sdp
int main(){
    Board_init();
    Serial_init();
    printf("Welcome to Xbee Test1\n");
    printf("UART INIT\n");
    Timer_init();

    printf("Timers Init\n");
    if(Xbee_init(XBEE_UART_ID) == FAILURE){
        printf("Xbee Failed to initilize\n");
        return FAILURE;
    }
    printf("XBEE Initialized\n");

// Master sends packets and listens for responses
    #ifdef XBEE_1
    Mavlink_send_Test_data(XBEE_UART_ID, 1);
    Timer_new(TIMER_TIMEOUT, DELAY_TIMEOUT);
    Timer_new(TIMER_STATUS, DELAY_STATUS);
    while(1){
        Xbee_runSM();
        //lost a packet, report it, and restart
        if(Timer_isActive(TIMER_TIMEOUT) != TRUE){
            Mavlink_send_Test_data(XBEE_UART_ID, 1);
            count_lost++;
            Timer_new(TIMER_TIMEOUT, DELAY_TIMEOUT);
            printf("lost_packet: %d\n", get_time());
        }
        //Printout the status
        if(Timer_isActive(TIMER_STATUS) != TRUE){
            Timer_new(TIMER_STATUS, DELAY_STATUS);
            printf("Status: %d,%d [Recieved,Lost] TIME: %d\n", count_recieved, count_lost, get_time());
        }

    }
    #else
    while(1){
        Xbee_runSM();
    }
    #endif
}
示例#21
0
文件: Override.c 项目: BananaSlug/sdp
int  main(void) {
    // Initialize the UART, Timers, and I2C
    Board_init();
    Serial_init();
    Timer_init();
    Override_init();
    printf("Override board initialized.\n");

    while(1){
        if (inOverride && !Override_isTriggered()) {
            inOverride = FALSE;
            printf("Override disabled.\n");
        }
        else if (!inOverride && Override_isTriggered()) {
            inOverride = TRUE;
            printf("Override enabled.\n");
        }
    }

    return (SUCCESS);
}
示例#22
0
文件: Thermal.c 项目: sdajani/sdp
int main(void) {
// Initialize the UART,Timers, and I2C1
    //int row = 0;
    Board_init();
    Timer_init();
    Thermal_init();
    Serial_init();

    /*Timer_new(TIMER_TEST,100);
    while(!Timer_isExpired(TIMER_TEST);*/
#ifdef DEBUG_TEST2
    printf("Initialized Thermal module.\n");
#endif

    uint8_t row = 0, col = 0;
    Timer_new(TIMER_TEST,PRINT_DELAY);

    while(1) {
        if (Timer_isExpired(TIMER_TEST)) {
            #ifdef DEBUG_TEST2
                printf("Sending thermal data...\n");
            #endif
            sendSerial32(0xFFFF1234);

            for(col = 0; col < TOTAL_PIXEL_COLS; col++) {
                for(row = 0; row < TOTAL_PIXEL_ROWS; row++){
                    uint8_t pixel = row + (col * TOTAL_PIXEL_ROWS);
                    sendSerialFloat(finalPixelTempF[pixel]);
                }
                sendSerial32(END_COL_SEQUENCE);
            }

            #ifdef DEBUG_TEST2
                printf("\nFinished sending thermal data.\n");
            #endif
            Timer_new(TIMER_TEST,PRINT_DELAY);
        } // Timer_isExpired
        Thermal_runSM();
    } // while
}
示例#23
0
文件: main.c 项目: sky1993/git_1
int main( void)
{
	unsigned int Count = 0;
	
	P0 = 0xff;
	P1 = 0xff;
	P2 = 0xff;
	P3 = 0xff;
	LCD_Initial();
	GotoXY(0,0);
	Print("    Freq: 10  Hz");
	GotoXY(0,1);
	Print("squa   Vpp : 5 V");
	Timer_init();

	while(1)
	{
		if (P3 != 0xff)
		{
			Deal_key();
		}

		if (mode == 0)
		{
			DA_out(sin[Number_Frequency]);
		}
		else if (mode == 1)
		{
			DA_out(triangle[Number_Frequency]);
		}
		else if (mode == 2)
		{
			DA_out(square[Number_Frequency / 60]);
		}
	}
}
示例#24
0
void Server_run()
{
	int timeout = 1000, rc;
	struct pollfd *pollfds;
	int tcpsock, sockopt = 1;
	struct sockaddr_storage sin;
	int val, clientcount;
	etimer_t janitorTimer;
	const char *port;
	const char *addr;
	struct addrinfo hints, *res;
	bool_t enable_ipv6;

	/* max clients + listen sock + udp sock + client connecting that will be disconnected */
	pollfds = malloc((getIntConf(MAX_CLIENTS) + 3) * sizeof(struct pollfd));
	if (pollfds == NULL)
		Log_fatal("out of memory");

	/* Figure out bind address and port */
	if (bindport != 0)
		port = bindport;
	else
		port = getStrConf(BINDPORT);

	if (bindaddr != 0)
		addr = bindaddr;
	else
		addr = getStrConf(BINDADDR);
	
	/* Prepare TCP socket */

	enable_ipv6 = getBoolConf(ENABLE_IPV6);
	if (enable_ipv6) { /* Test if supported */
		int testsock = socket(AF_INET6, SOCK_STREAM, 0);
		if (testsock < 0 && errno == EAFNOSUPPORT) {
			Log_warn("IPv6 is not supported on this machine. Falling back to IPv4.");
			enable_ipv6 = false;
		}
		if (testsock > 0) close(testsock);
	}

	memset(&hints, 0, sizeof hints);
	if (enable_ipv6) {
		hints.ai_family = AF_INET6;
		hints.ai_socktype = SOCK_STREAM;
		hints.ai_flags = AI_PASSIVE | AI_V4MAPPED;
		hints.ai_protocol = 0;
	} else { /* IPv4 */
		hints.ai_family = AF_INET;
		hints.ai_socktype = SOCK_STREAM;
		hints.ai_flags = AI_PASSIVE;
		hints.ai_protocol = 0;
	}
	rc = getaddrinfo(addr, port, &hints, &res);
	if (rc != 0)
		Log_fatal("getaddrinfo: %s", gai_strerror(rc));

	Log_info("Bind to [%s]:%s", addr ? addr : "::" , port);

	tcpsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
	if (tcpsock < 0)
		Log_fatal("socket: %s", strerror(errno));
	if (setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(int)) != 0)
		Log_fatal("setsockopt: %s",strerror(errno));
	rc = bind(tcpsock, res->ai_addr, res->ai_addrlen);
	if (rc < 0)
		Log_fatal("bind: %s", strerror(errno));
	rc = listen(tcpsock, 3);
	if (rc < 0)
		Log_fatal("listen");
	fcntl(tcpsock, F_SETFL, O_NONBLOCK);

	pollfds[LISTEN_SOCK].fd = tcpsock;
	pollfds[LISTEN_SOCK].events = POLLIN;
	freeaddrinfo(res);

	/* Prepare UDP socket */

	memset(&hints, 0, sizeof hints);
	if (enable_ipv6) {
		hints.ai_family = AF_INET6;
		hints.ai_socktype = SOCK_DGRAM;
		hints.ai_flags = AI_PASSIVE | AI_V4MAPPED;
		hints.ai_protocol = 0;
	} else { /* IPv4 */
		hints.ai_family = AF_INET;
		hints.ai_socktype = SOCK_DGRAM;
		hints.ai_flags = AI_PASSIVE;
		hints.ai_protocol = 0;
	}
	rc = getaddrinfo(addr, port, &hints, &res);
	if (rc != 0) Log_fatal("getaddrinfo: %s", gai_strerror(rc));

	udpsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
	rc = bind(udpsock, res->ai_addr, res->ai_addrlen);
	if (rc < 0)
		Log_fatal("bind %d %s: %s", bindport, bindaddr, strerror(errno));
	val = 0xe0;
	rc = setsockopt(udpsock, IPPROTO_IP, IP_TOS, &val, sizeof(val));
	if (rc < 0)
		Log_warn("Server: Failed to set TOS for UDP Socket");
	val = 0x80;
	rc = setsockopt(udpsock, IPPROTO_IP, IP_TOS, &val, sizeof(val));
	if (rc < 0)
		Log_warn("Server: Failed to set TOS for UDP Socket");
	fcntl(udpsock, F_SETFL, O_NONBLOCK);
	listen(udpsock, 10);

	pollfds[UDP_SOCK].fd = udpsock;
	pollfds[UDP_SOCK].events = POLLIN | POLLHUP | POLLERR;
	freeaddrinfo(res);

	Timer_init(&janitorTimer);
	
	Log_info("uMurmur version %s ('%s') protocol version %d.%d.%d",
	         UMURMUR_VERSION, UMURMUR_CODENAME, PROTVER_MAJOR, PROTVER_MINOR, PROTVER_PATCH);
	Log_info("Visit http://code.google.com/p/umurmur/");
	
	/* Main server loop */
	while (!shutdown_server) {
		struct sockaddr_storage remote;
		int i;
		
		pollfds[UDP_SOCK].revents = 0;
		pollfds[TCP_SOCK].revents = 0;
		clientcount = Client_getfds(&pollfds[2]);
		
		timeout = (int)(1000000LL - (int64_t)Timer_elapsed(&janitorTimer)) / 1000LL;
		if (timeout <= 0) {
			Client_janitor();
			Timer_restart(&janitorTimer);
			timeout = (int)(1000000LL - (int64_t)Timer_elapsed(&janitorTimer)) / 1000LL;
		}
		rc = poll(pollfds, clientcount + 2, timeout);
		if (rc == 0) { /* Timeout */
			/* Do maintenance */
			Timer_restart(&janitorTimer);
			Client_janitor();
			continue;
		}
		if (rc < 0) {
			if (errno == EINTR) /* signal */
				continue;
			else
				Log_fatal("poll: error %d", errno);
		}
		if (pollfds[LISTEN_SOCK].revents) { /* New tcp connection */
			int tcpfd, flag = 1;
			uint32_t addrlen;
			addrlen = sizeof(struct sockaddr_storage);
			tcpfd = accept(pollfds[LISTEN_SOCK].fd, (struct sockaddr*)&remote, &addrlen);
			fcntl(tcpfd, F_SETFL, O_NONBLOCK);
			setsockopt(tcpfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
			Log_debug("Connection from %s port %d\n", inet_ntoa(remote.sin_addr),
					ntohs(remote.sin_port));
			if (Client_add(tcpfd, &remote) < 0)
				close(tcpfd);
		}

		if (pollfds[UDP_SOCK].revents) {
			Client_read_udp();
		}
		for (i = 0; i < clientcount; i++) {
			if (pollfds[i + 2].revents & POLLIN) {
				Client_read_fd(pollfds[i + 2].fd);
			}
			if (pollfds[i + 2].revents & POLLOUT) {
				Client_write_fd(pollfds[i + 2].fd);
			}
		}
	}

	/* Disconnect clients */
	Client_disconnect_all();
	free(pollfds);
}
示例#25
0
文件: Drive.c 项目: SamChenzx/sdp
int main(){
    //Initializations
    Board_init();
    Serial_init();
    Timer_init();
    Drive_init();
    ENABLE_OUT_TRIS = OUTPUT;  
    ENABLE_OUT_LAT = MICRO;

#ifdef RECIEVE_CONTROL
    ENABLE_OUT_LAT = RECIEVER;
    while(1){
        ;
    }
#endif

    printf("Actuator Test Harness Initiated\n\n");

    //Test Rudder
    printf("Centering rudder.\n");
    setRudder(RUDDER_TURN_LEFT, 0);
    delayMillisecond(ACTUATOR_DELAY);

    printf("Turning rudder left.\n");
    setRudder(RUDDER_TURN_LEFT, 100); //push to one direction
    delayMillisecond(ACTUATOR_DELAY);
    printf("Centering rudder.\n");
    setRudder(RUDDER_TURN_LEFT, 0);
    delayMillisecond(ACTUATOR_DELAY);

    printf("Turning rudder right.\n");
    setRudder(RUDDER_TURN_RIGHT, 100);
    delayMillisecond(ACTUATOR_DELAY);
    
    printf("Centering rudder.\n");
    setRudder(RUDDER_TURN_LEFT, 0);
    

    //Test Motor Left
    printf("Testing left motor.\n");
    setLeftMotor(0);
    delayMillisecond(ACTUATOR_DELAY);
    printf("Driving left motor forward.\n");
    setLeftMotor(100);
    delayMillisecond(ACTUATOR_DELAY);
    setLeftMotor(0);
    delayMillisecond(ACTUATOR_DELAY);
    //printf("Driving left motor reverse.\n");
    //setLeftMotor(MIN_PULSE);
    //delayMillisecond(ACTUATOR_DELAY);
    //setLeftMotor(0);
   

    //Test Motor Right
    printf("Testing right motor.\n");
    setRightMotor(0);
    delayMillisecond(ACTUATOR_DELAY);
    printf("Driving right motor forward.\n");
    setRightMotor(100);
    delayMillisecond(ACTUATOR_DELAY);
    setRightMotor(0);
    delayMillisecond(ACTUATOR_DELAY);
    //printf("Driving right motor reverse.\n");
    //setRightMotor(MIN_PULSE);
    //delayMillisecond(ACTUATOR_DELAY);
    //setRightMotor(STOP_PULSE);
    //delayMillisecond(ACTUATOR_DELAY);

//    // Remove this code
//    setRightMotor(MAX_PULSE);
//    setLeftMotor(MAX_PULSE);
//    while (1)
//        asm("nop");
//
//
//    printf("\nDone with drive test.\n");
    return SUCCESS;
}
示例#26
0
文件: Drive.c 项目: SamChenzx/sdp
int main(){
    //Initializations
    Board_init();
    Serial_init();
    Timer_init();
    Drive_init();
    int spd = 10;


    ENABLE_OUT_TRIS = OUTPUT;            //Set Enable output pin to be an output, fed to the AND gates
    ENABLE_OUT_LAT = MICRO;             //Initialize control to that of Microcontroller

    enum{
        MICRO_FORWARD = 0x01,         //State where Microcontroller is driving forward
        MICRO_STOP = 0x02,        //State where Micro is driving backwards
        MICRO_LIMBO = 0x03,
        RECIEVER_STATE = 0x04,        //Reciever has taken over state
    }test_state;

    printf("Boat is Initialized\n");

    //Initialize the state to begin at forward
    test_state = MICRO_FORWARD;
    Drive_stop();
    Timer_new(TIMER_TEST,RECIEVER_DELAY);
    int state_flag = 0;
    Override_init();


    while(1){

        switch(test_state){
            case MICRO_FORWARD:
                printf("STATE: MICRO_FORWARD\n\n\n");
                Drive_forward(spd); //The input param is a pwm duty cycle percentage that gets translated to a RC Servo time pulse
                Timer_new(TIMER_TEST2,RECIEVER_DELAY);
                test_state = MICRO_STOP;

                break;
            case MICRO_STOP:
                if(Timer_isExpired(TIMER_TEST2)){
                    printf("STATE: MICRO_STOP\n\n\n:");
                    Drive_stop();
                    Timer_new(TIMER_TEST2,RECIEVER_DELAY);
                    test_state = MICRO_LIMBO;
                }

                break;
            case MICRO_LIMBO:
                if(Timer_isExpired(TIMER_TEST2)){
                    printf("STATE: MICRO_LIMBO\n\n\n");
                    test_state = MICRO_FORWARD;
                }
                Drive_stop();
                break;
            case RECIEVER_STATE:
                ;
                break;

        }
        //If we got a pulse, control to reciever.
        if (OVERRIDE_TRIGGERED == TRUE){
            printf("Reciever Control\n\n");
            Timer_new(TIMER_TEST, 1000);    //Set timer that is greater than the pulsewidth of the CH3 signal(54Hz)
            OVERRIDE_TRIGGERED = FALSE;     //Re-init to zero so that we know when our pulse is triggered again.
            test_state = RECIEVER_STATE;    //Set state equal to reciever where we do nothing autonomous
            ENABLE_OUT_LAT = RECIEVER;      //Give control over to Reciever using the enable line
            INTEnable(INT_CN,1);
        }
        if (Timer_isExpired(TIMER_TEST)){   //Reciever gave up control
            printf("Micro Has Control\n\n");
            Timer_clear(TIMER_TEST);        //Clear timer so that it doesn't keep registering an expired signal
            test_state = MICRO_LIMBO;     //Set state equal to forward for regular function
            OVERRIDE_TRIGGERED = FALSE;     //Set Override to false to be sure that we don't trigger falsely
            ENABLE_OUT_LAT = MICRO;         //Give Control back to microcontroller using enable line
            Timer_new(TIMER_TEST2, RECIEVER_DELAY);
        }

    }


}
示例#27
0
文件: Drive.c 项目: SamChenzx/sdp
int main(){
    //Initializations
    Board_init();
    Serial_init();
    Timer_init();
    Drive_init();
    int spd = 10;

    ENABLE_TRIS = INPUT;                 //Set the direction of the Enable pin to be an input, a switch will be used for now
    ENABLE_OUT_TRIS = OUTPUT;            //Set Enable output pin to be an output, fed to the AND gates
    ENABLE_OUT_LAT = MICRO;             //Initialize control to that of Microcontroller

    enum{
        MICRO_FORWARD = 0x01,         //State where Microcontroller is driving forward
        MICRO_STOP = 0x02,        //State where Micro is driving backwards
        MICRO_LIMBO = 0x03,
        RECIEVER_STATE = 0x04,        //Reciever has taken over state
    }test_state;

    printf("Boat is Initialized\n");

    //Initialize the state to begin at forward
    test_state = MICRO_FORWARD;
    Drive_stop();
    Timer_new(TIMER_TEST,RECIEVER_DELAY);
    int state_flag = 0;
    while(1){
        if(Timer_isExpired(TIMER_TEST)){
            if ((ENABLE_BIT == 1)){
                test_state = RECIEVER_STATE;
                printf("YOU HAVE TRIGGERED THE RECIEVER FOR CONTROL\n\n");
                ENABLE_OUT_LAT = RECIEVER;                  //Set the Enable_out pin that will be routed to the AND gates
                state_flag = 1;

            }else if (ENABLE_BIT == 0){
                printf("MICRO HAS CONTROL\n\n");
                ENABLE_OUT_LAT = MICRO;                     //setting Enable_out that will be routed to AND gates
                if(state_flag == 1){
                    Timer_new(TIMER_TEST2,RECIEVER_DELAY);
                    test_state = MICRO_FORWARD;
                    state_flag = 0;
                }
            }
            Timer_new(TIMER_TEST,RECIEVER_DELAY);
        }

        switch(test_state){
            case MICRO_FORWARD:
                printf("STATE: MICRO_FORWARD\n\n\n");
                Drive_forward(spd); //The input param is a pwm duty cycle percentage that gets translated to a RC Servo time pulse
                Timer_new(TIMER_TEST2,RECIEVER_DELAY);
                test_state = MICRO_STOP;

                break;
            case MICRO_STOP:
                if(Timer_isExpired(TIMER_TEST2)){
                    printf("STATE: MICRO_STOP\n\n\n:");
                    Drive_stop();
                    Timer_new(TIMER_TEST2,RECIEVER_DELAY);
                    test_state = MICRO_LIMBO;
                }

                break;
            case MICRO_LIMBO:
                if(Timer_isExpired(TIMER_TEST2)){
                    printf("STATE: MICRO_LIMBO\n\n\n");
                    test_state = MICRO_FORWARD;
                }
                Drive_stop();
                break;
            case RECIEVER_STATE:
                ;
                break;

        }

    }


}
示例#28
0
文件: Drive.c 项目: SamChenzx/sdp
int main() {
    enum{
        forward =  0x01,
        backward = 0x02,
        idle    =  0x03,
    }drive_state;

    Board_init();
    Serial_init();
    Timer_init();
    Drive_init();
    int spd = 20;
    //Magnetometer_init();
    printf("Boat initialized.\n");
    Drive_forward(spd);
    Timer_new(TIMER_TEST,TEST_DELAY);
    drive_state = forward;
    int flag = 0;
    while (1) {
        switch(drive_state){
            case forward:
                Drive_forward(spd);
                if(Timer_isExpired(TIMER_TEST)){
                    drive_state = idle;
                    Drive_stop();
                    Timer_new(TIMER_TEST,TEST_DELAY);
                    flag = 0;
                }
                break;
            case idle:
                Drive_stop();
                if(Timer_isExpired(TIMER_TEST)){
                    if (flag == 0){
                        drive_state = backward;
                        Drive_backward(spd);
                    }else if (flag == 1){
                        drive_state = forward;
                        Drive_forward(spd);
                    }
                    Timer_new(TIMER_TEST,TEST_DELAY);
                }

                break;
            case backward:
                Drive_backward(spd);
                if(Timer_isExpired(TIMER_TEST)){
                    drive_state = idle;
                    Drive_stop();
                    Timer_new(TIMER_TEST,TEST_DELAY);
                    flag = 1;
                }
        }

            Drive_runSM();

    }

    Drive_stop();
    Drive_runSM();

    return SUCCESS;
}
示例#29
0
int main(int argc, char** argv)
{
	pspDebugScreenInit();
	pspDebugScreenEnableBackColor(0);
	
	SetupCallbacks();
	
	char* scriptFilename = "script.lua";
	
	lua_State *L;
	L = lua_open();
	luaL_openlibs(L);
	
	Aalib_init(L);
	Color_init(L);
	g2D_init(L);
	intraFont_init(L);
	Ctrl_init(L);
	Power_init(L);
	Time_init(L);
	Timer_init(L);
	Savedata_init(L);
	sceIo_init(L);
	Utility_init(L);
	USB_init(L);
	Xtream_init(L);
	
	
	SceCtrlData keys, oldkeys;
	int status = 0, i;
	
	while (1)
	{
	status = luaL_loadfile(L, scriptFilename);
		
	if (status == 0) 
		status = lua_pcall(L, 0, LUA_MULTRET, 0);
	

		if (status != 0) //If an error has occured
		{
		sceCtrlReadBufferPositive(&oldkeys, 1);
		
		pspDebugScreenInit();
		pspDebugScreenEnableBackColor(1);
		
			while (1)
			{			
				sceCtrlReadBufferPositive(&keys, 1);
			
				pspDebugScreenSetXY(0,0);
				printf("Lua Error:\n%s\n", lua_tostring(L, -1));
				printf("Press Start to reset.\n");
				
				if ((keys.Buttons &PSP_CTRL_START) && !(oldkeys.Buttons &PSP_CTRL_START))
					break;
				
				oldkeys = keys;
				
				for (i = 0; i < 10; i++)
					sceDisplayWaitVblankStart();
			}
			
		pspDebugScreenInit();
		pspDebugScreenEnableBackColor(0);
		
		lua_pop(L, 1);
		}
	}
		
	lua_close(L);

	intraFontShutdown();
	g2dTerm();
	cleanUSBDrivers();

	sceKernelExitGame();

	return 0;
}
示例#30
0
文件: server.c 项目: neokril/umurmur
void Server_run()
{
	int timeout = 1000, rc;
	struct pollfd *pollfds;
	int tcpsock, sockopt = 1;
	struct sockaddr_in sin;
	int val, clientcount;
	etimer_t janitorTimer;
	unsigned short port;
	in_addr_t inet_address;

	/* max clients + listen sock + udp sock + client connecting that will be disconnected */
	pollfds = malloc((getIntConf(MAX_CLIENTS) + 3) * sizeof(struct pollfd));
	if (pollfds == NULL)
		Log_fatal("out of memory");

	/* Figure out bind address and port */
	if (bindport != 0)
		port = htons(bindport);
	else
		port = htons(getIntConf(BINDPORT));

	if (bindaddr != NULL && inet_addr(bindaddr) != -1)
		inet_address = inet_addr(bindaddr);
	else if (inet_addr(getStrConf(BINDADDR)) !=  -1)
		inet_address = inet_addr(getStrConf(BINDADDR));
	else
		inet_address = inet_addr("0.0.0.0");
	Log_info("Bind to %s:%hu", inet_address == 0 ? "*" : inet_ntoa(*((struct in_addr *)&inet_address)), ntohs(port));

	/* Prepare TCP socket */
	memset(&sin, 0, sizeof(sin));
	tcpsock = socket(PF_INET, SOCK_STREAM, 0);
	if (tcpsock < 0)
		Log_fatal("socket");
	if (setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(int)) != 0)
		Log_fatal("setsockopt: %s", strerror(errno));
	sin.sin_family = AF_INET;
	sin.sin_port = port;
	sin.sin_addr.s_addr = inet_address;

	rc = bind(tcpsock, (struct sockaddr *) &sin, sizeof (struct sockaddr_in));
	if (rc < 0) Log_fatal("bind: %s", strerror(errno));
	rc = listen(tcpsock, 3);
	if (rc < 0) Log_fatal("listen");
	fcntl(tcpsock, F_SETFL, O_NONBLOCK);

	pollfds[LISTEN_SOCK].fd = tcpsock;
	pollfds[LISTEN_SOCK].events = POLLIN;

	/* Prepare UDP socket */
	memset(&sin, 0, sizeof(sin));
	udpsock = socket(PF_INET, SOCK_DGRAM, 0);
	sin.sin_family = AF_INET;
	sin.sin_port = port;
	sin.sin_addr.s_addr = inet_address;

	rc = bind(udpsock, (struct sockaddr *) &sin, sizeof (struct sockaddr_in));
	if (rc < 0)
		Log_fatal("bind %d %s: %s", getIntConf(BINDPORT), getStrConf(BINDADDR), strerror(errno));
	val = 0xe0;
	rc = setsockopt(udpsock, IPPROTO_IP, IP_TOS, &val, sizeof(val));
	if (rc < 0)
		Log_warn("Server: Failed to set TOS for UDP Socket");
	val = 0x80;
	rc = setsockopt(udpsock, IPPROTO_IP, IP_TOS, &val, sizeof(val));
	if (rc < 0)
		Log_warn("Server: Failed to set TOS for UDP Socket");

	fcntl(udpsock, F_SETFL, O_NONBLOCK);
	pollfds[UDP_SOCK].fd = udpsock;
	pollfds[UDP_SOCK].events = POLLIN | POLLHUP | POLLERR;

	Timer_init(&janitorTimer);

	Log_info("uMurmur version %s ('%s') protocol version %d.%d.%d",
	         UMURMUR_VERSION, UMURMUR_CODENAME, PROTVER_MAJOR, PROTVER_MINOR, PROTVER_PATCH);
	Log_info("Visit http://code.google.com/p/umurmur/");

	/* Main server loop */
	while (!shutdown_server) {
		struct sockaddr_in remote;
		int i;

		pollfds[UDP_SOCK].revents = 0;
		pollfds[TCP_SOCK].revents = 0;
		clientcount = Client_getfds(&pollfds[2]);

		timeout = (int)(1000000LL - (int64_t)Timer_elapsed(&janitorTimer)) / 1000LL;
		if (timeout <= 0) {
			Client_janitor();
			Timer_restart(&janitorTimer);
			timeout = (int)(1000000LL - (int64_t)Timer_elapsed(&janitorTimer)) / 1000LL;
		}
		rc = poll(pollfds, clientcount + 2, timeout);
		if (rc == 0) { /* Timeout */
			/* Do maintenance */
			Timer_restart(&janitorTimer);
			Client_janitor();
			continue;
		}
		if (rc < 0) {
			if (errno == EINTR) /* signal */
				continue;
			else
				Log_fatal("poll: error %d", errno);
		}
		if (pollfds[LISTEN_SOCK].revents) { /* New tcp connection */
			int tcpfd, flag = 1;
			uint32_t addrlen;
			addrlen = sizeof(struct sockaddr_in);
			tcpfd = accept(pollfds[LISTEN_SOCK].fd, (struct sockaddr*)&remote, &addrlen);
			fcntl(tcpfd, F_SETFL, O_NONBLOCK);
			setsockopt(tcpfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
			Log_debug("Connection from %s port %d\n", inet_ntoa(remote.sin_addr),
					  ntohs(remote.sin_port));
			if (Client_add(tcpfd, &remote) < 0)
				close(tcpfd);
		}

		if (pollfds[UDP_SOCK].revents) {
			Client_read_udp();
		}
		for (i = 0; i < clientcount; i++) {
			if (pollfds[i + 2].revents & POLLIN) {
				Client_read_fd(pollfds[i + 2].fd);
			}
			if (pollfds[i + 2].revents & POLLOUT) {
				Client_write_fd(pollfds[i + 2].fd);
			}
		}
	}

	/* Disconnect clients */
	Client_disconnect_all();
	free(pollfds);
}