Example #1
0
int main(void)
{
	setup();
	ds.set_time(&t);
	while(1)
	{	
		ds.get_date_time(&t);
		ds.get_time(time);
		ds.get_date(date);
		uart3.printf("=======\r\n");
		uart3.printf("%02d-%02d-%02d %02d:%02d:%02d\r\n",t.year,t.month,t.date,t.hour,t.min,t.sec);

		uart3.printf(date);
		uart3.printf(" ");
		uart3.printf(time);
		uart3.printf("\r\n");
		delay_ms(1000);
	}


}
Example #2
0
void setup()
{
	ebox_init();
	uart3.begin(9600);
	ds.begin(400000);
	
	t.year = 15;
	t.month = 7;
	t.date = 3;
	t.hour = 23;
	t.min = 59;
	t.sec = 55;
}
Example #3
0
void WeatherStation::setCurrentDate()
{
    DateTime nowDate;

    nowDate = RTCObj.now();

    currentYear = nowDate.year();
    currentMonth = nowDate.month();
    currentDate = nowDate.date();
    currentHour = nowDate.hour();
    currentMinute = nowDate.minute();
    currentSecond = nowDate.second();
}
Example #4
0
    void write_time(Usart& uart, DS3231 &rtc)
    {
        uint8_t buffer[8] = {0};
        rtc.readRaw(buffer, 8);

        write_digits(uart, buffer[3]);
        uart.write(':');
        write_digits(uart, buffer[2]);
        uart.write(':');
        write_digits(uart, buffer[1]);
        uart.write(' ');

        write_digits(uart, buffer[5]);
        uart.write('.');
        write_digits(uart, buffer[6]);
        uart.write(".20");
        write_digits(uart, buffer[7]);
    }
Example #5
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

}
Example #6
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 );


}