Skip to content

Commit

Permalink
Make callbacks const-qualified. Fix invalid memory usage in callback …
Browse files Browse the repository at this point in the history
…examples. Fix PowerBroker2#42
  • Loading branch information
martincizek committed Feb 1, 2021
1 parent 950e752 commit b0eb300
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
5 changes: 3 additions & 2 deletions examples/i2c_rx_data/i2c_rx_data.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ void hi()
recSize = myTransfer.rxObj(arr, recSize);
Serial.println(arr);
}

// supplied as a reference - persistent allocation required
const functionPtr callbackArr[] = { hi };
///////////////////////////////////////////////////////////////////


Expand All @@ -34,8 +37,6 @@ void setup()
Serial.begin(115200);
Wire.begin(0);

functionPtr callbackArr[] = { hi };

///////////////////////////////////////////////////////////////// Config Parameters
configST myConfig;
myConfig.debug = true;
Expand Down
5 changes: 3 additions & 2 deletions examples/i2c_rx_datum/i2c_rx_datum.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ void hi()
Serial.print(testStruct.z);
Serial.println(testStruct.y);
}

// supplied as a reference - persistent allocation required
const functionPtr callbackArr[] = { hi };
///////////////////////////////////////////////////////////////////


Expand All @@ -24,8 +27,6 @@ void setup()
Serial.begin(115200);
Wire.begin(0);

functionPtr callbackArr[] = { hi };

///////////////////////////////////////////////////////////////// Config Parameters
configST myConfig;
myConfig.debug = true;
Expand Down
5 changes: 3 additions & 2 deletions examples/uart_rx_with_callbacks/uart_rx_with_callbacks.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ void hi()
{
Serial.println("hi");
}

// supplied as a reference - persistent allocation required
const functionPtr callbackArr[] = { hi };
///////////////////////////////////////////////////////////////////


Expand All @@ -17,8 +20,6 @@ void setup()
Serial.begin(115200);
Serial1.begin(115200);

functionPtr callbackArr[] = { hi };

///////////////////////////////////////////////////////////////// Config Parameters
configST myConfig;
myConfig.debug = true;
Expand Down
12 changes: 6 additions & 6 deletions src/Packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ const uint8_t NUM_OVERHEAD = 6; // Delete

struct configST
{
Stream* debugPort = &Serial;
bool debug = true;
functionPtr* callbacks = NULL;
uint8_t callbacksLen = 0;
Stream* debugPort = &Serial;
bool debug = true;
const functionPtr* callbacks = NULL;
uint8_t callbacksLen = 0;
};


Expand Down Expand Up @@ -160,8 +160,8 @@ class Packet
};
fsm state = find_start_byte;

functionPtr* callbacks = NULL;
uint8_t callbacksLen = 0;
const functionPtr* callbacks = NULL;
uint8_t callbacksLen = 0;

Stream* debugPort;
bool debug = false;
Expand Down

0 comments on commit b0eb300

Please sign in to comment.