Test program for I2CEeprom library
This program does a number of write then read tests to ensure that the program can write and successfully read back buffers of various sizes. It also shows how to write and read arrays of POD classes.
main.cpp@0:64b9e47c0f13, 2015-08-16 (annotated)
- Committer:
- rhourahane
- Date:
- Sun Aug 16 18:56:28 2015 +0000
- Revision:
- 0:64b9e47c0f13
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rhourahane | 0:64b9e47c0f13 | 1 | #include "mbed.h" |
rhourahane | 0:64b9e47c0f13 | 2 | #include <I2CEeprom.h> |
rhourahane | 0:64b9e47c0f13 | 3 | |
rhourahane | 0:64b9e47c0f13 | 4 | DigitalOut myled(LED1); |
rhourahane | 0:64b9e47c0f13 | 5 | |
rhourahane | 0:64b9e47c0f13 | 6 | struct Info { |
rhourahane | 0:64b9e47c0f13 | 7 | char name[32]; |
rhourahane | 0:64b9e47c0f13 | 8 | int age; |
rhourahane | 0:64b9e47c0f13 | 9 | float score; |
rhourahane | 0:64b9e47c0f13 | 10 | }; |
rhourahane | 0:64b9e47c0f13 | 11 | |
rhourahane | 0:64b9e47c0f13 | 12 | const char *convBool(bool value) { return value ? "true" : "false"; } |
rhourahane | 0:64b9e47c0f13 | 13 | |
rhourahane | 0:64b9e47c0f13 | 14 | int main() { |
rhourahane | 0:64b9e47c0f13 | 15 | I2CEeprom memory(I2C_SDA,I2C_SCL, 0xAE, 32, 0); |
rhourahane | 0:64b9e47c0f13 | 16 | if (memory.write(1, 'Z') != 1) { |
rhourahane | 0:64b9e47c0f13 | 17 | printf("Write failed\n\r"); |
rhourahane | 0:64b9e47c0f13 | 18 | } |
rhourahane | 0:64b9e47c0f13 | 19 | |
rhourahane | 0:64b9e47c0f13 | 20 | char value; |
rhourahane | 0:64b9e47c0f13 | 21 | if (memory.read(1, value) == 0) { |
rhourahane | 0:64b9e47c0f13 | 22 | printf("Read failed\n\r"); |
rhourahane | 0:64b9e47c0f13 | 23 | } else { |
rhourahane | 0:64b9e47c0f13 | 24 | printf("Value read [%c] valid %s\n\r", value, convBool(value == 'Z')); |
rhourahane | 0:64b9e47c0f13 | 25 | } |
rhourahane | 0:64b9e47c0f13 | 26 | |
rhourahane | 0:64b9e47c0f13 | 27 | const char *pat = "Abcdefghijklmnopqrstuvwxyz"; |
rhourahane | 0:64b9e47c0f13 | 28 | if (memory.write(50, pat, 27) != 27) { |
rhourahane | 0:64b9e47c0f13 | 29 | printf("Write failed\n\r"); |
rhourahane | 0:64b9e47c0f13 | 30 | } |
rhourahane | 0:64b9e47c0f13 | 31 | |
rhourahane | 0:64b9e47c0f13 | 32 | if (memory.read(50, value) != 1) { |
rhourahane | 0:64b9e47c0f13 | 33 | printf("Read failed\n\r"); |
rhourahane | 0:64b9e47c0f13 | 34 | } else { |
rhourahane | 0:64b9e47c0f13 | 35 | printf("First char read [%c]\n\r", value); |
rhourahane | 0:64b9e47c0f13 | 36 | |
rhourahane | 0:64b9e47c0f13 | 37 | char buffer[28] = {'A', '\0' }; |
rhourahane | 0:64b9e47c0f13 | 38 | if (memory.read(50, buffer, 27) != 27) { |
rhourahane | 0:64b9e47c0f13 | 39 | printf("Read failed\n\r"); |
rhourahane | 0:64b9e47c0f13 | 40 | } else { |
rhourahane | 0:64b9e47c0f13 | 41 | printf("Value read [%s] valid %s\n\r", buffer, convBool(strcmp(buffer, pat) == 0)); |
rhourahane | 0:64b9e47c0f13 | 42 | } |
rhourahane | 0:64b9e47c0f13 | 43 | } |
rhourahane | 0:64b9e47c0f13 | 44 | |
rhourahane | 0:64b9e47c0f13 | 45 | memory.fill(0, 'A', 512); |
rhourahane | 0:64b9e47c0f13 | 46 | char small[33]; |
rhourahane | 0:64b9e47c0f13 | 47 | if (memory.read(10, small, 32) != 32) { |
rhourahane | 0:64b9e47c0f13 | 48 | printf("Read failed\n\r"); |
rhourahane | 0:64b9e47c0f13 | 49 | } else { |
rhourahane | 0:64b9e47c0f13 | 50 | bool same = true; |
rhourahane | 0:64b9e47c0f13 | 51 | for (int count = 0; count != 32; ++count) { |
rhourahane | 0:64b9e47c0f13 | 52 | same &= small[count] == 'A'; |
rhourahane | 0:64b9e47c0f13 | 53 | } |
rhourahane | 0:64b9e47c0f13 | 54 | printf("Value read [%.32s] valid %s\n\r", small, convBool(same)); |
rhourahane | 0:64b9e47c0f13 | 55 | } |
rhourahane | 0:64b9e47c0f13 | 56 | |
rhourahane | 0:64b9e47c0f13 | 57 | const char *bigPat = "123456789A123456789B123456789C123456789D123456789E123456789F123456789G123456789H123456789a123456789b123456789c123456789d123456789e123456789f123456789g123456789h123456789A123456789B123456789C123456789D123456789E123456789F123456789G123456789H123456789a123456789b123456789c123456789d123456789e123456789f123456789g123456789h"; |
rhourahane | 0:64b9e47c0f13 | 58 | size_t patSize = strlen(bigPat) + 1; |
rhourahane | 0:64b9e47c0f13 | 59 | if (!memory.write(42, bigPat, patSize)) { |
rhourahane | 0:64b9e47c0f13 | 60 | printf("Write failed\n\r"); |
rhourahane | 0:64b9e47c0f13 | 61 | } |
rhourahane | 0:64b9e47c0f13 | 62 | |
rhourahane | 0:64b9e47c0f13 | 63 | char bigBuffer[512] = {'A', '\0' }; |
rhourahane | 0:64b9e47c0f13 | 64 | if (memory.read(42, bigBuffer, patSize) != patSize) { |
rhourahane | 0:64b9e47c0f13 | 65 | printf("Read failed\n\r"); |
rhourahane | 0:64b9e47c0f13 | 66 | } else { |
rhourahane | 0:64b9e47c0f13 | 67 | printf("Value read [%s]\n\r", bigBuffer); |
rhourahane | 0:64b9e47c0f13 | 68 | printf("Same is %s\n\r", convBool(strcmp(bigPat, bigBuffer) == 0)); |
rhourahane | 0:64b9e47c0f13 | 69 | } |
rhourahane | 0:64b9e47c0f13 | 70 | |
rhourahane | 0:64b9e47c0f13 | 71 | int values[4] = { 1, 2, 3, 4}; |
rhourahane | 0:64b9e47c0f13 | 72 | if (memory.write(4, values) != sizeof(values)) { |
rhourahane | 0:64b9e47c0f13 | 73 | printf("Write failed\n\r"); |
rhourahane | 0:64b9e47c0f13 | 74 | } |
rhourahane | 0:64b9e47c0f13 | 75 | |
rhourahane | 0:64b9e47c0f13 | 76 | int newValues[4]; |
rhourahane | 0:64b9e47c0f13 | 77 | if (memory.read(4, newValues) != sizeof(values)) { |
rhourahane | 0:64b9e47c0f13 | 78 | printf("Read failed\n\r"); |
rhourahane | 0:64b9e47c0f13 | 79 | } |
rhourahane | 0:64b9e47c0f13 | 80 | |
rhourahane | 0:64b9e47c0f13 | 81 | for (int count = 0; count != 4; ++count) { |
rhourahane | 0:64b9e47c0f13 | 82 | printf("Values[%d] = %d\n\r", count, newValues[count]); |
rhourahane | 0:64b9e47c0f13 | 83 | } |
rhourahane | 0:64b9e47c0f13 | 84 | |
rhourahane | 0:64b9e47c0f13 | 85 | Info foo[] = { |
rhourahane | 0:64b9e47c0f13 | 86 | { "Bill", 18, 2.3 }, |
rhourahane | 0:64b9e47c0f13 | 87 | { "Bert", 21, 4.5 }, |
rhourahane | 0:64b9e47c0f13 | 88 | { "Mary", 19, 7.5 } |
rhourahane | 0:64b9e47c0f13 | 89 | }; |
rhourahane | 0:64b9e47c0f13 | 90 | |
rhourahane | 0:64b9e47c0f13 | 91 | if (!memory.write(100, foo)) { |
rhourahane | 0:64b9e47c0f13 | 92 | printf("Write failed\n\r"); |
rhourahane | 0:64b9e47c0f13 | 93 | } |
rhourahane | 0:64b9e47c0f13 | 94 | |
rhourahane | 0:64b9e47c0f13 | 95 | Info bar[3]; |
rhourahane | 0:64b9e47c0f13 | 96 | if (!memory.read(100, bar)) { |
rhourahane | 0:64b9e47c0f13 | 97 | printf("Read failed\n\r"); |
rhourahane | 0:64b9e47c0f13 | 98 | } |
rhourahane | 0:64b9e47c0f13 | 99 | for (int count = 0; count != 3; ++count) { |
rhourahane | 0:64b9e47c0f13 | 100 | printf("name %s, age %d, score %f\n\r", bar[count].name, bar[count].age, bar[count].score); |
rhourahane | 0:64b9e47c0f13 | 101 | } |
rhourahane | 0:64b9e47c0f13 | 102 | |
rhourahane | 0:64b9e47c0f13 | 103 | while(1) { |
rhourahane | 0:64b9e47c0f13 | 104 | myled = 1; |
rhourahane | 0:64b9e47c0f13 | 105 | wait(0.2); |
rhourahane | 0:64b9e47c0f13 | 106 | myled = 0; |
rhourahane | 0:64b9e47c0f13 | 107 | wait(0.2); |
rhourahane | 0:64b9e47c0f13 | 108 | } |
rhourahane | 0:64b9e47c0f13 | 109 | } |