void inputGetAndSend(void){ static bool penDown = false; static int sleepCounter = 0; touchPosition tempPos = {0}; FifoMessage msg = {0}; u16 keys= REG_KEYXY; msg.SystemInput.keys = keys; if(keys & KEY_TOUCH) { penDown = false; } else { msg.SystemInput.keys |= KEY_TOUCH; if(penDown) { touchReadXY(&tempPos); if(tempPos.rawx && tempPos.rawy) { msg.SystemInput.keys &= ~KEY_TOUCH; msg.SystemInput.touch = tempPos; } else { penDown = false; } } else { penDown = true; } } if(keys & KEY_LID) sleepCounter++; else sleepCounter = 0; //sleep if lid has been closed for 20 frames if(sleepCounter >= 20) { systemSleep(); sleepCounter = 0; } msg.type = SYS_INPUT_MESSAGE; //set message type fifoSendDatamsg(FIFO_SYSTEM, sizeof(msg), (u8*)&msg); }
void _powerController::sleep(){ systemSleep(); }
void loop() { #ifdef DEBUG Serial.println(); time_t utc = now(); printTime(utc, "UTC"); time_t local = myTZ.toLocal(utc, &tcr); printTime(local, tcr->abbrev); #endif systemSleep(); // Send the unit to sleep // Manual command to turn light on if (!digitalRead(CMD_MAN_IN)) { if (!first) { // Used only once to set the time with external input // setTime(20, 25, 00, 30, 10, 2016); // Set system clock in UTC!! #ifdef DEBUG Serial.println(getSystemTime()); #endif time_t systemTime = getSystemTime(); RTC.set(systemTime); tmDriftInfo diUpdate = RTC.read_DriftInfo(); // Update DriftInfo in RTC diUpdate.DriftStart = systemTime; RTC.write_DriftInfo(diUpdate); setDriftInfo(diUpdate); first = true; cleanLuxArray(); relayState = relayStateMem = true; digitalWrite(RELAY_SW_OUT, relayState); } } else { // When first == true the first cycle in automatic mode is executed, // after manual mode if (wdCount >= wdMatch || first) { first = false; wdCount = 0; // Conversion from UTC (system clock) to local time time_t utc = now(); time_t local = myTZ.toLocal(utc, &tcr); if (checkEnable(local)) { wdMatch = (uint16_t) WD_WAIT_EN_TIME / WD_TICK_TIME; // One time mode: the sensor reads and goes into sleep mode autonomously BH1750.wakeUp(BH1750_ONE_TIME_HIGH_RES_MODE_2); // Wait for the sensor to be fully awake. // Less than 500ms is not enough when the program doesn't write on the // serial output delay(500); float lux = sample(); relayState = checkLightCond(lux); #ifdef DEBUG printTime(local, tcr->abbrev); printLuxArray(); Serial.print("= "); Serial.print(lux); Serial.print(" "); if (relayState) Serial.println("ON"); else Serial.println("OFF"); #endif } else { // Set longer period to check the current time. // This requires less reads on the RTC when the light management is not // enabled. The accuracy of time when the system is enabled depends on // the watchdog tick (8 s is the longest) and on the wait time. // For this application the accuracy of one second is not needed: // - the "enable" event could be late up to WD_WAIT_DIS_TIME seconds. // - the "disable" event could be late up to WD_WAIT_EN_TIME seconds. wdMatch = (uint16_t) WD_WAIT_DIS_TIME / WD_TICK_TIME; cleanLuxArray(); relayState = false; } // Relay output update if (relayState != relayStateMem) { digitalWrite(RELAY_SW_OUT, relayState); relayStateMem = relayState; } } } }