/* To process ActionCode for GR Peach Devices */ #include "ActionCode.h" #include "DS1820.h" #include "DHT.h" int dht11Process (int act, PinName pin, char* arg) { int result = ACTION_SUCCESS; static DHT o(pin,DHT11); int s; if (act == READ_VALUE) { s = o.readData(); if (s != ERROR_NONE) { sprintf(arg,"Reading Error !\r\n"); } else { sprintf(arg,"Temp=%g, Humidity=%g\r\n", o.ReadTemperature(CELCIUS), o.ReadHumidity()); } result = DATA_RETURN; } else { result = ACTION_NOT_VALID; } return result; } int ds1820Process (int act, PinName pin, char* arg) { int result = ACTION_SUCCESS; static DS1820 o(pin); if (act == READ_VALUE) { o.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready wait(1); // Wait for ready convert sprintf(arg, "Temp=%g", o.temperature()); result = DATA_RETURN; // printf ("In temp Process temp = %g arg = %s and result = %d \r\n",o.temperature(),arg,result); } else { result = ACTION_NOT_VALID; } return result; } int servoProcess (int act, PinName pin, char* arg) { int result = ACTION_SUCCESS; static PwmOut o(pin); float period; switch (act) { case WRITE_VALUE: period = atof(arg); /* if (period < 0.05) period = 0.05; // Protect range; if (period > 0.1) period = 0.1; */ printf ("Action = %d Argument for Servo = %s Period = %f \r\n",act,arg,period); o.period_ms(20); o.write(period); break; case OPEN: o.period_ms(20); o.write(0.06); break; case CLOSE: o.period_ms(20); o.write(0.025); break; case TOGGLE: printf ( " Value for DigitalOut = %g \r\n",o.read()); period = o.read(); if (period > 0.04) // Open { o.period_ms(20); o.write(0.025); // Close } else // Close { o.period_ms(20); o.write(0.06); } break; case READ_VALUE : sprintf(arg, "Angle=%.3f", o.read()); // 0 is ON and 1 is OFF printf ("Read input = %s\r\n",arg); result = DATA_RETURN; break; default: result = ACTION_NOT_VALID; } return result; } int webcamProcess (int act, PinName pin, char* arg) { int result = ACTION_SUCCESS; if (act == READ_VALUE) { strcpy(arg, "url=camera.htm"); result = DATA_RETURN; } else { result = ACTION_NOT_VALID; } return result; } int irProcess (int act, PinName pin, char* arg) { int result = ACTION_SUCCESS; static PwmOut o(pin); if (act == WRITE_VALUE) { float period = atof(arg)/(100.0); printf ("Action = %d Argument for IR = %s Period = %f",act,arg,period); o.period_us(IR_FREQ); o.write(period); } else { result = ACTION_NOT_VALID; } return result; } int digitalOutProcess ( int act, DigitalOut o, char *arg) { int result = ACTION_SUCCESS; // To make it toggle if create the new one everytime the Pin status will be destroyed asn alsways show 0 // To keep status of the previous state because use static for DigitalOut make other I/O cannot work switch (act) { case TURN_ON: o.write(1); break; case TURN_OFF: o.write(0); break; case TOGGLE: printf ( " Value for DigitalOut = %d \r\n",o.read()); o = o.read() ?0:1; break; case READ_DIGITAL_INPUT : sprintf(arg, "Status=%d", o.read()); result = DATA_RETURN; break; default: result = ACTION_NOT_VALID; } return result; } // Relay use signal low = ON signal High = OFF int relayProcess ( int act, DigitalOut o, char *arg) { int result = ACTION_SUCCESS; // To make it toggle if create the new one everytime the Pin status will be destroyed asn alsways show 0 // To keep status of the previous state because use static for DigitalOut make other I/O cannot work switch (act) { case TURN_ON: o.write(0); break; case TURN_OFF: o.write(1); break; case TOGGLE: printf ( " Value for DigitalOut = %d \r\n",o.read()); o = o.read() ?0:1; break; case READ_DIGITAL_INPUT : sprintf(arg, "Status=%d", !(o.read())); // 0 is ON and 1 is OFF printf ("Read input = %s\r\n",arg); result = DATA_RETURN; break; default: result = ACTION_NOT_VALID; } return result; } // If there are return values, Result will be put in arg and send back to caller int processAction (int devClass, int action, PinName pin, char *arg) { int result = ACTION_SUCCESS; switch(devClass) { case dLED1 : static DigitalOut led1(pin); result = digitalOutProcess(action, led1, arg); break; //optional case dLED2 : static DigitalOut led2(pin); result = digitalOutProcess(action, led2,arg); break; //optional case dLED3 : static DigitalOut led3(pin); result = digitalOutProcess(action, led3,arg); break; //optional case RELAY1 : // Relat active on low need to set to high first time use static DigitalOut relay1(pin); static int firsttime1 = 1; if (firsttime1) { relay1.write(1); firsttime1 = 0; } result = relayProcess(action, relay1,arg); break; //optional case RELAY2 : static DigitalOut relay2(pin); static int firsttime2 = 1; if (firsttime2) { relay1.write(1); firsttime2 = 0; } result = relayProcess(action, relay2,arg); break; //optional case SENSORDS1820 : // result = dht11Process (action, pin, arg); result = ds1820Process (action, pin, arg); break; //optional case SERVO: result = servoProcess(action, pin, arg); break; case IR: result = irProcess(action, pin, arg); break; case WEBCAM: result = webcamProcess(action, pin, arg); break; default : //Optional result = 0; } return result; };