Skip to content

Commit

Permalink
Finalized
Browse files Browse the repository at this point in the history
  • Loading branch information
JLefebvre55 committed Oct 26, 2023
1 parent b0d810d commit 7b51489
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 74 deletions.
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=I2CIP
version=0.4.0
version=1.0.0
author=Jayden Lefebvre <jayden.lefebvre@peapodtech.com>
maintainer=Jayden Lefebvre <jayden.lefebvre@peapodtech.com>
sentence=A library of protocols for interfacing with I2C devices on a modular switched network.
paragraph=See README for compatible hardware specifications. Stateless for plug-and-play compatibility. Tested on Arduino Uno.
paragraph=See README for compatible hardware specifications. State management for plug-and-play compatibility. Tested on Arduino Nano.
category=Communication
url=https://github.com/PeaPodTechnologies/I2CIP/
architectures=avr
Expand Down
65 changes: 35 additions & 30 deletions src/I2CIP.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,29 @@ Module::~Module() {
}

DeviceGroup* Module::deviceGroupFactory(const i2cip_id_t& id) {
#ifdef I2CIP_DEBUG_SERIAL
DEBUG_DELAY();
I2CIP_DEBUG_SERIAL.print(F("-> Builtin EEPROM DeviceGroup Factory: Matching '"));
I2CIP_DEBUG_SERIAL.print(id);
I2CIP_DEBUG_SERIAL.print("':");
DEBUG_DELAY();
#endif
// #ifdef I2CIP_DEBUG_SERIAL
// DEBUG_DELAY();
// I2CIP_DEBUG_SERIAL.print(F("-> Builtin EEPROM DeviceGroup Factory: Matching '"));
// I2CIP_DEBUG_SERIAL.print(id);
// I2CIP_DEBUG_SERIAL.print("':");
// DEBUG_DELAY();
// #endif

if(id == EEPROM::getStaticIDBuffer() || strcmp(id, EEPROM::getStaticIDBuffer()) == 0) {
#ifdef I2CIP_DEBUG_SERIAL
DEBUG_DELAY();
I2CIP_DEBUG_SERIAL.print(F(" Match! Creating...\n"));
DEBUG_DELAY();
#endif
// #ifdef I2CIP_DEBUG_SERIAL
// DEBUG_DELAY();
// I2CIP_DEBUG_SERIAL.print(F(" Match! Creating...\n"));
// DEBUG_DELAY();
// #endif

return new DeviceGroup(EEPROM::getStaticIDBuffer(), EEPROM::eepromFactory);
}

#ifdef I2CIP_DEBUG_SERIAL
DEBUG_DELAY();
I2CIP_DEBUG_SERIAL.print(F(" EEPROM ID Mismatch!\n"));
DEBUG_DELAY();
#endif
// #ifdef I2CIP_DEBUG_SERIAL
// DEBUG_DELAY();
// I2CIP_DEBUG_SERIAL.print(F(" EEPROM ID Mismatch!\n"));
// DEBUG_DELAY();
// #endif

return nullptr;
}
Expand Down Expand Up @@ -247,22 +247,27 @@ bool Module::add(Device& device, bool overwrite) {
#endif
return false;
}

Device* d = entry->value[device.getFQA()];

