3 char pin_dir = "/sys/class/gpio/gpio";
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)
11 void SetPin(int value, int pin_num) {
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");
19 strncpy(buffer, "1", ARRAY_SIZE(buffer) - 1);
22 strncpy(buffer, "0", ARRAY_SIZE(buffer) - 1);
24 int write = write(pin, buffer, strlen(buffer));
25 if (write < 0) perror ("Failed to write to pin");
29 //TODO: Be able to write to multiple PWM modules - easy to change
30 // but current unsure about how many PWM signals we need
32 void ActuatorHandler(FCGIContext *context, ActuatorId id, const char *set_value) {
35 switch(id) { //Add new actuators here
36 case ACT_PRESSURE: //Suppose is pressure regulator. 0-700 input (kPa)
38 if (pwm_active == 0) { //if inactive, start the pwm module
41 pwm_set_period(FREQ); //50Hz defined in pwm header file
43 int value = strtol(set_value, &ptr, 10);
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
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);
56 FCGI_RejectJSONEx(context,
57 STATUS_ERROR, "Invalid pressure specified.");
62 int value = strtol(set_value, &ptr, 10);
64 //code to set pin to value
65 SetPin(value, 1); //"1" will need to be changed to pin numbers decided by electronics team
67 const char *state = "off";
70 FCGI_BeginJSON(context, STATUS_OK);
71 FCGI_JSONKey("description");
72 FCGI_JSONValue("\"Solenoid 1 turned %s!\"", state);
75 FCGI_RejectJSON(context, "Invalid actuator value specified");
79 FCGI_RejectJSONEx(context,
80 STATUS_ERROR, "Invalid actuator id specified.");