1 void ActuatorHandler(FCGIContext *context, ActuatorId id, const char *set_value) {
4 switch(id) { //Add new actuators here
5 case ACT_PRESSURE: //Suppose is pressure regulator. 0-700 input (kPa)
7 //TBA, currently in a separate file
8 //pwm_convert_duty(value); - convert input to required duty cycle
9 //pwm_set_frequency(PWM_id, freq); - can be done during PWM setup, frequency won't change (50Hz?)
10 //pwm_set_duty_cycle(PWM_id, duty_cycle); - this is the low/high percentage, will vary with input
11 //may also need to set polarity?
12 //if pwm is not setup, run setup functions first
14 int value = strtol(set_value, &ptr, 10);
15 if (*ptr == '\0' && value >= 0 && value <= 700) {
16 FCGI_BeginJSON(context, STATUS_OK);
17 FCGI_JSONKey("description");
18 FCGI_JSONValue("\"Set pressure to %d kPa!\"", value);
21 FCGI_RejectJSONEx(context,
22 STATUS_ERROR, "Invalid pressure specified.");
26 //requires digital control
28 int value = strtol(set_value, &ptr, 10);
30 //code to set pin to value
31 //move this to separate function, will need to be repeated
34 if ((fd = open("/sys/class/gpio/gpio50/value", O_WRONLY)) < 0) //randomly chose pin 50 (each pin can be mapped to a certain switch case as required)
35 perror("Failed to open pin");
37 strncpy(buffer, "1", ARRAY_SIZE(buffer) - 1); //copy value to buffer
39 strncpy(buffer, "0", ARRAY_SIZE(buffer) - 1);
41 int write = write(fd, buffer, strlen(buffer)); //write buffer to pin
42 if (write < 0) perror("Failed to write to pin");
45 const char *state = "off";
48 FCGI_BeginJSON(context, STATUS_OK);
49 FCGI_JSONKey("description");
50 FCGI_JSONValue("\"Solenoid 1 turned %s!\"", state);
53 FCGI_RejectJSON(context, "Invalid actuator value specified");
57 FCGI_RejectJSONEx(context,
58 STATUS_ERROR, "Invalid actuator id specified.");