Пример #1
0
void WeatherStation::run()
{
    uint8_t dataType;
    int16_t data;
    boolean ret;

    /*event*/

    if (g_RTCIntFlag)  //it must be at the front of      processRTCAlarm()
    {
        g_RTCIntFlag = false;
        RTCObj.clearINTStatus();
    }

    if ( EVENT_STATE_SET == eventRTCState )
    {
        processRTCAlarm();
        eventRTCState = EVENT_STATE_DOING;
    }

    if ( EVENT_STATE_SET == eventButtonState )
    {
        processButton();
        eventButtonState = EVENT_STATE_DOING;
    }


    if ( SYS_STATE_WAITING == systemState )
    {
        if ( STATE_DOING == getActionState())  //high priority
        {
            systemState = SYS_STATE_ACTION;
        }
        else if ( (uint8_t)((millis()/1000) - startTime) >= overTime )
        {
            systemState = SYS_STATE_GOTOSLEEP;

            DBG_PRINTLN("goto sleep");

            /**to minimize the time to sleep state, so there is not sleep state case **/
            if ( (SW_RUN == digitalRead(PALETTE_PIN_SW)) && (SW_MAKE == switchState) )
            {
#if !_DEBUG
                dettachUSB();
#endif
                switchState = SW_RUN;
            }


            disableBeforeSleep();

            wakeFlag = false;

            sleep();

            enableAfterSleep();
            systemState = SYS_STATE_WAITING;

            DBG_PRINTLN_VAR(systemState,DEC);

        }
        else
        {
            //nothing
        }
    }
    else if ( SYS_STATE_ACTION == systemState )
    {

        /*action*/

        if ( STATE_DOING == a_actionList[ACTION_SAMPLE_DATA].state )
        {
            DBG_PRINTLN("**Sample");
            ret = sensorMgt.run();

            if ( ret )
            {
                a_actionList[ACTION_SAMPLE_DATA].state = STATE_DONE;
            }
            else
            {
                delay(10);
            }
        }

        if ( STATE_DOING == a_actionList[ACTION_SAVE_DATA].state )
        {
            if ( STATE_DONE == a_actionList[ACTION_SAMPLE_DATA].state )
            {
                DBG_PRINTLN("**Save");
                for ( uint8_t i = 0; i < DATA_TYPE_MAXNUM; i++ )
                {
                    if ( false == a_measureData[i].valid )
                    {
                        break;
                    }

                    dataType = a_measureData[i].dataType;
                    data = a_measureData[i].p_sensor->getValue( dataType );

                    dataHouse.putData( data, dataType, currentHour );
                }

                a_actionList[ACTION_SAVE_DATA].state = STATE_DONE;

            }

        }

        if ( STATE_DOING == a_actionList[ACTION_DISPLAY].state )
        {
            if (STATE_DONE == a_actionList[ACTION_SAMPLE_DATA].state)
            {
                displayMgt.state = DISPLAY_STATE_NEXT;
            }

            if ( DISPLAY_STATE_NEXT == displayMgt.state )
            {
                DBG_PRINTLN("**Display");

                displayNext();

                a_actionList[ACTION_DISPLAY].state = STATE_DONE;
            }

        }


        if ( STATE_DONE == getActionState() )
        {
            startTime = millis()/1000;

            if( (END_HOURTIME_ONEDAY == currentHour) && (EVENT_STATE_DOING == eventRTCState) )
            {   //todo:  muti thread
                DBG_PRINTLN_VAR(currentHour,DEC);
                dataHouse.updateDate( currentYear, currentMonth, currentDate );
            }

            eventRTCState = EVENT_STATE_DONE;
            eventButtonState = EVENT_STATE_DONE;

            for ( uint8_t i = 0; i < ACTION_NUMBER; i++ )  //todo
            {
                a_actionList[i].state = STATE_INIT;
            }

            systemState = SYS_STATE_WAITING;

            DBG_PRINTLN_VAR(systemState,DEC);

        }

    }



#if 0
DateTime now = RTCObj.now();
Serial.println(now.minute(), DEC);
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.date(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println(' ');
Serial.println(RTCObj.getTemperature());


#endif

}
Пример #2
0
void WeatherStation::init( DataConfig *p_dataConfig, uint8_t dataNum )
{
    uint8_t i, index;
    DataConfig *p_dataConfigRecord = NULL;

    /**init config table**/
    p_dataConfigRecord = p_dataConfig;
    index = 0;

    DBG_PRINTLN("measure table:");

    for( i = 0; i < dataNum; i++ )
    {
        a_measureData[index].p_sensor = sensorMgt.creatObject( p_dataConfigRecord->sensorID, p_dataConfigRecord->attachPin, p_dataConfigRecord->auxiliaryPin );
        if ( NULL == a_measureData[index].p_sensor )
        {
            String info;

            info = "Error: Rule No.";
            info += (i+1);

            showErrInfo( info );

            break;
        }

        a_measureData[index].dataType = p_dataConfigRecord->type;
        a_measureData[index].valid = true;

        dataHouse.addDataTypeIndex(a_measureData[index].dataType);

        DBG_PRINTLN_VAR(index, DEC);
        DBG_PRINTLN_VAR((word)a_measureData[index].p_sensor, HEX);

        index++;
        p_dataConfigRecord++;


    }

    measureNum = index;

    //todo enable power
    digitalWrite( PALETTE_PIN_MVCC, LOW );
    pinMode( PALETTE_PIN_MVCC, OUTPUT );
    delay(500);  //waiting power stable

    /**init devices**/
	Wire.begin();
	RTCObj.begin();
    lcd.init();
    lcd.backlight();

    setCurrentDate();
    dataHouse.init( currentYear, currentMonth, currentDate, currentHour );

    sensorMgt.start();

    /**get the state of Switch**/
    pinMode(PALETTE_PIN_SW, INPUT_PULLUP);
    switchState = digitalRead(PALETTE_PIN_SW);

    /**attach interrupt**/
    pinMode(STAION_PIN_RTC_INT, INPUT_PULLUP);
    pinMode(STAION_PIN_BUTTON, INPUT );

    PCattachInterrupt( STAION_PIN_RTC_INT );
    PCattachInterrupt( STAION_PIN_BUTTON );

    RTCObj.clearINTStatus();

#if _DEBUG
	RTCObj.enableInterrupts(EveryMinute);
#else
	RTCObj.enableInterrupts(EveryHour);
#endif

    /**Start**/
    pinMode(PALETTE_PIN_BLK, OUTPUT);
    digitalWrite(PALETTE_PIN_BLK,HIGH);

    startTime = (uint8_t)(millis()/1000);

    /**emulate pushing button**/
    setEvent( STAION_PIN_BUTTON );


}