Esempio n. 1
0
//*****************************************************************************
//
// Main loop
//
//*****************************************************************************
int main(void) {

	// Initialize relevant GPIO pins and periodic timer
	InitClocksGPIOAndTimer();

	// Initialize the UART.
	ConfigureUART();

	UARTprintf("Started!\n");


	// Init to shorter period
	ulPeriod = (SysCtlClockGet() * LIGHTS_ON_PERIOD_SEC);
#ifdef RUN_AS_MASTER
	uint32_t ulIdleMinutesLimit = SHORT_IDLE_TIME_MINS;
	int lightSwitchState = 0;
#else
	bool ambienceOn = false;
	bool lightSwitchOn = false;
#endif

	// Set the blue led on at startup. Will shut down in first periodic timer trig.
	setStatusLedState(false);

	while (1) {
#ifdef RUN_AS_MASTER
		if (lightSwitchTrigged) {
			int i, realHit = 1;

			for (i = 0; i < 50; i++) {

				SysCtlDelay(SysCtlClockGet() / 1000);

				if (lightSwitchGPIOActive() == 0) {
					realHit = 0;
					break;
				}
			}
			if (realHit) {
				UARTprintf("Light switch hit\n");
				if (lightSwitchState == OFF) {
					lightSwitchState = ON;
					UARTprintf("Switching ON\n");
					SendIRCode(SWITCH_ON);
					rampBottomPWM(PWM_RAMP_UP);

					// Use longer idle period
					ulIdleMinutesLimit = LONG_IDLE_TIME_MINS;
					startIdleDetectionTimer(ulPeriod, true);
				} else {
					lightSwitchState = OFF;
					UARTprintf("Switching OFF\n");
					SendIRCode(SWITCH_OFF);
					rampBottomPWM(PWM_RAMP_DOWN);

					// Use shorter idle period
					ulIdleMinutesLimit = SHORT_IDLE_TIME_MINS;
					startIdleDetectionTimer(ulPeriod, true);
				}
			} else {
				UARTprintf("False hit\n");
			}
			lightSwitchTrigged = 0;

		}

		if (idleTimerTrigged) {
			// idle (no movement detected) timer trigged
			idleTimerTrigged = 0;
			ulIdleMinutes ++;
			if (ulIdleMinutes < ulIdleMinutesLimit)
			{
				startIdleDetectionTimer(ulPeriod, false);
				blink_n(1);
			}
			else
			{
				UARTprintf("Idle timer trigged, turning leds off\n");

				if (motionDetectorGPIOActive()) {
					// Level is still high - let's restart the timer.

					UARTprintf("Level still high, restarting timer\n");
					startIdleDetectionTimer(ulPeriod, true);
				} else {
					setStatusLedState(false);

					// Turn the PMW output off
					SendIRCode(AMBIENCE_OFF);
					rampTopPWM(PWM_RAMP_DOWN);
					if (lightSwitchState == ON)
					{
						SendIRCode(SWITCH_OFF);
						rampBottomPWM(PWM_RAMP_DOWN);
					}
				}
			}
		}

		if (motionDetectorTrigged) {
			// Movement detected

			motionDetectorTrigged = 0;
			UARTprintf("motionDetectorTrigged\n");

			// Send this in any case. Rx shall maintain its own state
			SendIRCode(AMBIENCE_ON);
			if (!idleTimerRunning()) {
				setStatusLedState(true);

				// Turn the PMW output on
				rampTopPWM(PWM_RAMP_UP);
				if (lightSwitchState == ON)
				{
					SendIRCode(SWITCH_ON);
					rampBottomPWM(PWM_RAMP_UP);
				}

				UARTprintf("Starting timer\n");
			} else {
				// Leds are on at this point - no need to set those on again
				UARTprintf("RE-Starting timer\n");
			}

			startIdleDetectionTimer(ulPeriod, true);
		}
		//if (timerTrigged)

		{
			// signal the state using on-board led and UART printouts

			bool gpioActive = motionDetectorGPIOActive();
			if (gpioActive) {
				UARTprintf("Input active (motion detected)\n");
			}
			if (!idleTimerRunning()) {
				UARTprintf("Timer not running\n");
			} else {
				UARTprintf("Timer running for %d\n",
						idleTimerRemainingSecondsGet());
				if (gpioActive) {
					setStatusLedState(true);
				} else {
					setStatusLedState(false);
				}
			}
		}
#else
		UARTprintf("SLAVE LOOP: %d\n", ir_pulse_count);
		if (receivedIRCode != 0) {
			uint32_t _code = receivedIRCode;
			receivedIRCode = 0;
			switch (_code) {
			case AMBIENCE_ON:
				if (!ambienceOn)
				{
					rampTopPWM(PWM_RAMP_UP);
					ambienceOn = true;
				}
				break;

			case AMBIENCE_OFF:
				if (ambienceOn)
				{
					rampTopPWM(PWM_RAMP_DOWN);
					ambienceOn = false;
				}
				break;

			case SWITCH_ON:
				if (!lightSwitchOn)
				{
					rampBottomPWM(PWM_RAMP_UP);
					lightSwitchOn = true;
				}
				break;

			case SWITCH_OFF:
				if (lightSwitchOn)
				{
					rampBottomPWM(PWM_RAMP_DOWN);
					lightSwitchOn = false;
				}
				break;

			default:
				break;
			}

			// Blink out the code
			if (_code < 10)
			{
				// blink_n(_code);
			}
		}
#endif
		//
		// Sleep until next interrupt
		//
		SysCtlSleep();
	}
}
void loop(void)
{	
	// Read the state of the "Send" push-button value
	buttonState1 = digitalRead(buttonPin1);
	// Read the state of the "Receive" push-button value
	buttonState2 = digitalRead(buttonPin2);

	if (buttonState1 == HIGH) {
		  
		std::cout << "Sending IR signal" << std::endl;
		digitalWrite(orangeLedPin, HIGH); 
		delay(200);
		digitalWrite(orangeLedPin, LOW); 
		delay(200);
		
		SendIRCode();
		delay(15);  // wait 15 milliseconds before sending it again
		SendIRCode();  // repeat IR code if it is neccesary
	 
		//delay(3000);  // wait 3 seconds to resend the code
    }

	if ((buttonState2==HIGH) || (currentpulse != 0)){

	if (buttonState2==HIGH){
		std::cout << "Ready to decode IR!" << std::endl;
		digitalWrite(blueLedPin, HIGH); 
		delay(200);
		digitalWrite(blueLedPin, LOW); 
		delay(200);
	}

	uint16_t highpulse, lowpulse; // Temporary storage timing
	highpulse = lowpulse = 0; // Start out with no pulse length

	while (digitalRead(IRpin)) {
		// count off another few microseconds
		highpulse++;
		delayMicroseconds(RESOLUTION);

		// If the pulse is too long, we 'timed out' - either nothing
		// was received or the code is finished, so print what
		// we've grabbed so far, and then reset
		if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
			printpulses();
			sendpulse=currentpulse;
			currentpulse=0;
			return;
		}
	}
	// we didn't time out so lets stash the reading
	pulses[currentpulse][0] = highpulse;

	// same as above
	while (! digitalRead(IRpin)) {
		// pin is still LOW
		lowpulse++;
		delayMicroseconds(RESOLUTION);
		if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
			printpulses();
			sendpulse=currentpulse;
			currentpulse=0;
			return;
		}
	}
	pulses[currentpulse][1] = lowpulse;

	// we read one high-low pulse successfully, continue!
	currentpulse++;

	}
}