Question: /ghc.c/ #include #include #include #include ghcontrol.h int main(void) { time_t now; struct controls ctrl = { 0 }; struct setpoints sets = { 0 };

 /ghc.c/ #include #include #include #include "ghcontrol.h" int main(void) { time_t now;struct controls ctrl = { 0 }; struct setpoints sets = {0 }; struct readings creadings = { 0 }; sets = GhSetTargets();

/ghc.c/

#include

#include

#include

#include "ghcontrol.h"

int main(void) {

time_t now;

struct controls ctrl = { 0 };

struct setpoints sets = { 0 };

struct readings creadings = { 0 };

sets = GhSetTargets();

GhControllerInit();

while (1) {

now = time(NULL);

creadings = GhGetReadings();

GhDisplayReadings(creadings);

GhDisplayTargets();

ctrl = GhSetControls(sets,creadings);

GhDisplayControls(ctrl);

GhDelay(GHUPDATE);

}

return EXIT_FAILURE;

}

/** @brief Gh control constants, structures, function prototypes

* @file ghcontrol.h

*/

// Constants

#ifndef GHCONTROL_H

#define GHCONTROL_H

#define SEARCHSTR "serial\t\t"

#define SYSINFOBUFSZ 512

#define GHUPDATE 2000

#define SENSORS 3

#define TEMPERATURE 0

#define HUMIDITY 1

#define PRESSURE 2

#define SIMULATE 1

#define USTEMP 50

#define LSTEMP -10

#define USHUMID 100

#define LSHUMID 0

#define USPRESS 1016

#define LSPRESS 975

#define STEMP 25.0

#define SHUMID 55.0

#define ON 1

#define OFF 0

#define SIMTEMPERATURE 1

#define SIMHUMIDITY 1

#define SIMPRESSURE 1

// Includes

#include

#include

#include

#include

#include

#include

#include

// Function Prototypes

int GhGetRandom(int range);

uint64_t GhGetSerial(void);

void GhDisplayHeader(const char *sname);

void GhDelay(int milliseconds);

double GhGetHumidity(void);

double GhGetPressure(void);

double GhGetTemperature(void);

void GhControllerInit(void);

void GhDisplayReadings(time_t rtime, double dreads[SENSORS]);

void GhDisplayTargets(void);

void GhDisplayControls(struct controls ctrl);

struct setpoints GhSetTargets(void);

struct controls GhSetControls(struct setpoints target,struct readings rdata);

struct readings GhGetReadings(void);

#endif

/** ghcontrol.c */

#include "ghcontrol.h"

#include

#include

#include

#include

#include

void GhControllerInit(void)

{

srand((unsigned)time (NULL));

GhSetTargets();

GhDisplayHeader("Name");

}

void GhDisplayHeader(const char *sname) {

fprintf(stdout, "%s's Greenhouse Controller ", sname);

}

void GhDisplayReadings(struct readings rdata)

{

printf(" %sReadings\tT: %4.1lfC\tH: %4.1lf%%\tP: %6.1lfmB",

ctime(&rdata.rtime),rdata.temperature,rdata.humidity,

rdata.pressure);

}

void GhSetControls(int *heater,int *humidifier,struct readings rdata)

