Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 5:b73ea174e997
- Parent:
- 4:f537311ddc53
- Child:
- 6:ec3b01ce9687
--- a/main.cpp Tue Oct 14 09:10:45 2014 +0000
+++ b/main.cpp Tue Oct 14 14:54:32 2014 +0000
@@ -7,7 +7,7 @@
const int SLAVE_ADDRESS = 0x84;
const int I2C_FREQUENCY = 100000;
-const int I2C_BUFFER_SIZE = 5;
+const int I2C_BUFFER_SIZE = 6;
enum COMMAND { PUSH, PULL, CLEAR, PRINT };
@@ -40,14 +40,14 @@
int pointer = 0;
while (1) {
- int i = slave.receive();
- switch (i) {
+ int rec = slave.receive();
+ switch (rec) {
case I2CSlave::ReadAddressed:
{
int value = memory.get(pointer);
buffer[0] = pointer;
intToByte(buffer+1, value);
- if (!slave.write(buffer, I2C_BUFFER_SIZE)) {
+ if (!slave.write(buffer, 5)) {
pc.printf("Retrieving and sending to master %d@%d\r\n", value, pointer);
} else {
pc.printf("Failed to send to master %d@%d\r\n", value, pointer);
@@ -56,45 +56,52 @@
}
case I2CSlave::WriteAddressed:
{
- if (!slave.read(buffer, I2C_BUFFER_SIZE)) {
- // Check command byte
- switch (buffer[0]) {
- case PUSH:
- {
- int address = buffer[1];
+ // First we read the command byte
+ int command = slave.read();
+
+ // Check the command
+ switch (command)
+ {
+ case PUSH:
+ // Expect 5 more bytes [address] [int value]
+ if(!slave.read(buffer, 5)) {
+ int address = buffer[0];
int value;
- byteToInt(buffer+2, &value);
+ byteToInt(buffer+1, &value);
pc.printf("Storing %d@%d\r\n", value, address);
memory.set(address, value);
- break;
+ } else {
+ pc.printf("PUSH received with missing address/data\r\n");
}
-
- case PRINT:
- {
- memory.print();
- break;
- }
-
- case PULL:
- {
- pc.printf("Setting pointer to %d\r\n", buffer[1]);
- if (buffer[1] < Memory::MEMORY_SIZE) {
- pointer = buffer[1];
+ break;
+
+ case PULL:
+ // Expect 1 more byte [address]
+ if(!slave.read(buffer, 1)) {
+ int address = buffer[0];
+ pc.printf("Setting pointer to %d\r\n", address);
+ if (address < Memory::MEMORY_SIZE) {
+ pointer = address;
} else {
pc.printf("Address out of boundary\r\n");
}
- break;
+ } else {
+ pc.printf("PULL received with missing address\r\n");
}
-
- default:
- {
- pc.printf("Unknown command byte\r\n");
- }
- }
- } else {
- pc.printf("Received WriteAddressed] without data\r\n");
+ break;
+
+ case CLEAR:
+ pc.printf("Clearing the memory\r\n");
+ memory.reset();
+ break;
+
+ case PRINT:
+ memory.print();
+ break;
+
+ default:
+ pc.printf("Unknown command byte\r\n");
}
- break;
}
}