if(dptr != nullptr && overwrite) {
#ifdef I2CIP_DEBUG_SERIAL
DEBUG_DELAY();
I2CIP_DEBUG_SERIAL.print(F("Overwriting Device.\n"));
DEBUG_DELAY();
#endif
if(d != nullptr) {
if(dptr != nullptr && overwrite) {
#ifdef I2CIP_DEBUG_SERIAL
DEBUG_DELAY();
I2CIP_DEBUG_SERIAL.print(F("Overwriting Device.\n"));
DEBUG_DELAY();
#endif

// Remove old device
entry->value.remove(*dptr);
this->devices_fqabst.remove((*dptr)->getFQA());
delete *dptr;
// Delete old device
this->remove(d, true);
} else {
// Was in HashTable but not BST
this->devices_fqabst.insert(fqa, d, true);
return false; // Invoke deletion
}
}

// Insert into BST (by pointer copy)
BSTNode<i2cip_fqa_t, Device*>* ptr = this->devices_fqabst.insert(fqa, &device, true);
BSTNode<i2cip_fqa_t, Device*>* ptr = this->devices_fqabst.insert(fqa, &device);
if(ptr == nullptr) {
#ifdef I2CIP_DEBUG_SERIAL
DEBUG_DELAY();
Expand Down Expand Up @@ -498,7 +503,7 @@ i2cip_errorlevel_t Module::operator()(const i2cip_fqa_t& fqa, bool update, bool
I2CIP_DEBUG_SERIAL.print(F("-> Device Not Found!\n"));
DEBUG_DELAY();
#endif
return I2CIP_ERR_HARD;
return I2CIP_ERR_SOFT;
}
i2cip_errorlevel_t errlev = device->pingTimeout();
// I2CIP_ERR_BREAK(errlev);
Expand Down
2 changes: 1 addition & 1 deletion src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <Arduino.h>

// Uncomment to enable debug
#define DEBUG 1
// #define DEBUG 1

// CROSS-LIBRARY DEBUG COMPATIBILITY
#ifdef DEBUG
Expand Down
4 changes: 2 additions & 2 deletions src/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
* @param lsb Position to insert (LSB)
* @param bits Number of bits to overwrite
**/
#define OVERWRITE_BITS(existing, data, lsb, bits) ((existing) & ~(((1 << (bits)) - 1) << (lsb)) | (((data) & ((1 << (bits)) - 1)) << (lsb)))
#define OVERWRITE_BITS(existing, data, lsb, bits) (((existing) & ~(((1 << (bits)) - 1) << (lsb))) | (((data) & ((1 << (bits)) - 1)) << (lsb)))

#define I2CIP_DEVICES_PER_GROUP ((size_t)16)
#define I2CIP_DEVICES_PER_GROUP ((size_t)4)
#define I2CIP_ID_SIZE ((size_t)10)

namespace I2CIP {
Expand Down
38 changes: 19 additions & 19 deletions src/device.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ template <typename G, typename A> G InputInterface<G, A>::getCache(void) const {
template <typename G, typename A> void InputInterface<G, A>::setCache(G value) { this->cache = value; }

template <typename G, typename A> void InputInterface<G, A>::clearCache(void) {
#ifdef I2CIP_DEBUG_SERIAL
DEBUG_DELAY();
I2CIP_DEBUG_SERIAL.print(F("InputInterface::clearCache() Not Implemented; Nothing Done\n"));
DEBUG_DELAY();
#endif
// #ifdef I2CIP_DEBUG_SERIAL
// DEBUG_DELAY();
// I2CIP_DEBUG_SERIAL.print(F("InputInterface::clearCache() Not Implemented; Nothing Done\n"));
// DEBUG_DELAY();
// #endif
}

template <typename G, typename A> void InputInterface<G, A>::setArgsA(A args) { this->argsA = args; }
Expand Down Expand Up @@ -62,15 +62,15 @@ template <typename G, typename A> i2cip_errorlevel_t InputInterface<G, A>::get(c

// If successful, update last cache
if(errlev == I2CIP::i2cip_errorlevel_t::I2CIP_ERR_NONE) {
#ifdef I2CIP_DEBUG_SERIAL
DEBUG_DELAY();
I2CIP_DEBUG_SERIAL.print(F(" -> "));
I2CIP_DEBUG_SERIAL.print(temp);
I2CIP_DEBUG_SERIAL.print(F(" @0x"));
I2CIP_DEBUG_SERIAL.print((uint16_t)temp, HEX);
I2CIP_DEBUG_SERIAL.print(F(", setting cache\n"));
DEBUG_DELAY();
#endif
// #ifdef I2CIP_DEBUG_SERIAL
// DEBUG_DELAY();
// I2CIP_DEBUG_SERIAL.print(F(" -> "));
// I2CIP_DEBUG_SERIAL.print(temp);
// I2CIP_DEBUG_SERIAL.print(F(" @0x"));
// I2CIP_DEBUG_SERIAL.print((uint16_t)temp, HEX);
// I2CIP_DEBUG_SERIAL.print(F(", setting cache\n"));
// DEBUG_DELAY();
// #endif
this->clearCache(); this->cache = temp; this->argsA = arg;
}
return errlev;
Expand All @@ -83,11 +83,11 @@ template <typename S, typename B> OutputInterface<S, B>::~OutputInterface() { }
template <typename S, typename B> void OutputInterface<S, B>::setValue(S value) { this->value = value; }

template <typename S, typename B> void OutputInterface<S, B>::resetFailsafe(void) {
#ifdef I2CIP_DEBUG_SERIAL
DEBUG_DELAY();
I2CIP_DEBUG_SERIAL.print(F("OutputInterface::resetFailsafe() Not Implemented; Nothing Done\n"));
DEBUG_DELAY();
#endif
// #ifdef I2CIP_DEBUG_SERIAL
// DEBUG_DELAY();
// I2CIP_DEBUG_SERIAL.print(F("OutputInterface::resetFailsafe() Not Implemented; Nothing Done\n"));
// DEBUG_DELAY();
// #endif
}

template <typename S, typename B> S OutputInterface<S, B>::getValue(void) const { return this->value; }
Expand Down
2 changes: 1 addition & 1 deletion src/eeprom.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#define I2CIP_EEPROM_TIMEOUT 100 // How long to wait for a write to complete (ms)

#define I2CIP_EEPROM_ID "24LC32"
#define I2CIP_EEPROM_DEFAULT "[{\"" I2CIP_EEPROM_ID "\":[80]}]"
#define I2CIP_EEPROM_DEFAULT "[{\"" I2CIP_EEPROM_ID "\":[80],\"SHT31\":[68]}]"

// Future-Proofing ;)
// namespace ControlSystemsOS {
Expand Down
46 changes: 27 additions & 19 deletions test/test_3_eeprom/test_3_eeprom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ size_t bufferlen = 0;

void test_device_oop(void) {
// Initialize EEPROM - Done after Serial.begin for debug output to work
eeprom = (EEPROM*)I2CIP::eepromFactory(eeprom_fqa);
eeprom = (EEPROM*)EEPROM::eepromFactory(eeprom_fqa);

TEST_ASSERT_TRUE_MESSAGE(eeprom != nullptr, "EEPROM Object Instantiation");

Expand Down Expand Up @@ -66,30 +66,35 @@ void test_eeprom_read_word(void) {
TEST_ASSERT_EQUAL_UINT8_MESSAGE(I2CIP_TEST_EEPROM_BYTE1, (c >> 8), "EEPROM Read Byte (Match 2/2)");
}

void test_eeprom_overwrite_contents(void) {
i2cip_errorlevel_t result = eeprom->overwriteContents(I2CIP::i2cip_eeprom_default);
TEST_ASSERT_EQUAL_UINT8_MESSAGE(I2CIP_ERR_NONE, result, "EEPROM Overwrite Contents");
}
// void test_eeprom_overwrite_contents(void) {
// i2cip_errorlevel_t result = eeprom->overwriteContents(I2CIP::i2cip_eeprom_default);
// TEST_ASSERT_EQUAL_UINT8_MESSAGE(I2CIP_ERR_NONE, result, "EEPROM Overwrite Contents");
// }

void test_eeprom_read_contents(void) {
i2cip_errorlevel_t result = eeprom->readContents((uint8_t*)buffer, bufferlen);
TEST_ASSERT_EQUAL_UINT8_MESSAGE(I2CIP_ERR_NONE, result, "EEPROM Read Contents");
TEST_ASSERT_NOT_EQUAL_MESSAGE(0, bufferlen, "EEPROM Read Contents (Empty)");
// void test_eeprom_read_contents(void) {
// i2cip_errorlevel_t result = eeprom->readContents((uint8_t*)buffer, bufferlen);
// TEST_ASSERT_EQUAL_UINT8_MESSAGE(I2CIP_ERR_NONE, result, "EEPROM Read Contents");
// TEST_ASSERT_NOT_EQUAL_MESSAGE(0, bufferlen, "EEPROM Read Contents (Empty)");

#ifdef I2CIP_TEST_EEPROM_OVERWRITE
TEST_ASSERT_EQUAL_STRING_MESSAGE(I2CIP::i2cip_eeprom_default, (char*)buffer, "EEPROM Read Contents (Match)");
#endif
}
// #ifdef I2CIP_TEST_EEPROM_OVERWRITE
// TEST_ASSERT_EQUAL_STRING_MESSAGE(I2CIP::i2cip_eeprom_default, (char*)buffer, "EEPROM Read Contents (Match)");
// #endif
// }

void test_device_io_default(void) {
i2cip_errorlevel_t result = ((Device*)eeprom)->get();
TEST_ASSERT_EQUAL_UINT8_MESSAGE(I2CIP_ERR_NONE, result, "EEPROM Input Getter (Default Args)");
#ifdef I2CIP_TEST_EEPROM_OVERWRITE
result = ((Device*)eeprom)->set();
result = eeprom->getOutput()->reset();
TEST_ASSERT_EQUAL_UINT8_MESSAGE(I2CIP_ERR_NONE, result, "EEPROM Output Setter (Default Value, Args)");
#endif
}

void test_device_delete(void) {
delete eeprom;
TEST_ASSERT_TRUE_MESSAGE(true, "EEPROM Deletion Fail");
}

// TODO: Test Device IO Repeatability in loop()
// (i.e. multiple calls to get() and set() without intermediate calls to resetCache())

Expand Down Expand Up @@ -118,12 +123,15 @@ void setup() {
RUN_TEST(test_device_io_default);
delay(1000);

#ifdef I2CIP_TEST_EEPROM_OVERWRITE
RUN_TEST(test_eeprom_overwrite_contents);
delay(2000);
#endif
// #ifdef I2CIP_TEST_EEPROM_OVERWRITE
// RUN_TEST(test_eeprom_overwrite_contents);
// delay(1000);
// #endif

RUN_TEST(test_eeprom_read_contents);
// RUN_TEST(test_eeprom_read_contents);
// delay(1000);

RUN_TEST(test_device_delete);
delay(2000);

UNITY_END();
Expand Down

0 comments on commit 7b51489

Please sign in to comment.