SEDO subject project

Dependencies:   ds3231 mbed-rtos mbed DHT

main.cpp

Committer:
ValenSalLop
Date:
2017-04-26
Revision:
6:dff022cf4d99
Parent:
5:19ccff89f555
Parent:
2:1d79c4e47430
Child:
7:8065e844e99a

File content as of revision 6:dff022cf4d99:

#include "mbed.h"
#include "rtos.h"
#include "ds3231.h"
#include "DHT.h"

#define ESC 0x1B

#define I2C_SDA D14
#define I2C_SCL D15
#define DHT_PIN D2
#define LCD_PIN1 D8
#define LCD_PIN2 D13
#define LCD_PIN3 D9
#define LCD_PIN4 D4
#define LCD_PIN5 D5
#define LCD_PIN6 D6
#define LCD_PIN7 D7
#define M_DIR_PIN D4
#define M_SPE_PIN D5

void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member);
void get_user_input(char* message, uint8_t min, uint8_t max, bool* member);
void setupRTC(Ds3231 rtc, time_t* epoch_time, ds3231_cntl_stat_t rtc_control_status, ds3231_time_t rtc_time, ds3231_calendar_t rtc_calendar);


Ds3231 rtc(D14, D15);

PwmOut m_speed(M_SPE_PIN);
DigitalOut m_direction(M_DIR_PIN);


DHT sensor(D2, DHT22);

int main(void)
{
    //rtc variables
    time_t epoch_time;
    //default, use bit masks in ds3231.h for desired operation
    ds3231_cntl_stat_t rtc_control_status = {0,0};
    ds3231_time_t rtc_time;
    ds3231_calendar_t rtc_calendar;

    //setupRTC(rtc, &epoch_time, rtc_control_status, rtc_time, rtc_calendar);

    char buffer[32];
    for(;;) {
        printf("%c[2J", ESC); //clear screen
        printf("%c[H", ESC); //move cursor to Home

        //new epoch time fx
        epoch_time = rtc.get_epoch();

        printf("\nTime as seconds since January 1, 1970 = %d\n", epoch_time);

        printf("\nTime as a basic string = %s", ctime(&epoch_time));

        strftime(buffer, 32, "%I:%M %p\n", localtime(&epoch_time));
        printf("\nTime as a custom formatted string = %s", buffer);

        while(m_speed < 1.0) {
            m_speed = m_speed + 0.01;
            wait(0.1);
            if(m_speed >= 1.0) {
            }
        }
        m_speed = 0;
        m_direction = !m_direction;


        int error = 0;
        float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;

        error = sensor.readData();
        if (0 == error) {
            c   = sensor.ReadTemperature(CELCIUS);
            f   = sensor.ReadTemperature(FARENHEIT);
            k   = sensor.ReadTemperature(KELVIN);
            h   = sensor.ReadHumidity();
            dp  = sensor.CalcdewPoint(c, h);
            dpf = sensor.CalcdewPointFast(c, h);
            printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f);
            printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n", h, dp, dpf);
        } else {
            printf("Error: %d\n", error);
        }
        wait(5.0);
    }//loop
}


void setupRTC(Ds3231 rtc, time_t* epoch_time, ds3231_cntl_stat_t rtc_control_status, ds3231_time_t rtc_time, ds3231_calendar_t rtc_calendar)
{

    rtc.set_cntl_stat_reg(rtc_control_status);

    //get day from user
    get_user_input("\nPlease enter day of week, 1 for Sunday (1-7): ", 1,
                   7, &rtc_calendar.day);

    //get day of month from user
    get_user_input("\nPlease enter day of month (1-31): ", 1, 31,
                   &rtc_calendar.date);

    //get month from user
    get_user_input("\nPlease enter the month, 1 for January (1-12): ", 1,
                   12, &rtc_calendar.month);

    //get year from user
    get_user_input("\nPlease enter the year (0-99): ",0, 99,
                   &rtc_calendar.year);

    //Get time mode
    get_user_input("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 0, 1,
                   &rtc_time.mode);

    if(rtc_time.mode) {
        //Get AM/PM status
        get_user_input("\nIs it AM or PM? 0 for AM 1 for PM: ", 0, 1,
                       &rtc_time.am_pm);
        //Get hour from user
        get_user_input("\nPlease enter the hour (1-12): ", 1, 12,
                       &rtc_time.hours);
    } else {
        //Get hour from user
        get_user_input("\nPlease enter the hour (0-23): ", 0, 23,
                       &rtc_time.hours);
    }

    //Get minutes from user
    get_user_input("\nPlease enter the minute (0-59): ", 0, 59,
                   &rtc_time.minutes);


    //Get seconds from user
    get_user_input("\nPlease enter the second (0-59): ", 0, 59,
                   &rtc_time.seconds);



    //Set the time, uses inverted logic for return value
    if(rtc.set_time(rtc_time)) {
        printf("\nrtc.set_time failed!!\n");
        exit(0);
    }

    //Set the calendar, uses inverted logic for return value
    if(rtc.set_calendar(rtc_calendar)) {
        printf("\nrtc.set_calendar failed!!\n");
        exit(0);
    }


}

/**********************************************************************
* Function: get_user_input()
* Parameters: message - user prompt
*             min - minimum value of input
*             max - maximum value of input
*             member - pointer to struct member
* Returns: none
*
* Description: get time/date input from user
*
**********************************************************************/
void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member)
{
    uint32_t temp;

    do {
        printf("\n%s", message);

        //for some reason mbed doesn't like a pointer to a member in scanf
        //term.scanf("%d", member); works with gcc on RPi
        scanf("%d", &temp);

        *member = temp;

        if((*(member)< min) || (*(member) > max)) {
            printf("\nERROR-RTI");
        }
    } while((*(member) < min) || (*(member) > max));
}


void get_user_input(char* message, uint8_t min, uint8_t max, bool* member)
{
    uint32_t temp;

    do {
        printf("\n%s", message);

        //for some reason mbed doesn't like a pointer to a member in scanf
        //term.scanf("%d", member); works with gcc on RPi
        scanf("%d", &temp);

        *member = temp;

        if((*(member)< min) || (*(member) > max)) {
            printf("\nERROR-RTI");
        }
    } while((*(member) < min) || (*(member) > max));
}