void lcdSetup() {
    Semaphore_Params mySemParams;
    Clock_Params myClkParams;

    /* Use pull up on button inputs to save power */  
    pinMode(PUSH1, INPUT_PULLUP);
    pinMode(PUSH2, INPUT_PULLUP);

    /* Both buttons post the semaphore */
    attachInterrupt(PUSH1, button1Interrupt, FALLING);
    attachInterrupt(PUSH2, button2Interrupt, FALLING);

    /* set LCD's EXTMODE low (sadly this is the UART Rx pin too) */
    digitalWrite(12, 0);

    /* turn off MPU power */
    digitalWrite(22, 0);

    /* put flash into power down mode */
    SPI.begin();
    SPI.transfer((uint8_t)32, 0xb9);
    SPI.end();

    /* power down the tmp007 */
    Wire.begin();
    tmp007.begin(TMP007_CFG_1SAMPLE);
    tmp007.write16(TMP007_CONFIG, TMP007_CFG_ALERTEN | TMP007_CFG_TRANSC | TMP007_CFG_1SAMPLE);
    Wire.end();
  
    Semaphore_Params_init(&mySemParams);

    mySem = Semaphore_create(0, &mySemParams, NULL);
  
    Clock_Params_init(&myClkParams);

    myClkParams.period = 100000; /* one second */
    myClkParams.startFlag = true;

    /* comment out for steady state power testing */
    Clock_create(myClkFunc, 0, &myClkParams, NULL); 
  
    /* set to 1970 plus 45 years */
    Seconds_set(secondsOffset);
  
    myScreen.begin(); /* also calls SPI.begin() */

    myScreen.setFont(1);
    myScreen.text(10, 10, "Hello!");
    myScreen.flush(); 

    SPI.end(); /* to save power */

    /* Say Hello for 1 second */
    delay(1000);

    SPI.begin();
    myScreen.clear();
    SPI.end(); /* to save Power */
}
void lcdLoop() {

    Semaphore_pend(mySem, BIOS_WAIT_FOREVER);

    /* Advance quickly */
    while(digitalRead(PUSH2) == 0) {
        secondsOffset += 100;
        Seconds_set(secondsOffset);
        printTime();
    }

    /* reverse more slowly */
    while(digitalRead(PUSH1) == 0) {
        secondsOffset -= 10;
        Seconds_set(secondsOffset);
        printTime();
    }

    printTime();
}
Example #3
0
//*****************************************************************************
//
//! Main application function.
//!
//! This function is meant to initialize all devices and tasks used in this
//! application. After the initialization is complete, the tasks start running.
//!
//! \return None.
//
//*****************************************************************************
int main(void)
{
    // Initialize General configurations
    Board_initGeneral();

    // Initialize GPIO
    Board_initGPIO();

    // Turn off all LEDS
    GPIO_write(Board_LED0, Board_LED_OFF);
    GPIO_write(Board_LED1, Board_LED_OFF);
    GPIO_write(Board_LED2, Board_LED_OFF);

	// Initializes interrupts
    GPIO_setCallback(Board_BUTTON0, _callback_Button_Select);
    GPIO_setCallback(Board_BUTTON1, _callback_Button_Up);
    GPIO_setCallback(Board_BUTTON2, _callback_Button_Down);
    GPIO_setCallback(Board_SENSE_RISER_DOWN, _callback_Festo_Riser_Down);
    GPIO_setCallback(Board_SENSE_RISER_UP, _callback_Festo_Riser_Up);
    GPIO_setCallback(Board_SENSE_SAMPLE_IN_PLACE, _callback_Festo_Piece_In);

    // Enable Interrupts
    GPIO_enableInt(Board_BUTTON0);
    GPIO_enableInt(Board_BUTTON1);
    GPIO_enableInt(Board_BUTTON2);
    GPIO_enableInt(Board_SENSE_RISER_DOWN);
    GPIO_enableInt(Board_SENSE_RISER_UP);
    GPIO_enableInt(Board_SENSE_SAMPLE_IN_PLACE);

    Seconds_set(1432639800);

    // Start BIOS
    BIOS_start();

    return (0);
}
Example #4
0
//*****************************************************************************
//
//! Festo Station Task.
//!
//! This task function do all the work related to controlling the Festo
//! Station. The task will first initialize the Driver and then run a infinite
//! loop, waiting and processing external events.
//!
//! \return None.
//
//*****************************************************************************
Void _task_FESTO(UArg arg0, UArg arg1)
{
	// initialize driver
	FestoStationDriver DriverInstance;
	FestoStationDriver* Driver = &DriverInstance;

    Festo_Driver_Init(Driver);

	uint32_t EventPosted;
	DisplayMessage MessageObject;

	MessageObject.ScreenID = 0;
	MessageObject.uptimeSeconds = 0;
	MessageObject.piecesProcessed = 0;
	MessageObject.blackAccepted = 0;
	MessageObject.blackRejected = 0;
	MessageObject.plasticAccepted = 0;
	MessageObject.plasticRejected = 0;
	MessageObject.orangeAccepted = 0;
	MessageObject.orangeRejected = 0;
	MessageObject.piecesProcessedPerSecond = 0;
	MessageObject.heightCalibrated = 230;
	MessageObject.upperHeightCalibrated = 245;
	MessageObject.lowerHeightCalibrated = 227;

	time_t t1 = time(NULL);
	strcpy((char*) MessageObject.timeString, asctime(localtime(&t1)));


	uint32_t uptimeSeconds = 0;
	uint32_t piecesProcessed = 0;
	uint32_t blackAccepted = 0;
	uint32_t blackRejected = 0;
	uint32_t plasticAccepted = 0;
	uint32_t plasticRejected = 0;
	uint32_t orangeAccepted = 0;
	uint32_t orangeRejected = 0;
	uint32_t metallicAccepted = 0;
	uint32_t metallicRejected = 0;
	uint32_t piecesProcessedPerSecond = 0;


	uint32_t *ColourAccepted;
	uint32_t *ColourRejected;
	uint32_t *MaterialAccepted;
	uint32_t *MaterialRejected;

	uint8_t colour = 0;
	uint8_t material = 0;

	uint32_t i = 0;

	uint32_t FestoState = 0;
	// 0 = stopped
	// 1 = idle
	// 2 = initial state
	// 10 = cal

	int32_t LowerLimit = 1200;
	int32_t UpperLimit = 1500;

	int32_t Uptime = 0;
	int32_t Time0 = 0;
	int32_t Time1 = 0;
	uint8_t Running = 0;

	Clock_Params clockParams;
	Clock_Handle myClock;
	Clock_Params_init(&clockParams);
	clockParams.arg = (UArg) Board_ACTUATOR_EJECTOR;
	myClock = Clock_create(_Festo_Deactivate_Ejector, 200, &clockParams, NULL);

	uint32_t heightMeasured = 0;
	uint32_t heightCalibratedADC = 1380;
	uint32_t heightCalibrated10mm = 225;

	//float ConvertFactor = 0.1*MessageObject.heightCalibrated/1200;

    GPIO_write(Board_LED0, Board_LED_OFF);
    GPIO_write(Board_LED1, Board_LED_OFF);
    GPIO_write(Board_LED2, Board_LED_ON);

    Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);

	Clock_start(Clock_1_sec);

	while(1)
	{
		EventPosted = Event_pend(FestoEvents,
						Event_Id_NONE,
						FESTO_EVENT_BUTTON_UP + FESTO_EVENT_BUTTON_DOWN +
						FESTO_EVENT_BUTTON_SELECT + FESTO_EVENT_RISER_DOWN +
						FESTO_EVENT_RISER_UP + FESTO_EVENT_ADC_FINISH +
						FESTO_EVENT_PIECE_IN_PLACE + FESTO_EVENT_EJECTOR_FINISHED +
						FESTO_EVENT_TICK + FESTO_EVENT_COOLDOWN +
						FESTO_EVENT_PIECE_NOT_IN_PLACE,
						FESTO_TIMEOUT);

		if (EventPosted & FESTO_EVENT_BUTTON_UP)
		{
			if (FestoState == 0)
			{
				Festo_Control_Driver(Driver, FESTO_ENABLED);
				FestoState = 1;
				Running = 1;
				GPIO_write(Board_LED0, Board_LED_ON);
				GPIO_write(Board_LED1, Board_LED_OFF);
				GPIO_write(Board_LED2, Board_LED_OFF);
				MessageObject.ScreenID = 1;
				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
				Time0 = Clock_getTicks();
			}
			else if (FestoState == 12)
			{
				MessageObject.heightCalibrated++;
				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
			}
			else if (FestoState == 13)
			{
				MessageObject.upperHeightCalibrated++;
				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
			}
			else if (FestoState == 14)
			{
				MessageObject.lowerHeightCalibrated++;
				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
			}
		}
		else if (EventPosted & FESTO_EVENT_BUTTON_DOWN)
		{
			if (FestoState == 1)
			{
				Festo_Control_Ejector(Driver, FESTO_EJECTOR_RETRACT);
				Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER);
				Festo_Control_Driver(Driver, FESTO_DISABLED);
				FestoState = 0;
				Running = 0;
				GPIO_write(Board_LED0, Board_LED_OFF);
				GPIO_write(Board_LED1, Board_LED_OFF);
				GPIO_write(Board_LED2, Board_LED_ON);
				MessageObject.ScreenID = 0;
				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
			}
			else if (FestoState == 12)
			{
				MessageObject.heightCalibrated--;
				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
			}
			else if (FestoState == 13)
			{
				MessageObject.upperHeightCalibrated--;
				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
			}
			else if (FestoState == 14)
			{
				MessageObject.lowerHeightCalibrated--;
				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
			}
		}
		else if (EventPosted & FESTO_EVENT_BUTTON_SELECT)
		{
			if (FestoState == 0)
			{
				GPIO_write(Board_LED0, Board_LED_OFF);
				GPIO_write(Board_LED1, Board_LED_ON);
				GPIO_write(Board_LED2, Board_LED_OFF);

				Festo_Control_Driver(Driver, FESTO_ENABLED);
				FestoState = 10;

				MessageObject.ScreenID = 2;

				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);

				Festo_Control_Ejector(Driver, FESTO_EJECTOR_RETRACT);
				if (Festo_Sense_Riser_Down(Driver) != 1)
				{
					Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER);
				}
				else
				{
					FestoState = 11;
					MessageObject.ScreenID = 3;
					Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
				}
			}
			else if (FestoState == 11)
			{
				FestoState = 12;
				MessageObject.ScreenID = 4;
				MessageObject.heightCalibrated = 230;

				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
			}
			else if (FestoState == 12)
			{
				FestoState = 13;

				Festo_Control_Platform(Driver, FESTO_PLATFORM_RAISE);

				Event_post(FestoEvents, FESTO_EVENT_ADC_START);
			}
			else if (FestoState == 13)
			{
				FestoState = 14;
				MessageObject.ScreenID = 6;
				MessageObject.lowerHeightCalibrated = 227;
				UpperLimit = MessageObject.upperHeightCalibrated *
						(1.0 * heightCalibratedADC)/(1.0 * heightCalibrated10mm);

				System_printf("Upper Limit Cal: %d for %d *0.1 mm\n", UpperLimit, MessageObject.upperHeightCalibrated);
				System_flush();

				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
			}
			else if (FestoState == 14)
			{
				FestoState = 15;
				MessageObject.ScreenID = 7;

				LowerLimit = MessageObject.lowerHeightCalibrated *
						(1.0 * heightCalibratedADC)/(1.0 * heightCalibrated10mm);

				System_printf("Lower Limit Cal: %d for %d *0.1 mm\n", LowerLimit, MessageObject.lowerHeightCalibrated);
				System_flush();

				Festo_Control_Ejector(Driver, FESTO_EJECTOR_RETRACT);
				Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER);

				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
			}
			else if (FestoState == 15)
			{
				FestoState = 0;
				MessageObject.ScreenID = 0;

				GPIO_write(Board_LED0, Board_LED_OFF);
				GPIO_write(Board_LED1, Board_LED_OFF);
				GPIO_write(Board_LED2, Board_LED_ON);

				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
			}
			else
			{

			}
		}
		else if (EventPosted & FESTO_EVENT_RISER_UP)
		{
			if (FestoState == 3)
			{
				if (Festo_Sense_Piece_Placed(Driver) == 1)
				{
					FestoState = 4;
					// wait estabilize
					for (i = 0; i < 1000000; i++);
					Event_post(FestoEvents, FESTO_EVENT_ADC_START);
				}
				else
				{
					FestoState = 1;
					Festo_Control_Ejector(Driver, FESTO_EJECTOR_RETRACT);
					Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER);
				}
			}
			else if (FestoState == 13)
			{
				Event_post(FestoEvents, FESTO_EVENT_ADC_START);
			}
			else
			{

			}
		}
		else if (EventPosted & FESTO_EVENT_RISER_DOWN)
		{
			if (FestoState == 2)
			{
				if (Festo_Sense_Piece_Placed(Driver) == 1)
				{
					// get a lot of samples
					for (i = 0; i < 1200; i++)
					{
						// this waits to get a reliable reading. If the reading is different from before, reset.
						if (colour != Festo_Sense_Piece_Colour(Driver) ||
								material != Festo_Sense_Piece_Material(Driver))
						{
							i = 0;
						}
						colour = Festo_Sense_Piece_Colour(Driver);
						material = Festo_Sense_Piece_Material(Driver);
					}

					FestoState = 3;
					Festo_Control_Platform(Driver, FESTO_PLATFORM_RAISE);
				}
				else
				{
					FestoState = 1;
				}
			}
			if (FestoState == 5)
			{
				Festo_Control_Ejector(Driver, FESTO_EJECTOR_EXTEND);
				Clock_start(myClock);
			}
			if (FestoState == 6)
			{
				Festo_Control_Ejector(Driver, FESTO_EJECTOR_RETRACT);
				Clock_start(myClock);
				FestoState = 7;
			}
			if (FestoState == 10)
			{
				FestoState = 11;
				MessageObject.ScreenID = 3;
				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
			}
		}
		else if (EventPosted & FESTO_EVENT_ADC_FINISH)
		{
		    if (Mailbox_pend(ADCMailbox, &heightMeasured, BIOS_NO_WAIT))
		    {
		    	Festo_Sense_Set_Piece_Height(Driver, heightMeasured);
		    }
		    else
		    {
		    	Event_post(FestoEvents, FESTO_EVENT_ADC_START);
		    }
			if (FestoState == 4)
			{
				if (colour == FESTO_COLOR_ORANGE && material == FESTO_PIECE_OTHER)
				{
					ColourAccepted = &orangeAccepted;
					ColourRejected = &orangeRejected;
					MaterialAccepted = &plasticAccepted;
					MaterialRejected = &plasticRejected;
				}
				else if (colour == FESTO_COLOR_OTHER && material == FESTO_PIECE_OTHER)
				{
					ColourAccepted = &blackAccepted;
					ColourRejected = &blackRejected;
					MaterialAccepted = &plasticAccepted;
					MaterialRejected = &plasticRejected;
				}
				else if (material == FESTO_PIECE_METALLIC)
				{
					ColourAccepted = NULL;
					ColourRejected = NULL;
					MaterialAccepted = &metallicAccepted;
					MaterialRejected = &metallicRejected;
				}
				else
				{
					ColourAccepted = NULL;
					ColourRejected = NULL;
					MaterialAccepted = NULL;
					MaterialRejected = NULL;
				}

				if (heightMeasured < UpperLimit && heightMeasured > LowerLimit)//withn range
				{
					if (ColourAccepted != NULL)
					{
						(*ColourAccepted)++;
					}
					if (MaterialAccepted != NULL)
					{
						(*MaterialAccepted)++;
						piecesProcessed++;
					}
					FestoState = 6;
					System_printf("Piece is acceptable\n");
					System_flush();

					Festo_Control_Ejector(Driver, FESTO_EJECTOR_EXTEND);
					Clock_start(myClock);
				}
				else // out of range
				{
					if (ColourAccepted != NULL)
					{
						(*ColourRejected)++;
					}
					if (MaterialAccepted != NULL)
					{
						(*MaterialRejected)++;
						piecesProcessed++;
					}
					System_printf("Piece is NOT acceptable\n");
					System_flush();
					FestoState = 5;
					Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER);
				}
			}
			else if (FestoState == 13)
			{
				heightCalibratedADC = heightMeasured;
				heightCalibrated10mm = MessageObject.heightCalibrated;

				System_printf("ADC Cal: %d for %d *0.1 mm\n", heightCalibratedADC, heightCalibrated10mm);
				System_flush();

				MessageObject.ScreenID = 5;
				MessageObject.upperHeightCalibrated = 245;

				Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);
			}
		}
		else if (EventPosted & FESTO_EVENT_EJECTOR_FINISHED)
		{
			if (FestoState == 6)
			{
				Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER);
				Clock_start(myClock);
			}
			else if (FestoState == 7)
			{
				FestoState = 1;
			}
			else
			{
				Festo_Control_Ejector(Driver, FESTO_EJECTOR_RETRACT);
				Clock_start(myClock);
				FestoState = 7;
			}
		}
		else if (EventPosted & FESTO_EVENT_TICK)
		{
			if (Running)
			{
				Time1 = Clock_getTicks();
				Uptime += (Time1 - Time0);
				Time0 = Time1;
				uptimeSeconds = Uptime * 0.001;

				piecesProcessedPerSecond = 100 * piecesProcessed/(0.016667*uptimeSeconds);

				MessageObject.piecesProcessed = piecesProcessed;
				MessageObject.blackAccepted = blackAccepted;
				MessageObject.blackRejected = blackRejected;
				MessageObject.plasticAccepted = plasticAccepted;
				MessageObject.plasticRejected = plasticRejected;
				MessageObject.orangeAccepted = orangeAccepted;
				MessageObject.orangeRejected = orangeRejected;
				MessageObject.piecesProcessedPerSecond = piecesProcessedPerSecond;
				MessageObject.uptimeSeconds = uptimeSeconds;
				MessageObject.metalAccepted = metallicAccepted;
				MessageObject.metalRejected = metallicRejected;
			}

			Seconds_set(Seconds_get()+1);
			t1 = time(NULL);
			strcpy((char*) MessageObject.timeString, asctime(localtime(&t1)));
			Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT);

		}
		else if (EventPosted & FESTO_EVENT_PIECE_NOT_IN_PLACE)
		{
			if (FestoState <= 4)
			{
				FestoState = 1;
				Festo_Control_Ejector(Driver, FESTO_EJECTOR_RETRACT);
				Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER);
			}
		}
		else
		{
			if (FestoState == 1)
			{
				if (Festo_Sense_Piece_Placed(Driver) == 1)
				{
					FestoState = 2;
					if (Festo_Sense_Riser_Down(Driver) == 0)
					{
						Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER);
					}
					else
					{
					    Event_post(FestoEvents, FESTO_EVENT_RISER_DOWN);
					}
				}
			}
		}
		Task_sleep(100);
	}
}