783bd41eb8e712e3af368946586322861d61494c
[matches/MCTX3420.git] / BBB code / ActuatorHandler_real.c
1 void ActuatorHandler(FCGIContext *context, ActuatorId id, const char *set_value) {
2         char *ptr;
3         
4         switch(id) { //Add new actuators here
5                 case ACT_PRESSURE: //Suppose is pressure regulator. 0-700 input (kPa)
6                 //requires PWM control
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
13                 {
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);
19                                 FCGI_EndJSON();
20                         } else {
21                                 FCGI_RejectJSONEx(context, 
22                                         STATUS_ERROR, "Invalid pressure specified.");
23                         }
24                 } break;
25                 case ACT_SOLENOID1:
26                 //requires digital control
27                 {
28                         int value = strtol(set_value, &ptr, 10);
29                         if (*ptr == '\0') {
30                                 //code to set pin to value
31                                 //move this to separate function, will need to be repeated
32                                 int fd;
33                                 char buffer[10];
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");
36                                 if (value) {
37                                         strncpy(buffer, "1", ARRAY_SIZE(buffer) - 1);                           //copy value to buffer
38                                 } else {
39                                         strncpy(buffer, "0", ARRAY_SIZE(buffer) - 1);
40                                 }
41                                 int write = write(fd, buffer, strlen(buffer));                                  //write buffer to pin
42                                 if (write < 0) perror("Failed to write to pin");
43                                 close(fd);
44                                 //code for server
45                                 const char *state = "off";
46                                 if (value)
47                                         state = "on";
48                                 FCGI_BeginJSON(context, STATUS_OK);
49                                 FCGI_JSONKey("description");
50                                 FCGI_JSONValue("\"Solenoid 1 turned %s!\"", state);
51                                 FCGI_EndJSON();
52                         } else {
53                                 FCGI_RejectJSON(context, "Invalid actuator value specified");
54                         }
55                 } break;
56                 default:
57                         FCGI_RejectJSONEx(context, 
58                                 STATUS_ERROR, "Invalid actuator id specified.");
59         }
60 }

UCC git Repository :: git.ucc.asn.au