nexpaq / SerialInterface

Fork of SerialInterface by Greg Steiert

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialInterface.cpp Source File

SerialInterface.cpp

00001 /* Pmod Interface Library
00002  *
00003  */
00004 
00005 #include "mbed.h"
00006 #include "SerialInterface.h"
00007 
00008 SerialInterface::SerialInterface(I2C *i2c, SPI *spi, DigitalInOut* gpio, AnalogIn* ain): _i2c(i2c), _spi(spi) 
00009 {
00010     _i2c = i2c;  // save pointer to I2C port
00011     _spi = spi;  // save pointer to SPI port
00012     _gpio = gpio;  // save pointer to GPIO pins
00013     _ain = ain;  // save pointer to AIN pins
00014 }
00015 
00016 SerialInterface::~SerialInterface()
00017 {
00018 }
00019 
00020 /* Analog In
00021  * /a              read all
00022  * /a/[pin]        read from pin
00023  *   Returns 16bit normalized result
00024  */
00025 void SerialInterface::fnc_ain(char* resp)
00026 {
00027     switch (_args[0]) {
00028         case 0:
00029             sprintf(resp, "[0x%04X,0x%04X,0x%04X,0x%04X,0x%04X,0x%04X,0x%04X,0x%04X]", 
00030             _ain[0].read_u16(), _ain[1].read_u16(), _ain[2].read_u16(), _ain[3].read_u16(), 
00031             _ain[4].read_u16(), _ain[5].read_u16(), _ain[6].read_u16(), _ain[7].read_u16() );
00032             break;
00033         case 1:
00034             sprintf(resp, "[0x%04X]", _ain[_args[1]].read_u16());
00035             break;
00036         default:
00037             sprintf(resp, "[-1]");
00038             break;
00039     }
00040 }
00041 
00042 /* Digital I/O
00043  * /d              read all
00044  * /d/[pin]        read from pin
00045  * /d/[pin]/[cfg]  write configuration to pin
00046  * [pin] = number (from 0 to 7) of the pin to access
00047  * [cfg] = pin configuration:
00048  *     0 - output low
00049  *     1 - output high
00050  *     2 - input pull down
00051  *     3 - input pull up
00052  *     4 - input pull none
00053  */
00054 void SerialInterface::fnc_dio(char* resp)
00055 {
00056     int bdat;
00057     int bcnt;
00058     switch (_args[0]) {
00059         case 0:
00060             sprintf(resp, "[");
00061             resp +=1;
00062             for (bcnt = 0; bcnt < 8 ; bcnt++) {
00063                 bdat = _gpio[bcnt].read();
00064                 sprintf(resp, "%d,", bdat);
00065                 resp +=2;
00066             }
00067             sprintf((resp-1), "]");
00068             break;
00069         case 1:
00070             if (_args[1] > 7) {
00071                 sprintf(resp, "[-1]");
00072             } else {
00073                 sprintf(resp,"[%d]", _gpio[_args[1]].read());
00074             }
00075             break;
00076         case 2:
00077             if (_args[1] > 7) {
00078                 sprintf(resp, "[-1]");
00079             } else {
00080                 if (_args[2] < 2) {
00081                     _gpio[_args[1]].write(_args[2]);
00082                     _gpio[_args[1]].output();
00083                 } else {
00084                     if (_args[2] == 2) {
00085                         _gpio[_args[1]].mode(PullDown);
00086                     } else if (_args[2] == 3) {
00087                         _gpio[_args[1]].mode(PullUp);
00088                     } else {
00089                         _gpio[_args[1]].mode(PullNone);
00090                     }
00091                     _gpio[_args[1]].input();
00092                 }
00093                 sprintf(resp,"[%d]", _args[2]);
00094             }
00095             break;
00096         default:
00097             sprintf(resp, "[-1]");
00098             break;
00099     }
00100 }
00101 
00102 /* I2C
00103  * /i/[even]/[data]...       write
00104  * /i/[odd]/[cnt]/[data]...  read
00105  * [even] = even I2C address used for writes
00106  * [odd]  = odd I2C address used for reads
00107  * [data] = data to be writen, if data is included with a read, the data
00108  *     will be written prior to the read to set the register address
00109  * [cnt]  = number of bytes to read
00110  * returns data read in JSON array
00111  */
00112 void SerialInterface::fnc_i2c(char* resp)
00113 {
00114     int dcnt=0;
00115     if (_args[IA_CNT] < 2) {
00116         sprintf(resp, "[-1]");
00117     } else {
00118         if (_args[IA_ADD] & 1) {
00119             sprintf(resp, "[");
00120             resp +=1;
00121             if (_args[IA_CNT] > 2) {
00122                 for (dcnt = 0; dcnt < (_args[IA_CNT] -2) ; dcnt++) {
00123                     _dbuf[dcnt] = _args[(dcnt +3)];
00124                 }
00125                 if ((*_i2c).write(_args[IA_ADD], _dbuf, dcnt, true) != 0) {
00126                     sprintf(resp, "-1,");
00127                     resp +=3;
00128                 }
00129             }
00130             if ((*_i2c).read(_args[IA_ADD], _dbuf, _args[IA_DATA])!=0) {
00131                 sprintf(resp, "-1]");
00132             } else {
00133                 for (dcnt = 0; dcnt < _args[IA_DATA]; dcnt++) {
00134                     sprintf(resp,"0x%02X,", _dbuf[dcnt]);
00135                     resp +=5;
00136                 }
00137                 sprintf((resp-1), "]");
00138             }
00139         } else {
00140             for (dcnt = 0; dcnt < (_args[IA_CNT] -1) ; dcnt++) {
00141                 _dbuf[dcnt] = _args[(dcnt +2)];
00142             }
00143             if ((*_i2c).write(_args[IA_ADD], _dbuf, dcnt) == 0) {
00144                 sprintf(resp,"[%d]", dcnt);
00145             } else {
00146                 sprintf(resp, "[-1]");
00147             }
00148         }
00149     }
00150 }
00151 
00152 /* SPI
00153  * /s/[cfg]/[data]...      read+write
00154  * [cfg] = SPI configuration specifies gpio to use for chip
00155  *   select and other SPI parameters:
00156  *   0x[Byte3][Byte2][Byte1][Byte0]
00157  *     Byte3 = SCK frequency in MHz (0 = no change)
00158  *     Byte2 = Format:
00159  *       0 = no change
00160  *       1 = Mode 1
00161  *       2 = Mode 2
00162  *       3 = Mode 3
00163  *       4 = Mode 0
00164  *     Byte1 = CS polarity 1 = active high, 0 = active low
00165  *     Byte0 = GPIO to use for CS (0-7)
00166  *       0 = P5_3
00167  *       1 = P5_4
00168  *       2 = P5_5
00169  *       3 = P5_6
00170  *       4 = P3_0
00171  *       5 = P3_1
00172  *       6 = P3_2
00173  *       7 = P3_3
00174  * [data] = data to be writen
00175  *   this shifts out each data byte provided and
00176  *   returns each byte read in a JSON array
00177  */
00178 void SerialInterface::fnc_spi(char* resp)
00179 {
00180     int dcnt=1;
00181     int dataIn;
00182     spiConfig_t spiCfg;
00183     if (_args[0] < 1) {
00184         sprintf(resp, "[-1]");
00185     } else {
00186         spiCfg.merged = _args[1];
00187         if (spiCfg.freq) {
00188             (*_spi).frequency(spiCfg.freq * 1000000);
00189         }
00190         if (spiCfg.format) {
00191             (*_spi).format(8, (spiCfg.format & 3));
00192         }
00193         if (_args[0] > 1) {
00194             sprintf(resp, "[");
00195             resp++;
00196             _gpio[spiCfg.csPin] = (spiCfg.csPol);
00197             while(dcnt < _args[0]) {
00198                 dcnt++;
00199                 dataIn = (*_spi).write(_args[dcnt]);
00200                 sprintf(resp, "0x%02X,", dataIn);
00201                 resp += 5;
00202             }
00203             _gpio[spiCfg.csPin] = !(spiCfg.csPol);
00204             sprintf((resp-1), "]");
00205         }
00206     }
00207 }
00208 
00209 void SerialInterface::call(char* input, char* output)
00210 {
00211     char cmd;
00212     _args[0] = 0;
00213     if (*input == '/') {
00214         input++;
00215         cmd = *input;
00216         input = strchr(input, '/');
00217         while (*input == '/') {
00218             input++;
00219             _args[(_args[0]+1)] = strtol(input, &input, 0);
00220             if (input) {
00221                 _args[0]++;
00222             }
00223         }
00224         switch (cmd) {
00225             case 'a':
00226             case 'A':
00227                 fnc_ain(output);
00228                 break;
00229             case 'd':
00230             case 'D':
00231                 fnc_dio(output);
00232                 break;
00233             case 'i':
00234             case 'I':
00235                 fnc_i2c(output);
00236                 break;
00237             case 's':
00238             case 'S':
00239                 fnc_spi(output);
00240                 break;
00241             default:
00242                 sprintf(output, "!commands: ain dio i2c spi");
00243                 break;
00244         }
00245     } else {
00246         sprintf(output, "!format:  /cmd/arg1/arg2...");
00247 
00248     }
00249 }