Add test gui
[matches/MCTX3420.git] / BBB code / old / ActuatorHandler_real.c
1 #include "pwm.h"
2
3 char pin_dir = "/sys/class/gpio/gpio";
4 int pwm_active = 0;
5
6 /** Sets a GPIO pin to the desired value
7 *       @param value - the value (1 or 0) to write to the pin
8 *       @param pin_num - the number of the pin (refer to electronics team)
9 */
10
11 void SetPin(int value, int pin_num) {
12         int pin;
13         char buffer[10];
14         char pin_path[40];
15         snprintf(pin_path, sizeof(pin_path), "%s%d%s", pin_dir, pin_num, "/value");     //create pin path
16         pin = open(pin_path, O_WRONLY);
17         if (pin < 0) perror("Failed to open pin");
18         if (value) {
19                 strncpy(buffer, "1", ARRAY_SIZE(buffer) - 1);
20         }
21         else {
22                 strncpy(buffer, "0", ARRAY_SIZE(buffer) - 1);
23         }
24         int write = write(pin, buffer, strlen(buffer));
25         if (write < 0) perror ("Failed to write to pin");
26         close(pin);
27 }
28
29 //TODO: Be able to write to multiple PWM modules - easy to change
30 //              but current unsure about how many PWM signals we need
31
32 void ActuatorHandler(FCGIContext *context, ActuatorId id, const char *set_value) {
33         char *ptr;
34         
35         switch(id) {                                    //Add new actuators here
36                 case ACT_PRESSURE:                      //Suppose is pressure regulator. 0-700 input (kPa)
37                 {       
38                         if (pwm_active == 0) {  //if inactive, start the pwm module
39                                 pwm_init();
40                                 pwm_start();
41                                 pwm_set_period(FREQ);                           //50Hz defined in pwm header file
42                         }
43                         int value = strtol(set_value, &ptr, 10);
44                         //Beaglebone code
45                         if(value >= 0 && value <= 700) {
46                                 double duty = value/700 * 100;          //convert pressure to duty percentage
47                                 pwm_set_duty((int)duty);                        //set duty percentage for actuator
48                         }
49                         //server code
50                         if (*ptr == '\0' && value >= 0 && value <= 700) {
51                                 FCGI_BeginJSON(context, STATUS_OK);
52                                 FCGI_JSONKey("description");
53                                 FCGI_JSONValue("\"Set pressure to %d kPa!\"", value);
54                                 FCGI_EndJSON();
55                         } else {
56                                 FCGI_RejectJSONEx(context, 
57                                         STATUS_ERROR, "Invalid pressure specified.");
58                         }
59                 } break;
60                 case ACT_SOLENOID1:
61                 {
62                         int value = strtol(set_value, &ptr, 10);
63                         if (*ptr == '\0') {
64                                                                                 //code to set pin to value
65                                 SetPin(value, 1);               //"1" will need to be changed to pin numbers decided by electronics team
66                                                                                 //code for server
67                                 const char *state = "off";
68                                 if (value)
69                                         state = "on";
70                                 FCGI_BeginJSON(context, STATUS_OK);
71                                 FCGI_JSONKey("description");
72                                 FCGI_JSONValue("\"Solenoid 1 turned %s!\"", state);
73                                 FCGI_EndJSON();
74                         } else {
75                                 FCGI_RejectJSON(context, "Invalid actuator value specified");
76                         }
77                 } break;
78                 default:
79                         FCGI_RejectJSONEx(context, 
80                                 STATUS_ERROR, "Invalid actuator id specified.");
81         }
82 }

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