예제 #1
0
int16_t main(void) {
     InitUSB();                              // initialize the USB registers and serial interface engine
     initChip();
     initMotor();   

    led_on(&led1);
    timer_setPeriod(LED_TIMER, 0.5);         //start internal clock with defined period
    timer_start(LED_TIMER);

    pin_write(IN1, 1);
    pin_write(IN2, 0);
    pin_write(D2, 65536*2/5);

    while (USB_USWSTAT!=CONFIG_STATE) {     // while the peripheral is not configured...
        ServiceUSB();                       // ...service USB requests
    }

    while (1) {
        ServiceUSB();                       // service any pending USB requests

        //LED1 TOGGLE TO CONFIRM RUNNING CODE
        if (timer_flag(LED_TIMER)) {
            timer_lower(LED_TIMER);
            led_toggle(&led1);

            pin_write(D2, VAL1);
        }       
    }
}
예제 #2
0
파일: usbhdfsd.c 프로젝트: AKuHAK2/ps2sdk
int _start( int argc, char *argv[])
{
	printf("USB HDD FileSystem Driver v%d.%d\n", MAJOR_VER, MINOR_VER);

	if (RegisterLibraryEntries(&_exp_usbmass) != 0) {
		printf("USBHDFSD: Already registered.\n");
		return MODULE_NO_RESIDENT_END;
	}

	// initialize the FAT driver
	if(InitFAT() != 0)
	{
		printf("USBHDFSD: Error initializing FAT driver!\n");
		return MODULE_NO_RESIDENT_END;
	}

	// initialize the USB driver
	if(InitUSB() != 0)
	{
		printf("USBHDFSD: Error initializing USB driver!\n");
		return MODULE_NO_RESIDENT_END;
	}

	// initialize the file system driver
	if(InitFS() != 0)
	{
		printf("USBHDFSD: Error initializing FS driver!\n");
		return MODULE_NO_RESIDENT_END;
	}

	// return resident
	return MODULE_RESIDENT_END;
}
예제 #3
0
int16_t main(void) {
	//initialize all system clocks
    init_clock();
	//initialize serial communications
    init_uart();
	//initialize pin driving library (to be able to use the &D[x] defs)
	init_pin();
	//initialize the UI library
    init_ui();
	//initialize the timer module
    init_timer();
	//initialize the OC module (used by the servo driving code)
	init_oc();
	
    imu_init()
	//Set servo control pins as output
	pin_digitalOut(PAN_PIN);
	pin_digitalOut(TILT_PIN);
	pin_digitalOut(SONIC_OUT_PIN);
	pin_digitalIn(SONIC_IN_PIN);
	
	//Set LED off
	led_off(LED);
	//Configure blinking rate for LED when connected
    timer_setPeriod(LED_TIM, 0.2);
    timer_start(LED_TIM);
	

    //Configure timer for reciever timeout
    timer_setPeriod(DIST_TIM, 0.05);


	//configure PWM on sonic output pin
	oc_pwm(PWM_OC, SONIC_OUT_PIN, NULL, SONIC_FREQ, 0x0000);
	
	//According to HobbyKing documentation: range .8 through 2.2 msec
	//Set servo control pins as OC outputs on their respective timers
	oc_servo(SERVO1_OC, PAN_PIN, 	SERVO1_TIM, SERVO_PERIOD, SERVO_MIN, SERVO_MAX, pan_set_val);
	oc_servo(SERVO2_OC, TILT_PIN, 	SERVO2_TIM, SERVO_PERIOD, SERVO_MIN, SERVO_MAX, tilt_set_val);

    InitUSB();                              // initialize the USB registers and serial interface engine
    while (USB_USWSTAT!=CONFIG_STATE) {     // while the peripheral is not configured...
        ServiceUSB();                       // ...service USB requests
		led_on(LED);
		//There's no point in driving the servos when there's no one connected yet.
    }
	
    while (1) {
        ServiceUSB();                       // service any pending USB requests
		
		//blink the LED
		if (timer_flag(LED_TIM)) {
            timer_lower(LED_TIM);
            led_toggle(LED);
        }
		
		//Update the servo control values.
        x_gout = gyro_read(OUT_X_L);
    }
}
예제 #4
0
int16_t main(void) {
    init_clock();
    init_ui();
    init_pin();
    init_spi();

    ENC_MISO = &D[1];
    ENC_MOSI = &D[0];
    ENC_SCK = &D[2];
    ENC_NCS = &D[3];

    pin_digitalOut(ENC_NCS);
    pin_set(ENC_NCS);

    spi_open(&spi1, ENC_MISO, ENC_MOSI, ENC_SCK, 2e8);


    InitUSB();                              // initialize the USB registers and serial interface engine
    while (USB_USWSTAT!=CONFIG_STATE) {     // while the peripheral is not configured...
        ServiceUSB();                       // ...service USB requests
    }
    while (1) {
        ServiceUSB();                       // service any pending USB requests
    }
}
예제 #5
0
int16_t main(void) {
    init_clock();
    init_ui();
    init_pin();
    init_timer();
    init_i2c();
    InitUSB();
    init_oc();

    init_display(&i2c3, 0x70, 0x71);
    init_game(&timer1, &timer2, &A[0], &disp1, &disp2);
    init_gun(&D[13], &A[1], &A[2], &timer3);
    init_launcher(&D[5], &D[8], &D[3], &D[4]);
    init_shooter(&D[6], &D[9], &D[1], &D[2], &D[7], &D[0], &oc3, &oc4);
    init_baller(&D[10], &oc2, &timer4, &shooter, &launcher);
    init_pix(&D[11], &timer5, 30, 0.05);
    init_audio(&D[12]);

    uint8_t level = 0;
    uint8_t hit_flag = 0;
    while (1) {
        ServiceUSB(); // usb times out if not checked fast enough :/
        level = run_game(hit_flag);
        ServiceUSB();
        hit_flag = run_gun(level);
        ServiceUSB();
        run_baller(level);
    }
}
예제 #6
0
void* RunLoopThread(void* args)
{
    unsigned i;

    InitUSB();

    currentRunLoop = CFRunLoopGetCurrent();

    CFRunLoopTimerContext context = {0, NULL, NULL, NULL, NULL};
    CFRunLoopTimerRef timer = CFRunLoopTimerCreate(kCFAllocatorDefault, 0.1, 0.1, 0, 0, &onTimerFired, &context);
    CFRunLoopAddTimer(currentRunLoop, timer, kCFRunLoopCommonModes);

    // Signal the parent that we are running
    adb_mutex_lock(&start_lock);
    adb_cond_signal(&start_cond);
    adb_mutex_unlock(&start_lock);

    CFRunLoopRun();
    currentRunLoop = 0;

    for (i = 0; i < vendorIdCount; i++) {
        IOObjectRelease(notificationIterators[i]);
    }
    IONotificationPortDestroy(notificationPort);

    usb_cleanup();

    DBG("RunLoopThread done\n");
    return NULL;    
}
예제 #7
0
void setup(void) {
    // Initialize PIC24 modules.
    init_clock();
    init_ui();
    init_timer();
    init_pin();
    init_oc();
    init_spi();
    init_uart();

    // Configure single SPI comms. system
    pin_digitalOut(SPI_CS);
    pin_set(SPI_CS);
    spi_open(spi_inst, SPI_MISO, SPI_MOSI, SPI_SCK, spi_freq, spi_mode);

    // Configure & start timers used.
    timer_setPeriod(&timer1, 1);
    timer_setPeriod(&timer2, 1);  // Timer for LED operation/status blink
    timer_setPeriod(&timer3, LOOP_TIME); // Timer for main control loop
    timer_start(&timer1);
    timer_start(&timer2);
    timer_start(&timer3);

    // Configure dual PWM signals for bidirectional motor control
    oc_pwm(&oc1, PWM_I1, NULL, pwm_freq, pwm_duty);
    oc_pwm(&oc2, PWM_I2, NULL, pwm_freq, pwm_duty);

    InitUSB();                              // initialize the USB registers and
                                            // serial interface engine
    while (USB_USWSTAT != CONFIG_STATE) {   // while periph. is not configured,
        ServiceUSB();                       // service USB requests
    }
}
예제 #8
0
int16_t main(void) {
    // printf("Starting Rocket Controller...\r\n");
    init_clock();
    init_ui();
    init_pin();
    init_timer();
    init_i2c();
    setup();
    init_servo_driver(&sd1, &i2c3, 16000., 0x0);
    init_servo(&servo4, &sd1, 0);
    InitUSB();
    U1IE = 0xFF; //setting up ISR for USB requests
    U1EIE = 0xFF;
    IFS5bits.USB1IF = 0; //flag
    IEC5bits.USB1IE = 1; //enable
    // uint32_t pid_command;
    servo_set(&servo4, 1500, 0);
    while (1) {
        if (timer_flag(&timer1)) {
            // Blink green light to show normal operation.
            timer_lower(&timer1);
            led_toggle(&led2);
            // servo_set(&servo4, 1500, 0);
        }
    }
}
예제 #9
0
int16_t main(void) {
    init_clock();
    init_timer();
    init_ui();
    init_pin();
    init_spi();
    init_oc();
    init_md();

    // Current measurement pin
    pin_analogIn(&A[0]);

    // SPI pin setup
    ENC_MISO = &D[1];
    ENC_MOSI = &D[0];
    ENC_SCK = &D[2];
    ENC_NCS = &D[3];
    pin_digitalOut(ENC_NCS);
    pin_set(ENC_NCS);

    // Open SPI in mode 1
    spi_open(&spi1, ENC_MISO, ENC_MOSI, ENC_SCK, 2e6, 1);

    // Motor setup
    md_velocity(&md1, 0, 0);

    // Get initial angle offset
    uint8_t unset = 1;
    while (unset) {
        ANG_OFFSET = enc_readReg((WORD) REG_ANG_ADDR);
        unset = parity(ANG_OFFSET.w);
    }
    ANG_OFFSET.w &= ENC_MASK;

    // USB setup
    InitUSB();
    while (USB_USWSTAT!=CONFIG_STATE) {
        ServiceUSB();
    }

    // Timers
    timer_setFreq(&timer2, READ_FREQ);
    timer_setFreq(&timer3, CTRL_FREQ);
    timer_start(&timer2);
    timer_start(&timer3);

    // Main loop
    while (1) {
        ServiceUSB();
        if (timer_flag(&timer2)) {
            timer_lower(&timer2);
            get_readings();
        }
        if (timer_flag(&timer3)) {
            timer_lower(&timer3);
            set_velocity();
        }
    }
}
예제 #10
0
int16_t main(void) {
    init_clock();
    init_ui();
    init_pin();
    init_spi();

    init_timer();
    init_oc();
    init_md();

    led_off(&led2);
    led_off(&led3);

    ENC_MISO = &D[1];
    ENC_MOSI = &D[0];
    ENC_SCK = &D[2];
    ENC_NCS = &D[3];


    read_angle.w=0x3FFF;
    Pscale.w=1;
    Iscale.w=0;
    direction.w=1;
    speed.w=0;
    angle_now.i=180;
    angle_prev.i=180;
    angle.w=10;
    uint8_t loop = 0;

    pin_digitalOut(ENC_NCS);
    pin_set(ENC_NCS);

    spi_open(&spi1, ENC_MISO, ENC_MOSI, ENC_SCK, 2e8,1);

    timer_setPeriod(&timer1, 0.05);
    timer_start(&timer1);

    InitUSB();  
                              // initialize the USB registers and serial interface engine
    while (USB_USWSTAT!=CONFIG_STATE) {     // while the peripheral is not configured...
        ServiceUSB();                       // ...service USB requests
    }  

   while(1){
    ServiceUSB();
        if (timer_flag(&timer1)) {
            timer_lower(&timer1);
            angle_prev=angle_now; 
            angle_prev_con=angle;                // service any pending USB requests
            angle_now = enc_readReg(read_angle);
            angle_now = mask_angle(angle_now);
            angle=convert_Angle(angle_prev,angle_now,&loop);

            spring_simple(angle);
        }
    }    
}
예제 #11
0
void init(void){
    init_pin();
    init_clock();
    init_uart();
    init_ui();
    init_timer();
    init_oc();
    init_motor();
    InitUSB(); // initialize the USB registers and serial interface engine    
    init_interrupts();
}
예제 #12
0
void setup(void) {
    // Initialize PIC24 modules.
    init_clock();
    init_ui();
    init_timer();
    init_pin();
    init_oc();
    init_spi();
    init_uart();

    // Configure single SPI comms. system
    pin_digitalOut(SPI_CS);
    pin_set(SPI_CS);
    spi_open(spi_inst, SPI_MISO, SPI_MOSI, SPI_SCK, spi_freq, spi_mode);

    // Configure & start timers used.
    timer_setPeriod(&timer1, 1);
    timer_setPeriod(&timer2, 1);  // Timer for LED operation/status blink
    timer_setPeriod(&timer3, LOOP_TIME); // Timer for main control loop
    timer_start(&timer1);
    timer_start(&timer2);
    timer_start(&timer3);

    // Configure motor current conversion coefficient
    CURRENT_CONV_COEF = MAX_ADC_OUTPUT * MOTOR_VOLTAGE_RESISTOR;
    cur_control.Kp = KP;
    cur_control.Kd = KD;
    cur_control.Ki = KI;
    cur_control.dt = LOOP_TIME;
    cur_control.integ_min = -100;
    cur_control.integ_max = 100;
    cur_control.integ_state = 0;
    read_motor_current(&motor);
    cur_control.prev_position = convert_motor_torque(motor.current);
    // Configure dual PWM signals for bidirectional motor control
    oc_pwm(&oc1, PWM_I1, NULL, pwm_freq, pwm_duty);
    oc_pwm(&oc2, PWM_I2, NULL, pwm_freq, pwm_duty);
    pin_analogIn(MOTOR_VOLTAGE);
    pin_digitalOut(DEBUGD0);
    pin_digitalOut(DEBUGD1);

    InitUSB();                              // initialize the USB registers and
                                            // serial interface engine
    while (USB_USWSTAT != CONFIG_STATE) {   // while periph. is not configured,
        ServiceUSB();                       // service USB requests
    }
}
예제 #13
0
void* RunLoopThread(void* unused)
{
    InitUSB();

    currentRunLoop = CFRunLoopGetCurrent();

    // Signal the parent that we are running
    adb_mutex_lock(&start_lock);
    adb_cond_signal(&start_cond);
    adb_mutex_unlock(&start_lock);

    CFRunLoopRun();
    currentRunLoop = 0;

    IOObjectRelease(notificationIterator);
    IONotificationPortDestroy(notificationPort);

    DBG("RunLoopThread done\n");
    return NULL;
}
예제 #14
0
int16_t main(void) {
    init_clock();
    init_uart();
    init_ui();
    init_timer();
    init_oc();
    
    led_on(&led2);
    timer_setPeriod(&timer2, 0.5);
    timer_start(&timer2);

    val1 = 0;
    val2 = 0;

    interval = 0.02;
    min_width = 5.5E-4;
    max_width = 2.3E-3;
    pos = 0; //16 bit int with binary point in front of the MSB

    oc_servo(&oc1,&D[0],&timer1, interval,min_width, max_width, pos);
    oc_servo(&oc2,&D[2],&timer3, interval,min_width, max_width, pos);

    printf("Good morning\n");

    InitUSB();                              // initialize the USB registers and serial interface engine
    while (USB_USWSTAT!=CONFIG_STATE) {     // while the peripheral is not configured...
        ServiceUSB();                       // ...service USB requests
    }
    while (1) {
        ServiceUSB();
        //write the values to the servos (move the servos to the requested position)
        pin_write(&D[0],val1);
        pin_write(&D[2],val2);                     
        if (timer_flag(&timer2)) {
            //show a heartbeat and a status message
            timer_lower(&timer2);
            led_toggle(&led1);
            printf("val1 = %u, val2 = %u\n", val1, val2);
        }
    }
}
예제 #15
0
int16_t main(void) {
    init_clock();
    init_ui();
    init_pin();
    init_timer();
    init_oc();
    init_spi();
    init_enc();
    init_md();
    init_motor();

    // (&motor1)->vel_set = 360.0;

    InitUSB();
    while (USB_USWSTAT!=CONFIG_STATE) {
        ServiceUSB();
    }
    while (1) {
        ServiceUSB();
    }
}
예제 #16
0
/*lint -save  -e970 Disable MISRA rule (6.3) checking. */
int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
  /* Write your local variable definition here */

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/

  InitUSB();

  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;){}
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
예제 #17
0
파일: main.c 프로젝트: ClockSelect/myevic
//=========================================================================
//----- (00000C48) --------------------------------------------------------
__myevic__ void InitHardware()
{
	SYS_UnlockReg();

	//  32.768kHz external crystal
	if ( dfStatus.x32off )
	{
		CLK_DisableXtalRC( CLK_PWRCTL_LXTEN_Msk );
	}
	else
	{
		SYS->GPF_MFPL &= ~(SYS_GPF_MFPL_PF0MFP_Msk|SYS_GPF_MFPL_PF1MFP_Msk);
		SYS->GPF_MFPL |=  (SYS_GPF_MFPL_PF0MFP_X32_OUT|SYS_GPF_MFPL_PF1MFP_X32_IN);

		CLK_EnableXtalRC( CLK_PWRCTL_LXTEN_Msk );
		CLK_WaitClockReady( CLK_STATUS_LXTSTB_Msk );
	}

	SetPWMClock();

	SYS_LockReg();

	#if (ENABLE_UART)
	InitUART0();
	#endif

	InitGPIO();

	if ( !PD3 )
	{
		gFlags.noclock = 1;
	}

	InitSPI0();
	InitEADC();
	InitPWM();
	InitTimers();
	InitUSB();
}
예제 #18
0
void* RunLoopThread(void* args)
{
    unsigned i;

    InitUSB();

    currentRunLoop = CFRunLoopGetCurrent();

    CFRunLoopTimerContext context = {0, NULL, NULL, NULL, NULL};
    CFRunLoopTimerRef timer = CFRunLoopTimerCreate(kCFAllocatorDefault, 0.1, 0.1, 0, 0, &onTimerFired, &context);
    CFRunLoopAddTimer(currentRunLoop, timer, kCFRunLoopCommonModes);

    // Signal the parent that we are running
    adb_mutex_lock(&start_lock);
    adb_cond_signal(&start_cond);
    adb_mutex_unlock(&start_lock);

    set_device_loop_state(kDeviceLoopRunning);
    CFRunLoopRun();
    set_device_loop_state(kDeviceLoopDead);
    currentRunLoop = 0;

    for (i = 0; i < vendorIdCount; i++) {
        IOObjectRelease(notificationIterators[i]);
    }
    IONotificationPortDestroy(notificationPort);

    if (notificationIterators != NULL) {
        D("Before notification free\n");
        free(notificationIterators);
        notificationIterators = NULL;
    }
    D("After all\n");
    initialized = 0;

    DBG("RunLoopThread done\n");
    return NULL;    
}
예제 #19
0
int16_t main(void) {
    init_clock();
    init_timer();
    init_pin();
    init_oc();
    init_md();

    pin_analogIn(&A[0]);

    md_velocity(&md1, 0xF000, 1);

    InitUSB();                              // initialize the USB registers and serial interface engine
    while (USB_USWSTAT!=CONFIG_STATE) {     // while the peripheral is not configured...
        LAST_CURRENT = CURRENT_CURRENT;
        CURRENT_CURRENT.w = pin_read(&A[0]);
        ServiceUSB();                       // ...service USB requests
    }
    while (1) {
        LAST_CURRENT = CURRENT_CURRENT;
        CURRENT_CURRENT.w = pin_read(&A[0]);
        ServiceUSB();                       // service any pending USB requests
    }
}
예제 #20
0
void InitAntTracker()
{
	pageRAMaddr=0;

	LED = 1;

	CheckConfig();

	tics=0;
	set_servo(SERVO_PAN,1500);
	set_servo(SERVO_TILT,1500);
//	set_servo(SERVO_AUX,1500);
	
	UpdatedDatosAvion=0;

	datosAvion.lon = -6.0f;
	datosAvion.lat = 37.0f;
	datosAvion.alt = 0.0f;

	datosAvion.home_lon = -6.0f;
	datosAvion.home_lat = 37.0f;
	datosAvion.home_alt = 0.0f;

	debugInfo.EnableDebug = 0;
	debugInfo.pan = 1500;
	debugInfo.tilt = 1500;
	debugInfo.grados_pan = 0.0f;
	debugInfo.grados_tilt = 0.0f;

	InitUSB();
	InitPWM();

	offset_pan = 0.0f;

	AD0BUSY = 1;
	
}
예제 #21
0
void* RunLoopThread(void* unused)
{
    unsigned i;

    InitUSB();

    currentRunLoop = CFRunLoopGetCurrent();

    // Signal the parent that we are running
    sdb_mutex_lock(&start_lock);
    sdb_cond_signal(&start_cond);
    sdb_mutex_unlock(&start_lock);

    CFRunLoopRun();
    currentRunLoop = 0;

    for (i = 0; i < vendorIdCount; i++) {
        IOObjectRelease(notificationIterators[i]);
    }
    IONotificationPortDestroy(notificationPort);

    DBG("RunLoopThread done\n");
    return NULL;    
}
예제 #22
0
파일: main.c 프로젝트: max1325/uae-wii
int main (int argc, char **argv)
{
	
	#ifdef GEKKO
	
	printf("\x1b[2;0H");

	//initialize libfat library
	if (fatInitDefault())
		printf("FAT subsytem initialized\n\n");
	else
		{
		printf("Couldn't initialize FAT subsytem\n\n");
		sleep(3);
		exit(0);
		}
		
	DIR *dp;
    
	dp = opendir ("sd:/");
	if (dp) sdismount = 1; else sdismount = 0;
	
	if (sdismount)
		printf("SD FAT subsytem initialized\n\n");
	else
		printf("Couldn't initialize SD fat subsytem\n\n");
 	
	if (sdismount) closedir (dp);
	
	usbismount = InitUSB();
	

    default_prefs (&currprefs, 0);
	cfgfile_load (&currprefs, SMBFILENAME, 0);
	printf("\n");
	
	if (currprefs.smb_enable) networkisinit = InitNetwork();
	
	if (networkisinit && currprefs.smb_enable) ConnectShare(); 

	sleep(2);

	if (!(log_quiet = !currprefs.write_logfile)) set_logfile("/uae/uae.log");
	
	#endif
	
	write_log("main started\n");
    init_sdl ();
	write_log("sdl inited\n");
    gui_init (argc, argv);
	write_log("Starting real main\n");
    real_main (argc, argv);
	
	#ifdef GEKKO
	if (smbismount) CloseShare ();
	DeInitUSB();
	fatUnmount(0);
	#endif

    return 0;
}
예제 #23
0
int16_t main(void) {
    init_pin();
    init_clock();
    init_uart();
    init_ui();
    init_timer();
    init_oc();

    //setup the signal input pin
    pin_digitalIn(&D[4]);

    val1 = 0;
    val2 = 0;
    pos = 0; //16 bit int with binary point in front of the MSB

    led_on(&led2);
    timer_setPeriod(&timer2, PULSE_FREQUENCY); //how often we send a pulse
    timer_start(&timer2);
    timer_setPeriod(&timer3, 0.5); //heartbeat
    timer_start(&timer3);

    oc_servo(&oc1,&D[0],&timer4, INTERVAL,MIN_WIDTH, MAX_WIDTH, pos);
    oc_servo(&oc2,&D[2],&timer5, INTERVAL,MIN_WIDTH, MAX_WIDTH, pos);
    oc_pwm(&oc3,&D[3],NULL,FREQ,ZERO_DUTY);

    printf("Good morning\n");

    InitUSB();                              // initialize the USB registers and serial interface engine
    while (USB_USWSTAT!=CONFIG_STATE) {     // while the peripheral is not configured...
        ServiceUSB();                       // ...service USB requests
    }
    while (1) {
        ServiceUSB();
        pin_write(&D[0],val1);
        pin_write(&D[2],val2); 
        //adapted from Patrick and Charlie's approach
        if (!send_pulse && timer_read(&timer2) < PULSE_WIDTH){
            send_pulse = 1;
            pin_write(&D[3],HALF_DUTY);     
            get_distance = 1;
        } else if (send_pulse && timer_read(&timer2) >= PULSE_WIDTH) {
            send_pulse = 0;
            pin_write(&D[3],ZERO_DUTY); 
        }

        if (timer_read(&timer2) >= ECHO_TIME)
        {
            if (pin_read(&D[4]) && get_distance)
            {
                printf("%d\n", timer_read(&timer2));
                get_distance = 0;
            }
        }
       if (timer_flag(&timer3)) {
            //show a heartbeat and a status message
            timer_lower(&timer3);
            led_toggle(&led1);
        }

    }
}
예제 #24
0
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	MSG msg;
	//HACCEL hAccelTable;
	int bnum;
	RECT screen;
	TCHAR ini_fname[256];

	// グローバル文字列を初期化しています。
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_CONEMU_WIN, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// 表示領域の定義
	// 必要以外の再書き換えを防止する
	screen.left = 0;
	screen.right = 650;
	screen.top = 0;
	screen.bottom = 399;

	// ini ファイルから設定を読む
	if (MakeIniFileName(_T("CONEMU.ini"), ini_fname)) {
		int value;
		value = GetPrivateProfileInt(_T("FRAMERATE"), _T("FPS"), 7, ini_fname);
		switch (value)
		{
		case 60:
			FPSmode = FPS60;
			break;
		case 30:
			FPSmode = FPS30;
			break;
		case 15:
			FPSmode = FPS15;
			break;
		default:
			FPSmode = FPS07;
			break;
		}
		Offset_X2 = GetPrivateProfileInt(_T("200LINES"), _T("OFFSET_X"), 20, ini_fname);
		Offset_Y2 = GetPrivateProfileInt(_T("200LINES"), _T("OFFSET_Y"), 34, ini_fname);
		Offset_X4 = GetPrivateProfileInt(_T("400LINES"), _T("OFFSET_X"), 20, ini_fname);
		Offset_Y4 = GetPrivateProfileInt(_T("400LINES"), _T("OFFSET_Y"), 34, ini_fname);
	}

	// USB 初期化
	if (InitUSB()) {
		MessageBox(NULL, _T("初期化に失敗しました。\r\nケーブルの接続を確認してください。"), _T("エラー"), MB_OK);
		return 1;
	}

	reallocDIB();
	ImmDisableIME(-1);

	// アプリケーションの初期化を実行します:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	// 画面描画ポジションテーブル
	for (size_t i = 0; i < 400; i++)
	{
		ppos[i] = g_lppxDIB + RowBytes * ((Lines - 1) - i);
	}

	// メイン メッセージ ループ:
	while (true)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
		{
			if (!(GetMessage(&msg, NULL, 0, 0)))
			{
				// USB 解放
				TrashUSB();

				// ini ファイルに設定を書き込み
				if (MakeIniFileName(_T("CONEMU.ini"), ini_fname)) {
					TCHAR cstr[10];
					switch (FPSmode)
					{
					case FPS60:
						wsprintf(cstr, _T("60"));
						break;
					case FPS30:
						wsprintf(cstr, _T("30"));
						break;
					case FPS15:
						wsprintf(cstr, _T("15"));
						break;
					case FPS07:
						wsprintf(cstr, _T("07"));
						break;
					default:
						break;
					}
					WritePrivateProfileString(_T("FRAMERATE"), _T("FPS"), cstr, ini_fname);
					wsprintf(cstr, _T("%d"), Offset_X2);
					WritePrivateProfileString(_T("200LINES"), _T("OFFSET_X"), cstr, ini_fname);
					wsprintf(cstr, _T("%d"), Offset_Y2);
					WritePrivateProfileString(_T("200LINES"), _T("OFFSET_Y"), cstr, ini_fname);
					wsprintf(cstr, _T("%d"), Offset_X4);
					WritePrivateProfileString(_T("400LINES"), _T("OFFSET_X"), cstr, ini_fname);
					wsprintf(cstr, _T("%d"), Offset_Y4);
					WritePrivateProfileString(_T("400LINES"), _T("OFFSET_Y"), cstr, ini_fname);
				}

				return (int)msg.wParam;
			}
				TranslateMessage(&msg);
				DispatchMessage(&msg);
		}
		else
		{
			bnum = ConvPixel();
			switch (bnum)
			{
			case 0:		// まだ1ライン分のデータが揃ってない
				break;
			case 399:	// 最下行のデータを受信
				InvalidateRect(hWnd, &screen, FALSE);
				break;
			default:
				break;
			}
		}
	}

}
예제 #25
0
파일: main.cpp 프로젝트: arne-cl/frodo-wii
extern "C" int main(int argc, char **argv)
{
	timeval tv;
	gettimeofday(&tv, NULL);
	srand(tv.tv_usec);
#if defined(GEKKO)
	DIR *dir_tmp;
#endif

	// Init SDL
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK) < 0) {
                fprintf(stderr, "Couldn't initialize SDL (%s)\n", SDL_GetError());
                return 1;
	}
        if (TTF_Init() < 0)
        {
                fprintf(stderr, "Unable to init TTF: %s\n", TTF_GetError() );
		return 1;
        }

	fflush(stdout);

	#ifdef GEKKO
	
	printf("\x1b[2;0H");

	//initialize libfat library
	if (fatInitDefault())
		printf("FAT subsytem initialized\n\n");
	else
		{
		printf("Couldn't initialize FAT subsytem\n\n");
		sleep(3);
		exit(0);
		}
		
	DIR *dp;
    
	dp = opendir ("sd:/");
	if (dp) sdismount = 1; else sdismount = 0;
	
	if (sdismount)
		printf("SD FAT subsytem initialized\n\n");
	else
		printf("Couldn't initialize SD fat subsytem\n\n");
 	
	if (sdismount) closedir (dp);
	
	usbismount = InitUSB();
	if (usbismount) 
		printf("USB FAT subsytem initialized\n\n");
	else
		printf("Impossible to initialize USB FAT subsytem\n\n");
	
	networkisinit = InitNetwork();	
		
	sleep(2);
	
	//create tmp directory if it does not exist
	dir_tmp = opendir("/frodo/tmp");	
	if (!dir_tmp) {mkdir("/frodo/tmp",0777);printf("Making tmp directory\n");sleep(2);} else closedir(dir_tmp);
	
	//Cancel the old files
	unlink ("/frodo/tmp/a");
	unlink ("/frodo/tmp/dummy");
	
	#endif

	Frodo *the_app = new Frodo();
	the_app->ArgvReceived(argc, argv);
	the_app->ReadyToRun();
	delete the_app;
	
	if (smbismount) CloseShare (true);

	#ifdef GEKKO
	DeInitUSB();
	fatUnmount(0);
	#endif

	return 0;
}
예제 #26
0
파일: bud.c 프로젝트: trevhoot/BestBuds
int16_t main(void) {
	init_clock();
	init_timer();
	init_pin();
	init_oc();
	init_ui();

	InitUSB();							  // initialize the USB registers and serial interface engine
	while (USB_USWSTAT!=CONFIG_STATE) {	 // while the peripheral is not configured...
		ServiceUSB();					   // ...service USB requests
	}

	// Configure Interrupts on the pic
	IEC1bits.CNIE = 1;
	CNEN1bits.CN2IE = 1;
	IFS1bits.CNIF = 0;

	IEC0bits.OC1IE = 1;
	IFS0bits.OC1IF = 0;
	timer_enableInterrupt(&timer1);
	timer_lower(&timer1);
	timer_enableInterrupt(&timer2);
	timer_lower(&timer2);
	timer_enableInterrupt(&timer4);
	timer_lower(&timer4);
	timer_enableInterrupt(&timer5);
	timer_lower(&timer5);



	// Configure Pins
	inPin0 = &A[0];	
	pin_analogIn(inPin0);
	inPin1 = &A[1];	
	pin_analogIn(inPin1);
	inPin2 = &A[2];	
	pin_analogIn(inPin2);
	inPin3 = &A[3];	
	pin_analogIn(inPin3);
	inPin4 = &A[4];	
	pin_analogIn(inPin4);

	irPin = &A[5];	
	pin_analogIn(irPin);

	outPin = &D[6];
	pin_digitalOut(outPin);
	oc_pwm(&oc1, outPin, NULL, 10, (uint16_t)(0));		// write to D2 with a 10Hz PWM signal
	pin_write(outPin, 10000);	//duty doesn't matter, really. 

	redPin = &D[7];
	pin_digitalOut(redPin);
	oc_pwm(&oc2, redPin, NULL, 100, (uint16_t)(0));	
	greenPin = &D[10];
	pin_digitalOut(greenPin);
	oc_pwm(&oc3, greenPin, NULL, 100, (uint16_t)(0));	
	bluePin = &D[8];
	pin_digitalOut(bluePin);
	oc_pwm(&oc4, bluePin, NULL, 100, (uint16_t)(0));	

	pingPin = &D[4];
	pin_digitalOut(pingPin);
	oc_pwm(&oc5, pingPin, &timer3, 40000, 0);
	receivePin = &D[12];
	pin_digitalIn(receivePin);

	// Motor controller pins
	dirPin = &D[0];
	pin_digitalOut(dirPin);
	nSleepPin = &D[3];
	pin_digitalOut(nSleepPin);
	pin_write(nSleepPin, 1);
	stepPin = &D[2];
	pin_digitalOut(stepPin);

	testPin = &D[13];
	pin_digitalOut(testPin);

	timer_setFreq(&timer1, 100);


	while (1) {
		ServiceUSB(); // service any pending USB requests


		
		irVoltage = pin_read(irPin);
		if (irVoltage < 40000){
			dist = 32768;
		}
		if (irVoltage >= 40000){
			dist = 32900;
		}


		if (dist != stepCount) {
			changeFlag += 1;
		} else {
			changeFlag = 0;
		}

		if (changeFlag >= 3){
			changeFlag = 0;
			motorControl(dist);
		}


		if (touching0 == 10){
			greenTarget = 40000;
			redTarget = 60000;
			blueTarget = 0;
			if (currentPetal == 0){
				greenTarget = 0;
				redTarget = 0;
				blueTarget = 0;
			}
			currentPetal == 0;
		}
		if (touching1 == 11){
			greenTarget = 20000;
			redTarget = 20000;
			blueTarget = 20000;
			if (currentPetal == 1){
				greenTarget = 0;
				redTarget = 0;
				blueTarget = 0;
			}
			currentPetal == 1;
		}
		if (touching2 == 12){
			greenTarget = 0;
			redTarget = 60000;
			blueTarget = 40000;
			if (currentPetal == 2){
				greenTarget = 0;
				redTarget = 0;
				blueTarget = 0;
			}
			currentPetal == 2;
		}
		if (touching3 == 13){
			greenTarget = 0;
			redTarget = 60000;
			blueTarget = 0;
			if (currentPetal == 3){
				greenTarget = 0;
				redTarget = 0;
				blueTarget = 0;
			}
			currentPetal == 3;
			
		}
		if (touching4 == 14){
			greenTarget = 60000;
			redTarget = 0;
			blueTarget = 0;
			if (currentPetal == 4){
				greenTarget = 0;
				redTarget = 0;
				blueTarget = 0;
			}
			currentPetal == 4;

		}
		if (greenDuty < greenTarget) {
			greenChange = 1;
		} else if (greenDuty > greenTarget) {
			greenChange = -1;
		} else {
			greenChange = 0;
			onTarget += 1;
		}
		if (redDuty < redTarget) {
			redChange = 1;
		} else if (redDuty > redTarget) {
			redChange = -1;
		} else {
			redChange = 0;
			onTarget += 1;
		}
		if (blueDuty < blueTarget) {
			blueChange = 1;
		} else if (blueDuty > blueTarget) {
			blueChange = -1;
		} else {
			blueChange = 0;
			onTarget += 1;
		}
		greenDuty += greenChange;
		redDuty += redChange;
		blueDuty += blueChange;
		pin_write(greenPin, greenDuty);
		pin_write(redPin, redDuty);
		pin_write(bluePin, blueDuty);


		/*
		// fade on when touched
		if (touching0 == 10){
			if (greenOn == 0){
				greenChange = 1;
			}
			if (greenOn == 1){
				greenChange = -1;
			}
			redChange = -1;
			blueChange = -1;
		}
		if (touching1 == 11){
			if (redOn == 1){
				redChange = -1;
			}
			if (redOn == 0){
				redChange = 1;
			}
			blueChange = -1;
			greenChange = -1;
		}
		if (touching2 == 12){
			if (blueOn == 1){
				blueChange = -1;
			}
			if (blueOn == 0){
				blueChange = 1;
			}
			greenChange = -1;
			redChange = -1;
		}
		greenDuty = greenDuty + greenChange;
		if (greenDuty == MAX_INT){
			greenDuty = MAX_INT -1;
			greenOn = 1;
			greenChange = 0;
		}
		if (greenDuty == 0){
			greenDuty = 1;
			greenOn = 0;
			greenChange = 0;
		}
		redDuty = redDuty + redChange;
		if (redDuty == MAX_INT){
			redDuty = MAX_INT - 1;
			redOn = 1;
			redChange = 0;
		}
		if (redDuty == 0){
			redDuty = 1;
			redOn = 0;
			redChange = 0;
		}
		blueDuty = blueDuty + blueChange;
		if (blueDuty == MAX_INT){
			blueDuty = MAX_INT - 1;
			blueOn = 1;
			blueChange = 0;
		}
		if (blueDuty == 0){
			blueDuty = 1;
			blueOn = 0;
			blueChange = 0;
		}
		pin_write(greenPin, greenDuty);
		pin_write(redPin, redDuty);
		pin_write(bluePin, blueDuty);
		*/

		/*
		if (iteration > 10000) {
			ping();
			iteration = 0;
		}
		iteration += 1;
		*/
	}
}