URI friendly Pmod Interface Library

Dependents:   MBD2PMD_WebServer ARD2PMD_WebServer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PmodInterface.cpp Source File

PmodInterface.cpp

00001 /* Pmod Interface Library
00002  *
00003  */
00004 
00005 #include "mbed.h"
00006 #include "PmodInterface.h"
00007 
00008 PmodInterface::PmodInterface()
00009 {
00010 }
00011 
00012 PmodInterface::~PmodInterface()
00013 {
00014 }
00015 
00016 // Initialize PmodInterface
00017 void PmodInterface::init(DigitalInOut **dio, I2C* i2c, MAX14661* mux, const int* mux_a, const int* mux_p)
00018 {
00019     _pmd = dio;  // save pointer to digital IO
00020     _i2c = i2c;  // save pointer to I2C interface
00021     _mux = mux;  // save pointer to multiplexer
00022     _mux_a = mux_a;  // save pointer to mux micro LUT
00023     _mux_p = mux_p;  // save pointer to mux pmod LUT
00024 }
00025 
00026 /* Digital I/O
00027  * /d              read all
00028  * /d/[pin]        read from pin
00029  * /d/[pin]/[cfg]  write configuration to pin
00030  * /d/[cfg]/[cfg]/[cfg]/[cfg]/[cfg]/[cfg]/[cfg]/[cfg]
00031  * [pin] = number (from 0 to 7) of the pin to access
00032  * [cfg] = pin configuration command:
00033  *     bit 0 - pin state to write
00034  *     bit 1 - pin state write disable
00035  *     bit 2 - pin direction
00036  *     bit 3 - pin direction write enable
00037  *     bit 4 - pin pullup state
00038  *     bit 5 - pin pullup write enable
00039  */
00040 void PmodInterface::fnc_dio(char* resp)
00041 {
00042     int bdat;
00043     int bcnt;
00044     switch (_args[0]) {
00045         case 0:
00046             bdat = 0;
00047             for (bcnt = 0; bcnt < 8 ; bcnt++) {
00048                 if ((*_pmd[bcnt]).read()) {
00049                     bdat += (1 << bcnt);
00050                 }
00051             }
00052             sprintf(resp,"0x%02X", bdat);
00053             break;
00054         case 1:
00055             if (_args[1] > 7) {
00056                 sprintf(resp, "!pin");
00057             } else {
00058                 sprintf(resp,"%d", (*_pmd[_args[1]]).read());
00059             }
00060             break;
00061         case 2:
00062             if (_args[1] > 7) {
00063                 sprintf(resp, "!pin");
00064             } else {
00065                 if (!(_args[2] & DB_OWD)) {
00066                     if (_args[2] & DB_OUT) {
00067                         (*_pmd[_args[1]]).write(1);
00068                         sprintf(resp++, "H");
00069                     } else {
00070                         (*_pmd[_args[1]]).write(0);
00071                         sprintf(resp++, "L");
00072                     }
00073                 }
00074                 if (_args[2] & DB_DWE) {
00075                     if (_args[2] & DB_DIR) {
00076                         (*_pmd[_args[1]]).output();
00077                         sprintf(resp++, "O");
00078                     } else {
00079                         (*_pmd[_args[1]]).input();
00080                         sprintf(resp++, "I");
00081                     }
00082                 }
00083                 if (_args[2] & DB_PWE) {
00084                     if (_args[2] & DB_PU) {
00085                         (*_pmd[_args[1]]).mode(PullUp);
00086                         sprintf(resp++, "U");
00087                     } else {
00088                         (*_pmd[_args[1]]).mode(PullNone);
00089                         sprintf(resp++, "N");
00090                     }
00091                 }
00092             }
00093             break;
00094         case 8:
00095             for (bcnt = 0; bcnt < 8 ; bcnt++) {
00096                 bdat = _args[bcnt +1];
00097                 if (!(bdat & DB_OWD)) {
00098                     if (bdat & DB_OUT) {
00099                         (*_pmd[bcnt]).write(1);
00100                         sprintf(resp++, "H");
00101                     } else {
00102                         (*_pmd[bcnt]).write(0);
00103                         sprintf(resp++, "L");
00104                     }
00105                 }
00106                 if (bdat & DB_DWE) {
00107                     if (bdat & DB_DIR) {
00108                         (*_pmd[bcnt]).output();
00109                         sprintf(resp++, "O");
00110                     } else {
00111                         (*_pmd[bcnt]).input();
00112                         sprintf(resp++, "I");
00113                     }
00114                 }
00115                 if (bdat & DB_PWE) {
00116                     if (bdat & DB_PU) {
00117                         (*_pmd[bcnt]).mode(PullUp);
00118                         sprintf(resp++, "U");
00119                     } else {
00120                         (*_pmd[bcnt]).mode(PullNone);
00121                         sprintf(resp++, "N");
00122                     }
00123                 }
00124                 sprintf(resp++, "/");
00125             }
00126             *(--resp) = '\0';
00127             break;
00128         default:
00129             sprintf(resp, "!args");
00130             break;
00131     }
00132 }
00133 
00134 /* I2C
00135  * /i/[even]/[data]...       write
00136  * /i/[odd]/[cnt]/[data]...  read
00137  * [even] = even I2C address used for writes
00138  * [odd]  = odd I2C address used for reads
00139  * [data] = data to be writen, if data is included with a read, the data
00140  *     will be written prior to the read to set the register address
00141  * [cnt]  = number of bytes to read
00142  */
00143 void PmodInterface::fnc_i2c(char* resp)
00144 {
00145     int dcnt=0;
00146     if (_args[IA_CNT] < 2) {
00147         sprintf(resp, "!args");
00148     } else {
00149         if (_args[IA_ADD] & 1) {
00150             if (_args[IA_CNT] > 2) {
00151                 for (dcnt = 0; dcnt < (_args[IA_CNT] -2) ; dcnt++) {
00152                     _dbuf[dcnt] = _args[(dcnt +3)];
00153                 }
00154                 if ((*_i2c).write(_args[IA_ADD], _dbuf, dcnt, true) != 0) {
00155                     sprintf(resp, "!write");
00156                     resp +=6;
00157                 }
00158             }
00159             if ((*_i2c).read(_args[IA_ADD], _dbuf, _args[IA_DATA])!=0) {
00160                 sprintf(resp, "!read");
00161             } else {
00162                 for (dcnt = 0; dcnt < _args[IA_DATA]; dcnt++) {
00163                     sprintf(resp,"0x%02X/", _dbuf[dcnt]);
00164                     resp +=5;
00165                 }
00166                 *(--resp) = '\0';
00167             }
00168         } else {
00169             for (dcnt = 0; dcnt < (_args[IA_CNT] -1) ; dcnt++) {
00170                 _dbuf[dcnt] = _args[(dcnt +2)];
00171             }
00172             if ((*_i2c).write(_args[IA_ADD], _dbuf, dcnt) == 0) {
00173                 sprintf(resp,"%d", dcnt);
00174             } else {
00175                 sprintf(resp, "!write");
00176             }
00177         }
00178     }
00179 }
00180 
00181 /* Multiplexer
00182  * /m/[pa]/[ma]/[pb]/[mb]  Set multiplexer channels A and B
00183  * [pa][pb]  int from 0 to 7 representint pmod pin
00184  * [ma][mb]  int from 0 to 15 representing micro pin
00185  */
00186 void PmodInterface::fnc_mux(char* resp)
00187 {
00188     if (_args[0] !=4) {
00189         sprintf(resp, "0x%08X", (*_mux).read());
00190     } else {
00191         if ((_args[1] > 8)||(_args[1] < 0)) {
00192             _args[1] =8;
00193         }
00194         if ((_args[2] > 16)||(_args[2] < 0)) {
00195             _args[2] =16;
00196         }
00197         if ((_args[3] > 8)||(_args[3] < 0)) {
00198             _args[3] =8;
00199         }
00200         if ((_args[4] > 16)||(_args[4] < 0)) {
00201             _args[4] =16;
00202         }
00203         int chA = _mux_p[_args[1]] | _mux_a[_args[2]];
00204         int chB = _mux_p[_args[3]] | _mux_a[_args[4]];
00205         (*_mux).setAB(chA, chB);
00206         sprintf(resp, "A: 0x%04X, B: 0x%04X", chA, chB);
00207     }
00208 }
00209 
00210 
00211 /* SPI
00212  * TBD
00213  */
00214 void PmodInterface::fnc_spi(char* resp)
00215 {
00216     if (_args[0] < 1) {
00217         sprintf(resp, "!args");
00218     } else {
00219         sprintf(resp, "spi tbd");
00220     }
00221 }
00222 
00223 void PmodInterface::call(char* input, char* output)
00224 {
00225     char cmd;
00226     _args[0] = 0;
00227     if (*input == '/') {
00228         input++;
00229         cmd = *input;
00230         input = strchr(input, '/');
00231         while (*input == '/') {
00232             input++;
00233             _args[(_args[0]+1)] = strtol(input, &input, 0);
00234             if (input) {
00235                 _args[0]++;
00236             }
00237         }
00238         switch (cmd) {
00239             case 'd':
00240             case 'D':
00241                 fnc_dio(output);
00242                 break;
00243             case 'i':
00244             case 'I':
00245                 fnc_i2c(output);
00246                 break;
00247             case 'm':
00248             case 'M':
00249                 fnc_mux(output);
00250                 break;
00251             case 's':
00252             case 'S':
00253                 fnc_spi(output);
00254                 break;
00255             default:
00256                 sprintf(output, "!commands:  dio i2c mux pwm spi");
00257                 break;
00258         }
00259     } else {
00260         sprintf(output, "!format:  /cmd/arg1/arg2...");
00261 
00262     }
00263 }