{

if(rdata.temperature

else{*heater = OFF;}

if(rdata.humidity

else {*humidifier = OFF;}

}

void GhSetTargets(void) {}

void GhDisplayTargets(void) {

fprintf(stdout, "Setpoints\tT: %5.1lfC\tH: %5.1lf%% ", STEMP, SHUMID);

}

void GhDisplayControls(struct controls ctrl)

{

printf(" Controls\tHeater: %1d\tHumidifier: %1d\tLights: %3d ", ctrl.heater, ctrl.humidifier, ctrl.light);

}

void GhDelay(int milliseconds) {

long wait;

clock_t now, start;

wait = milliseconds * (CLOCKS_PER_SEC / 1000);

start = clock();

now = start;

while ((now - start)

now = clock();

}

}

uint64_t GhGetSerial(void) {

static uint64_t serial = 0;

FILE *fp;

char buf[SYSINFOBUFSZ];

char searchstring[] = SEARCHSTR;

fp = fopen("/proc/cpuinfo", "r");

if (fp != NULL) {

while (fgets(buf, sizeof(buf), fp) != NULL) {

if (!strncasecmp(searchstring, buf, strlen(searchstring))) {

sscanf(buf + strlen(searchstring), "%lu", &serial);

}

}

fclose(fp);

}

if (serial == 0) {

system("uname -a");

system("ls --fu /usr/lib/codeblocks | grep -Po '\\.\\K[^ ]+'>stamp.txt");

fp = fopen("stamp.txt", "r");

if (fp != NULL) {

while (fgets(buf, sizeof(buf), fp) != NULL) {

sscanf(buf, "%lu", &serial);

}

fclose(fp);

}

}

return serial;

}

void GhGetReadings(double readings[SENSORS])

{

readings[HUMIDITY] = GhGetHumidity();

readings[TEMPERATURE] = GhGetTemperature();

readings[PRESSURE] = GhGetPressure();

}

struct readings GhGetReadings(void)

{

struct readings now = {0};

now.rtime = time(NULL);

now.temperature = GhGetTemperature();

now.humidity = GhGetHumidity();

now.pressure = GhGetPressure();

now.light = GhGetLight();

return now;

}

struct controls GhSetControls(struct setpoints target,struct readings rdata)

{

struct controls cset = { 0 };

if(rdata.temperature

{

cset.heater = 1;

digitalWrite(HSH_MCP23017_PINBASE+HEATERON, 1);

}

else

{

cset.heater = 0;

digitalWrite(HSH_MCP23017_PINBASE+HEATERON, 0);

}

if(rdata.humidity

{

cset.humidifier = 1;

digitalWrite(HSH_MCP23017_PINBASE+HUMIDIFIERON, 1);

}

else

{

cset.humidifier = 0;

digitalWrite(HSH_MCP23017_PINBASE+HUMIDIFIERON, 0);

}

return cset;

}

struct setpoints GhSetSetpoints(void)

{

struct setpoints cpoints = { 0 };

cpoints.temperature = CTEMP;

cpoints.humidity = CHUMID;

return cpoints;

}

double GhGetHumidity(void) {

#if SIMHUMIDITY

return GetRandom(UPPERAHUMID-LOWERAHUMID)+LOWERAHUMID;

#else

return 25.0;

#endif

}

double GhGetPressure(void) {

#if SIMPRESSURE

return GetRandom(UPPERAPRESS-LOWERAPRESS)+LOWERAPRESS;

#else

return 25.0;

#endif

}

double GhGetTemperature(void) {

#if SIMTEMPERATURE

return GetRandom(UPPERATEMP-LOWERATEMP)+LOWERATEMP;

#else

return 25.0;

#endif

}

int GhGetRandom(int range) {

return rand() % range;

}

This how far i got with my code but i can't find the problem anything will be helpfully thanks

Task 1 -Create/Use the readings Structure First add readings to control.h in a section called Structures, located before the Function Prototypes section. Our process for passing changes with the readings structure, as we can return a collection of data values rather than use address references. The new GhGetReadings function prototype is: struct readings GhGetReadings(void); We access the various parts of a structure by combining the name of the specific instance of a structure followed by a period and then the general name giving to a part of the structure. For instance in GhGet Readings we will first declare or set up an object named now of the structure readings and set all of the parts of this object to 0. Then modify GhGetReadings to use now as follows: struct readings GhGetReadings (void) { struct readings now = {0}; now.rtime = time(NULL); now. temperature - GhGetTemperature(); now.humidity = GhGetHumidity now.pressure - GhGetPressure(); return now; } Next we use this new form of passing data in our controller functions GhDisplayReadings and GhSetControls: void GhDisplayReadings (struct readings rdata) { printf(" KsReadings\tT: %4.11fC\tH: %4.11f%X\tP: %6.11fmB", ctime(&rdata.rime),rdata.temperature,rdata.humidity, rdata.pressure) } void GhSetControls(int *heater, int *humidifier, struct readings rdata) { if(rdata.temperature

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!