Command processor to access I2C and SPI Takes URI coded commands and returns JSON array
Fork of SerialInterface by
SerialInterface.h@9:f52181496512, 2017-10-19 (annotated)
- Committer:
- switches
- Date:
- Thu Oct 19 23:19:42 2017 +0000
- Revision:
- 9:f52181496512
- Parent:
- 8:a0937dc92631
- Child:
- 10:94a98d095b2a
Changed I2C and SPI to pass by pointer instead of by reference
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsteiert | 0:828bfd94972b | 1 | /* Pmod Interface Library |
gsteiert | 0:828bfd94972b | 2 | * |
gsteiert | 0:828bfd94972b | 3 | */ |
switches | 2:3f6a8ac111a9 | 4 | #ifndef SERIALINTERFACE_H |
switches | 2:3f6a8ac111a9 | 5 | #define SERIALINTERFACE_H |
gsteiert | 0:828bfd94972b | 6 | |
gsteiert | 0:828bfd94972b | 7 | #include "mbed.h" |
switches | 2:3f6a8ac111a9 | 8 | #include "SerialInterface.h" |
gsteiert | 0:828bfd94972b | 9 | |
switches | 8:a0937dc92631 | 10 | /** Serial Interface Library, Provides utilities for remotely accessing peripherals |
gsteiert | 0:828bfd94972b | 11 | * |
gsteiert | 0:828bfd94972b | 12 | * Example: |
gsteiert | 0:828bfd94972b | 13 | * @code |
gsteiert | 0:828bfd94972b | 14 | * // Configure board to pass UART signals to peripheral connector. |
gsteiert | 0:828bfd94972b | 15 | * |
switches | 2:3f6a8ac111a9 | 16 | * #include "SerialInterface.h" |
gsteiert | 0:828bfd94972b | 17 | * |
switches | 3:601b78524967 | 18 | * I2C i2c(P3_4, P3_5); |
switches | 4:0bd9ec504040 | 19 | * SPI spi(P5_1, P5_2, P5_0); |
switches | 8:a0937dc92631 | 20 | * DigitalInOut gpio[] = {P5_3, P5_4, P5_5, P5_6, P3_0, P3_1, P3_2, P3_3}; |
switches | 8:a0937dc92631 | 21 | * AnalogIn ain[] = {AIN_0, AIN_1, AIN_2, AIN_3, AIN_4, AIN_5, AIN_6, AIN_7}; |
switches | 8:a0937dc92631 | 22 | * |
switches | 8:a0937dc92631 | 23 | * SerialInterface serInt(i2c, spi, gpio, ain); |
gsteiert | 0:828bfd94972b | 24 | * |
gsteiert | 0:828bfd94972b | 25 | * int main() { |
gsteiert | 0:828bfd94972b | 26 | * char ibuf[256]; |
gsteiert | 0:828bfd94972b | 27 | * char obuf[256]; |
gsteiert | 0:828bfd94972b | 28 | * while(1) { |
gsteiert | 0:828bfd94972b | 29 | * scanf("%s", ibuf); |
switches | 3:601b78524967 | 30 | * serInt.call(ibuf, obuf); |
gsteiert | 0:828bfd94972b | 31 | * printf("%s=", ibuf); |
gsteiert | 0:828bfd94972b | 32 | * printf("%s\n", obuf); |
gsteiert | 0:828bfd94972b | 33 | * } |
gsteiert | 0:828bfd94972b | 34 | * @endcode |
gsteiert | 0:828bfd94972b | 35 | */ |
switches | 2:3f6a8ac111a9 | 36 | class SerialInterface |
gsteiert | 0:828bfd94972b | 37 | { |
gsteiert | 0:828bfd94972b | 38 | public: |
gsteiert | 0:828bfd94972b | 39 | |
switches | 2:3f6a8ac111a9 | 40 | /** Create a SerialInterface interface |
gsteiert | 0:828bfd94972b | 41 | * |
gsteiert | 0:828bfd94972b | 42 | */ |
switches | 9:f52181496512 | 43 | SerialInterface(I2C *i2c, SPI *spi, DigitalInOut* gpio, AnalogIn* ain); |
gsteiert | 0:828bfd94972b | 44 | |
switches | 2:3f6a8ac111a9 | 45 | ~SerialInterface(); |
gsteiert | 0:828bfd94972b | 46 | |
gsteiert | 0:828bfd94972b | 47 | /** Name the I2C arguments |
gsteiert | 0:828bfd94972b | 48 | */ |
gsteiert | 0:828bfd94972b | 49 | enum PINTi2cArgs { |
gsteiert | 0:828bfd94972b | 50 | IA_CNT = 0, /**< Argument Count */ |
gsteiert | 0:828bfd94972b | 51 | IA_ADD, /**< Device Address */ |
gsteiert | 0:828bfd94972b | 52 | IA_DATA, /**< Data, Read = # bytes to read, Write = first data byte */ |
gsteiert | 0:828bfd94972b | 53 | IA_RDDA /**< Read Data, data to write prior to read */ |
gsteiert | 0:828bfd94972b | 54 | }; |
gsteiert | 0:828bfd94972b | 55 | |
switches | 7:06a2eb2483f6 | 56 | /** Process URI encoded commands |
gsteiert | 0:828bfd94972b | 57 | * |
gsteiert | 0:828bfd94972b | 58 | * @param input a pointer to the string containing the command |
gsteiert | 0:828bfd94972b | 59 | * @param output a pointer to the string to write the result |
gsteiert | 0:828bfd94972b | 60 | */ |
gsteiert | 0:828bfd94972b | 61 | void call(char* input, char* output); |
gsteiert | 0:828bfd94972b | 62 | |
gsteiert | 0:828bfd94972b | 63 | private: |
gsteiert | 0:828bfd94972b | 64 | |
switches | 5:9e27e2a46fa6 | 65 | // Types |
switches | 5:9e27e2a46fa6 | 66 | typedef union { |
switches | 5:9e27e2a46fa6 | 67 | struct { |
switches | 5:9e27e2a46fa6 | 68 | char csPin; |
switches | 5:9e27e2a46fa6 | 69 | char csPol; |
switches | 5:9e27e2a46fa6 | 70 | char format; |
switches | 5:9e27e2a46fa6 | 71 | char freq; |
switches | 5:9e27e2a46fa6 | 72 | }; |
switches | 5:9e27e2a46fa6 | 73 | int merged; |
switches | 5:9e27e2a46fa6 | 74 | } spiConfig_t; |
switches | 5:9e27e2a46fa6 | 75 | |
gsteiert | 0:828bfd94972b | 76 | // Internal Functions |
gsteiert | 0:828bfd94972b | 77 | |
switches | 6:c9b7256c8261 | 78 | /** Process Analog Input Command |
switches | 6:c9b7256c8261 | 79 | * |
switches | 6:c9b7256c8261 | 80 | * @param resp a pointer to the string to write the result |
switches | 6:c9b7256c8261 | 81 | */ |
switches | 6:c9b7256c8261 | 82 | void fnc_ain(char* resp); |
switches | 6:c9b7256c8261 | 83 | |
switches | 5:9e27e2a46fa6 | 84 | /** Process Digital I/O Command |
switches | 5:9e27e2a46fa6 | 85 | * |
switches | 5:9e27e2a46fa6 | 86 | * @param resp a pointer to the string to write the result |
switches | 5:9e27e2a46fa6 | 87 | */ |
switches | 5:9e27e2a46fa6 | 88 | void fnc_dio(char* resp); |
switches | 5:9e27e2a46fa6 | 89 | |
gsteiert | 0:828bfd94972b | 90 | /** Process I2C Command |
switches | 5:9e27e2a46fa6 | 91 | * |
gsteiert | 0:828bfd94972b | 92 | * @param resp a pointer to the string to write the result |
gsteiert | 0:828bfd94972b | 93 | */ |
gsteiert | 0:828bfd94972b | 94 | void fnc_i2c(char* resp); |
gsteiert | 0:828bfd94972b | 95 | |
gsteiert | 0:828bfd94972b | 96 | /** Process SPI Command |
switches | 5:9e27e2a46fa6 | 97 | * |
gsteiert | 0:828bfd94972b | 98 | * @param resp a pointer to the string to write the result |
gsteiert | 0:828bfd94972b | 99 | */ |
gsteiert | 0:828bfd94972b | 100 | void fnc_spi(char* resp); |
gsteiert | 0:828bfd94972b | 101 | |
gsteiert | 0:828bfd94972b | 102 | // Internal Resources |
switches | 9:f52181496512 | 103 | I2C *_i2c; |
switches | 9:f52181496512 | 104 | SPI *_spi; |
switches | 5:9e27e2a46fa6 | 105 | DigitalInOut *_gpio; |
switches | 6:c9b7256c8261 | 106 | AnalogIn *_ain; |
gsteiert | 0:828bfd94972b | 107 | |
gsteiert | 0:828bfd94972b | 108 | // Internal Buffers |
switches | 2:3f6a8ac111a9 | 109 | int _args[64]; |
switches | 5:9e27e2a46fa6 | 110 | char _dbuf[256]; |
gsteiert | 0:828bfd94972b | 111 | |
gsteiert | 0:828bfd94972b | 112 | }; |
gsteiert | 0:828bfd94972b | 113 | |
gsteiert | 0:828bfd94972b | 114 | #endif |