From 352f9dcbbf38a77a43690f4b5d29ad54b5937cae Mon Sep 17 00:00:00 2001 From: Pete Lewis <601236+lewispg228@users.noreply.github.com> Date: Wed, 23 Feb 2022 10:03:39 -0700 Subject: [PATCH 1/2] Add check for non-zero numberOfBytesReceived ensure we received valid byte or bytes - note, an I2C scan does not send anything, so without this, it would overright registerNumber with Zero value. --- Firmware/Qwiic_KeyPad/Qwiic_KeyPad.ino | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Firmware/Qwiic_KeyPad/Qwiic_KeyPad.ino b/Firmware/Qwiic_KeyPad/Qwiic_KeyPad.ino index 0d7549f..98fc090 100644 --- a/Firmware/Qwiic_KeyPad/Qwiic_KeyPad.ino +++ b/Firmware/Qwiic_KeyPad/Qwiic_KeyPad.ino @@ -211,6 +211,8 @@ void loop(void) //(Serves rewritable I2C address and updateFifo command) void receiveEvent(int numberOfBytesReceived) { + if(numberOfBytesReceived > 0) // ensure we received valid byte or bytes - note, an I2C scan does not send anything, so without this, it would overright registerNumber with Zero. + { registerNumber = Wire.read(); //Get the memory map offset from the user //Begin recording the following incoming bytes to the temp memory map @@ -229,6 +231,7 @@ void receiveEvent(int numberOfBytesReceived) } recordSystemSettings(); + } } //Send back a number of bytes via an array, max 32 bytes From 2efbd98b825be0280e4b57465d10a4caf8cbae84 Mon Sep 17 00:00:00 2001 From: Pete Lewis <601236+lewispg228@users.noreply.github.com> Date: Wed, 23 Feb 2022 10:04:53 -0700 Subject: [PATCH 2/2] Formatting - adding tabs to inside of if statement --- Firmware/Qwiic_KeyPad/Qwiic_KeyPad.ino | 28 +++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Firmware/Qwiic_KeyPad/Qwiic_KeyPad.ino b/Firmware/Qwiic_KeyPad/Qwiic_KeyPad.ino index 98fc090..bbf0ad5 100644 --- a/Firmware/Qwiic_KeyPad/Qwiic_KeyPad.ino +++ b/Firmware/Qwiic_KeyPad/Qwiic_KeyPad.ino @@ -213,24 +213,24 @@ void receiveEvent(int numberOfBytesReceived) { if(numberOfBytesReceived > 0) // ensure we received valid byte or bytes - note, an I2C scan does not send anything, so without this, it would overright registerNumber with Zero. { - registerNumber = Wire.read(); //Get the memory map offset from the user + registerNumber = Wire.read(); //Get the memory map offset from the user - //Begin recording the following incoming bytes to the temp memory map - //starting at the registerNumber (the first byte received) - for (byte x = 0 ; x < numberOfBytesReceived - 1 ; x++) - { - byte temp = Wire.read(); //We might record it, we might throw it away - - if ( (x + registerNumber) < sizeof(memoryMap)) + //Begin recording the following incoming bytes to the temp memory map + //starting at the registerNumber (the first byte received) + for (byte x = 0 ; x < numberOfBytesReceived - 1 ; x++) { - //Clense the incoming byte against the read only protected bits - //Store the result into the register map - *(registerPointer + registerNumber + x) &= ~*(protectionPointer + registerNumber + x); //Clear this register if needed - *(registerPointer + registerNumber + x) |= temp & *(protectionPointer + registerNumber + x); //Or in the user's request (clensed against protection bits) + byte temp = Wire.read(); //We might record it, we might throw it away + + if ( (x + registerNumber) < sizeof(memoryMap)) + { + //Clense the incoming byte against the read only protected bits + //Store the result into the register map + *(registerPointer + registerNumber + x) &= ~*(protectionPointer + registerNumber + x); //Clear this register if needed + *(registerPointer + registerNumber + x) |= temp & *(protectionPointer + registerNumber + x); //Or in the user's request (clensed against protection bits) + } } - } - recordSystemSettings(); + recordSystemSettings(); } }