From 61dc7f26e6a204e9516e0569f1a56111b78df8c6 Mon Sep 17 00:00:00 2001 From: susanw1 Date: Tue, 21 Nov 2023 19:34:16 +0000 Subject: [PATCH] [#143] I2cModule uses generated constants --- .../ZscriptFullInclude.hpp | 10 +- .../main/c++/arduino/i2c-module/I2cModule.hpp | 16 ++- .../i2c-module/channels/I2cChannel.hpp | 26 +--- .../i2c-module/commands/GeneralI2cAction.hpp | 133 ++++++++---------- .../commands/I2cCapabilitiesCommand.hpp | 27 ++-- .../i2c-module/commands/I2cChannelInfo.hpp | 24 ++-- .../i2c-module/commands/I2cChannelSetup.hpp | 15 +- .../i2c-module/commands/I2cSetupCommand.hpp | 66 ++++----- .../notifications/I2cNotification.hpp | 5 + 9 files changed, 140 insertions(+), 182 deletions(-) diff --git a/receivers/native/arduino/src/main/c++/arduino-converter-files/ZscriptFullInclude.hpp b/receivers/native/arduino/src/main/c++/arduino-converter-files/ZscriptFullInclude.hpp index d3098b9d1..1b85ad614 100644 --- a/receivers/native/arduino/src/main/c++/arduino-converter-files/ZscriptFullInclude.hpp +++ b/receivers/native/arduino/src/main/c++/arduino-converter-files/ZscriptFullInclude.hpp @@ -118,8 +118,8 @@ class ArduinoZscriptBasicSetup { #endif #ifdef ZSCRIPT_HAVE_I2C_CHANNEL - if (Zscript::I2cChannel::getNotifChannelPersistMaxLength() > notifPersistLength) { - notifPersistLength = Zscript::I2cChannel::getNotifChannelPersistMaxLength(); + if (Zscript::i2c_module::I2cChannel::getNotifChannelPersistMaxLength() > notifPersistLength) { + notifPersistLength = Zscript::i2c_module::I2cChannel::getNotifChannelPersistMaxLength(); } #endif #ifdef ZSCRIPT_HAVE_UART_CHANNEL @@ -139,10 +139,10 @@ class ArduinoZscriptBasicSetup { #endif uint8_t i = 0; #if defined(ZSCRIPT_HAVE_I2C_MODULE) || defined(ZSCRIPT_HAVE_I2C_CHANNEL) - Zscript::I2cModule::setup(); + Zscript::i2c_module::I2cModule::setup(); #endif #ifdef ZSCRIPT_HAVE_I2C_CHANNEL - channels[i++] = &Zscript::I2cModule::channel; + channels[i++] = &Zscript::i2c_module::I2cModule::channel; #endif #if defined(ZSCRIPT_HAVE_UART_MODULE) || defined(ZSCRIPT_HAVE_UART_CHANNEL) @@ -170,7 +170,7 @@ class ArduinoZscriptBasicSetup { #ifdef ZSCRIPT_SUPPORT_NOTIFICATIONS uint8_t srcCount = 0; #ifdef ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS - notifSrcs[srcCount++] = &Zscript::I2cModule::notifSrc; + notifSrcs[srcCount++] = &Zscript::i2c_module::I2cModule::notifSrc; #endif #ifdef ZSCRIPT_PIN_SUPPORT_NOTIFICATIONS notifSrcs[srcCount++] = &Zscript::pins_module::PinModule::notificationSource; diff --git a/receivers/native/arduino/src/main/c++/arduino/i2c-module/I2cModule.hpp b/receivers/native/arduino/src/main/c++/arduino/i2c-module/I2cModule.hpp index eacbfa35f..46796108e 100644 --- a/receivers/native/arduino/src/main/c++/arduino/i2c-module/I2cModule.hpp +++ b/receivers/native/arduino/src/main/c++/arduino/i2c-module/I2cModule.hpp @@ -36,10 +36,14 @@ namespace Zscript { +namespace i2c_module { + template class I2cChannel; } +} + #include "commands/I2cChannelInfo.hpp" #include "commands/I2cChannelSetup.hpp" @@ -59,19 +63,21 @@ class I2cChannel; #endif #define MODULE_EXISTS_005 EXISTENCE_MARKER_UTIL -#define MODULE_SWITCH_005 MODULE_SWITCH_UTIL(I2cModule::execute) +#define MODULE_SWITCH_005 MODULE_SWITCH_UTIL(i2c_module::I2cModule::execute) #ifdef ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS # ifdef ZSCRIPT_SUPPORT_ADDRESSING # define MODULE_ADDRESS_EXISTS_005 EXISTENCE_MARKER_UTIL -# define MODULE_ADDRESS_SWITCH_005 ADDRESS_SWITCH_UTIL(I2cModule::address) +# define MODULE_ADDRESS_SWITCH_005 ADDRESS_SWITCH_UTIL(i2c_module::I2cModule::address) # endif # define MODULE_NOTIFICATION_EXISTS_005 EXISTENCE_MARKER_UTIL -# define MODULE_NOTIFICATION_SWITCH_005 NOTIFICATION_SWITCH_UTIL(I2cModule::notification) +# define MODULE_NOTIFICATION_SWITCH_005 NOTIFICATION_SWITCH_UTIL(i2c_module::I2cModule::notification) #endif namespace Zscript { +namespace i2c_module { + template class I2cModule : public ZscriptModule { #ifdef ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS @@ -88,7 +94,7 @@ class I2cModule : public ZscriptModule { public: #ifdef ZSCRIPT_HAVE_I2C_CHANNEL - static I2cChannel channel; + static I2cChannel channel; #endif @@ -193,4 +199,6 @@ GenericCore::ZscriptNotificationSource I2cModule::notifSrc; #endif } +} + #endif /* SRC_MAIN_CPP_ARDUINO_I2C_MODULE_ZSCRIPTI2CMODULE_HPP_ */ diff --git a/receivers/native/arduino/src/main/c++/arduino/i2c-module/channels/I2cChannel.hpp b/receivers/native/arduino/src/main/c++/arduino/i2c-module/channels/I2cChannel.hpp index 3c178d95b..b9737fe09 100644 --- a/receivers/native/arduino/src/main/c++/arduino/i2c-module/channels/I2cChannel.hpp +++ b/receivers/native/arduino/src/main/c++/arduino/i2c-module/channels/I2cChannel.hpp @@ -16,6 +16,8 @@ namespace Zscript { +namespace i2c_module { + template class I2cOutStream : public AbstractOutStream { public: @@ -230,28 +232,6 @@ class I2cChannel : public ZscriptChannel { } } } - -// void channelInfo(ZscriptCommandContext ctx) { -// CommandOutStream ctxOut = ctx.getOutStream(); -// ctxOut.writeField('N', 0); -// ctxOut.writeField('M', 0x5); -// } -// -// void channelSetup(ZscriptCommandContext ctx) { -// uint16_t addr; -// if (ctx.getField('A', &addr)) { -// if (addr >= 128) { -// ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE); -// return; -// } -// out.setAddr((uint8_t) addr); -// } -// if (ctx.hasField('P')) { -// uint8_t index = this->parser.getChannelIndex(); -// PersistenceSystem::writeSection(PersistenceSystem::getNotifChannelIdOffset(), 1, &index); -// } -// } - }; template @@ -271,4 +251,6 @@ bool I2cChannel::usingTmp = false; } +} + #endif /* SRC_MAIN_CPP_ARDUINO_I2C_MODULE_CHANNELS_ZSCRIPTI2CCHANNEL_HPP_ */ diff --git a/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/GeneralI2cAction.hpp b/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/GeneralI2cAction.hpp index fa08107f8..10ec91a40 100644 --- a/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/GeneralI2cAction.hpp +++ b/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/GeneralI2cAction.hpp @@ -9,34 +9,26 @@ #define SRC_MAIN_CPP_ARDUINO_I2C_MODULE_COMMANDS_GENERALI2CACTION_HPP_ #include +#include #include #define COMMAND_EXISTS_0052 EXISTENCE_MARKER_UTIL #define COMMAND_EXISTS_0053 EXISTENCE_MARKER_UTIL #define COMMAND_EXISTS_0054 EXISTENCE_MARKER_UTIL + /** * Handles the Send, Receive, and Send+Receive cases directly - they're pretty similar */ namespace Zscript { -template -class GeneralI2cAction { - static constexpr char ParamInterface__I = 'I'; - static constexpr char ParamAddr__A = 'A'; - static constexpr char ParamTenBit__T = 'T'; - static constexpr char ParamRetries__R = 'R'; - static constexpr char ParamReadLength__L = 'L'; - static constexpr char RespRetries__R = 'R'; - static constexpr char RespResultType__I = 'I'; - static constexpr char RespResultType__OK = 0; - static constexpr char RespResultType__ADDRNACK = 2; - static constexpr char RespResultType__DATANACK = 3; - static constexpr char RespResultType__OTHER = 4; +namespace i2c_module { +template +class GeneralI2cAction: I2cSendReceive_CommandDefs { public: - static constexpr uint8_t SEND_CODE = 0x02; - static constexpr uint8_t RECEIVE_CODE = 0x03; - static constexpr uint8_t SEND_RECEIVE_CODE = 0x04; + static constexpr uint8_t SEND_CODE = I2cSend_CommandDefs::CODE; + static constexpr uint8_t RECEIVE_CODE = I2cReceive_CommandDefs::CODE; + static constexpr uint8_t SEND_RECEIVE_CODE = I2cSendReceive_CommandDefs::CODE; /** Note, SEND | RECEIVE == SEND_RECEIVE */ enum class ActionType : uint8_t { @@ -45,11 +37,11 @@ class GeneralI2cAction { static void executeSendReceive(ZscriptCommandContext ctx, ActionType action) { uint16_t address; - if (!ctx.getField(ParamAddr__A, &address)) { + if (!ctx.getField(ReqAddress__A, &address)) { ctx.status(ResponseStatus::MISSING_KEY); return; } - if (ctx.hasField(ParamTenBit__T)) { + if (ctx.hasField(ReqTenBitMode__T)) { ctx.status(ResponseStatus::EXECUTION_ERROR); return; } @@ -60,7 +52,7 @@ class GeneralI2cAction { CommandOutStream out = ctx.getOutStream(); - uint16_t requestedLength = ctx.getField(ParamReadLength__L, (uint16_t) 0); + uint16_t requestedLength = ctx.getField(ReqLength__L, (uint16_t) 0); if (requestedLength > 32) { ctx.status(ResponseStatus::DATA_TOO_LONG); return; @@ -68,14 +60,11 @@ class GeneralI2cAction { // initial pass uint16_t interface; - if (!ctx.getField(ParamInterface__I, &interface)) { - interface = 0; - } else if (interface > 0) { - ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE); + if (!ctx.getReqdFieldCheckLimit(ReqInterface__I, 1, &interface)) { return; } - uint16_t retries = ctx.getField(ParamRetries__R, 5); + uint16_t retries = ctx.getField(ReqRetries__R, 5); if (retries > 255) { ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE); return; @@ -94,44 +83,20 @@ class GeneralI2cAction { uint8_t buffer[requestedLength]; switch ((ActionType) action) { - case ActionType::SEND: - Wire.beginTransmission((uint8_t) address); - if (ctx.getBigFieldSize() > 0) { - for (BigFieldBlockIterator tbi = ctx.getBigField(); tbi.hasNext();) { - DataArrayWLeng16 data = tbi.nextContiguous(); - Wire.write(data.data, data.length); - } - } - status = Wire.endTransmission(); - break; - case ActionType::RECEIVE: - { - uint8_t len = Wire.requestFrom((uint8_t) address, (uint8_t) requestedLength); - if (len == 0) { - status = 2; - } else if (len != requestedLength) { - status = 4; - } else { - status = 0; - for (uint8_t i = 0; i < requestedLength; i++) { - buffer[i] = (uint8_t) Wire.read(); - } - } - } - break; - case ActionType::SEND_RECEIVE: - Wire.beginTransmission((uint8_t) address); - if (ctx.getBigFieldSize() > 0) { - for (BigFieldBlockIterator tbi = ctx.getBigField(); tbi.hasNext();) { - DataArrayWLeng16 data = tbi.nextContiguous(); - Wire.write(data.data, data.length); + case ActionType::SEND: + Wire.beginTransmission((uint8_t) address); + if (ctx.getBigFieldSize() > 0) { + for (BigFieldBlockIterator tbi = ctx.getBigField(); tbi.hasNext();) { + DataArrayWLeng16 data = tbi.nextContiguous(); + Wire.write(data.data, data.length); + } } - } - status = Wire.endTransmission(false); - if (status == 0) { + status = Wire.endTransmission(); + break; + case ActionType::RECEIVE: { uint8_t len = Wire.requestFrom((uint8_t) address, (uint8_t) requestedLength); if (len == 0) { - status = 6; + status = 2; } else if (len != requestedLength) { status = 4; } else { @@ -140,52 +105,76 @@ class GeneralI2cAction { buffer[i] = (uint8_t) Wire.read(); } } - break; } - break; + break; + case ActionType::SEND_RECEIVE: + Wire.beginTransmission((uint8_t) address); + if (ctx.getBigFieldSize() > 0) { + for (BigFieldBlockIterator tbi = ctx.getBigField(); tbi.hasNext();) { + DataArrayWLeng16 data = tbi.nextContiguous(); + Wire.write(data.data, data.length); + } + } + status = Wire.endTransmission(false); + if (status == 0) { + uint8_t len = Wire.requestFrom((uint8_t) address, (uint8_t) requestedLength); + if (len == 0) { + status = 6; + } else if (len != requestedLength) { + status = 4; + } else { + status = 0; + for (uint8_t i = 0; i < requestedLength; i++) { + buffer[i] = (uint8_t) Wire.read(); + } + } + break; + } + break; } if (status == 0) { - infoValue = RespResultType__OK; + infoValue = RespResultInfo_Values::ok_value; if (((uint8_t) action & (uint8_t) ActionType::RECEIVE) != 0) { out.writeBigHex(buffer, requestedLength); } break; } else if (status == 3) { // abrupt failure during data send - don't retry, because its status is now unknown - infoValue = RespResultType__DATANACK; + infoValue = RespResultInfo_Values::dataNack_value; ctx.status(ResponseStatus::COMMAND_DATA_NOT_TRANSMITTED); break; } else if (status == 5 || status == 4) { if (recoverBusJam()) { - infoValue = RespResultType__OTHER; + infoValue = RespResultInfo_Values::other_value; ctx.status(ResponseStatus::PERIPHERAL_JAM); } else { - infoValue = RespResultType__OTHER; + infoValue = RespResultInfo_Values::other_value; ctx.status(ResponseStatus::COMMAND_FAIL); } break; } else if (status == 6 && action == ActionType::SEND_RECEIVE) { - infoValue = RespResultType__ADDRNACK; + infoValue = RespResultInfo_Values::addrNack_value; ctx.status(ResponseStatus::COMMAND_NO_TARGET); break; } else if (attemptsDone < retries) { // any other error, keep retrying. Beyond here, we must be at the end of all retries. } else if (status == 2) { - infoValue = RespResultType__ADDRNACK; + infoValue = RespResultInfo_Values::addrNack_value; ctx.status(ResponseStatus::COMMAND_NO_TARGET); break; } else { - infoValue = RespResultType__OTHER; + infoValue = RespResultInfo_Values::other_value; ctx.status(ResponseStatus::COMMAND_FAIL); break; } } - out.writeField(RespRetries__R, attemptsDone); - out.writeField(RespResultType__I, infoValue); + out.writeField(RespAttempts__R, attemptsDone); + out.writeField(RespResultInfo__I, infoValue); return; } + #ifdef ZSCRIPT_SUPPORT_ADDRESSING static void executeAddressing(ZscriptAddressingContext ctx) { uint16_t interface; @@ -249,7 +238,7 @@ class GeneralI2cAction { pinMode(SDA, INPUT_PULLUP); pinMode(SCL, INPUT_PULLUP); - if (digitalRead (SDA)) { + if (digitalRead(SDA)) { if (!digitalRead(SCL)) { Wire.begin(); return false; @@ -279,4 +268,6 @@ class GeneralI2cAction { } +} + #endif /* SRC_MAIN_CPP_ARDUINO_I2C_MODULE_COMMANDS_GENERALI2CACTION_HPP_ */ diff --git a/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cCapabilitiesCommand.hpp b/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cCapabilitiesCommand.hpp index fd27dbb30..fec158ba1 100644 --- a/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cCapabilitiesCommand.hpp +++ b/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cCapabilitiesCommand.hpp @@ -9,35 +9,26 @@ #define SRC_MAIN_CPP_ARDUINO_I2C_MODULE_COMMANDS_ZSCRIPTI2CCAPABILITIESCOMMAND_HPP_ #include +#include #include #define COMMAND_EXISTS_0050 EXISTENCE_MARKER_UTIL namespace Zscript { -template -class ZscriptI2cCapabilitiesCommand { -public: - static constexpr uint8_t CODE = 0x00; - static constexpr char RespCommands__C = 'C'; - static constexpr char RespNotificationsSupported__N = 'N'; - static constexpr char RespAddressingSupported__A = 'A'; - static constexpr char RespInterfaceCount__I = 'I'; - static constexpr char RespFrequenciesSupported__F = 'F'; - static constexpr char RespBitsetCapabilities__B = 'B'; - - static constexpr uint16_t RespBitsetCapabilities__LowSpeedSupported = 0x1; - static constexpr uint16_t RespBitsetCapabilities__BusFreeSupported = 0x2; - static constexpr uint16_t RespBitsetCapabilities__SmBusAddressResolution = 0x4; - static constexpr uint16_t RespBitsetCapabilities__TenBit = 0x8; +namespace i2c_module { +template +class ZscriptI2cCapabilitiesCommand : public Capabilities_CommandDefs { +public: static void execute(ZscriptCommandContext ctx) { CommandOutStream out = ctx.getOutStream(); - out.writeField(RespCommands__C, MODULE_CAPABILITIES(005)); + out.writeField(RespCommandsSet__C, MODULE_CAPABILITIES(005)); out.writeField(RespInterfaceCount__I, 1); out.writeField(RespFrequenciesSupported__F, 3); #ifdef ZSCRIPT_HAVE_I2C_MODULE - out.writeField(RespBitsetCapabilities__B, RespBitsetCapabilities__LowSpeedSupported | RespBitsetCapabilities__SmBusAddressResolution); + out.writeField(RespBitsetCapabilities__B, RespBitsetCapabilities_Values::lowSpeedSupported_field + | RespBitsetCapabilities_Values::smBusAddressResolution_field); #else out.writeField(RespBitsetCapabilities__B, RespBitsetCapabilities__LowSpeedSupported); #endif @@ -52,4 +43,6 @@ class ZscriptI2cCapabilitiesCommand { } +} + #endif /* SRC_MAIN_CPP_ARDUINO_I2C_MODULE_COMMANDS_ZSCRIPTI2CCAPABILITIESCOMMAND_HPP_ */ diff --git a/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cChannelInfo.hpp b/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cChannelInfo.hpp index 2e0f6be63..12cfccac4 100644 --- a/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cChannelInfo.hpp +++ b/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cChannelInfo.hpp @@ -17,34 +17,26 @@ namespace Zscript { template -class I2cModule; +class ZscriptChannel; + +namespace i2c_module { template -class I2cChannel; +class I2cModule; template -class ZscriptChannel; +class I2cChannel; template -class ZscriptI2cChannelInfoCommand { +class ZscriptI2cChannelInfoCommand: public ChannelInfo_CommandDefs { public: - static constexpr uint8_t CODE = 0x0c; - - static constexpr char ReqChannel__C = 'C'; - - static constexpr char RespChannelCount__N = 'N'; - static constexpr char RespChannelGlobal__C = 'C'; - static constexpr char RespAddress__A = 'A'; - static constexpr char RespInterface__I = 'I'; - - static void execute(ZscriptCommandContext ctx) { uint16_t channelIndex; if (!ctx.getReqdFieldCheckLimit(ReqChannel__C, Zscript::zscript.getChannelCount(), &channelIndex)) { return; } ZscriptChannel *selectedChannel = Zscript::zscript.getChannels()[channelIndex]; - if (selectedChannel->getAssociatedModule() != i2c_module::MODULE_FULL_ID) { + if (selectedChannel->getAssociatedModule() != MODULE_FULL_ID) { ctx.status(ResponseStatus::VALUE_UNSUPPORTED); return; } @@ -58,4 +50,6 @@ class ZscriptI2cChannelInfoCommand { } +} + #endif //SRC_MAIN_CPP_ARDUINO_I2C_MODULE_COMMANDS_ZSCRIPTI2CCHANNELINFOCOMMAND_HPP_ diff --git a/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cChannelSetup.hpp b/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cChannelSetup.hpp index 2143270a7..3ac5718d3 100644 --- a/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cChannelSetup.hpp +++ b/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cChannelSetup.hpp @@ -16,6 +16,8 @@ namespace Zscript { +namespace i2c_module { + template class I2cChannel; @@ -23,21 +25,15 @@ template class I2cModule; template -class ZscriptI2cChannelSetupCommand { +class ZscriptI2cChannelSetupCommand: public ChannelSetup_CommandDefs { public: - static constexpr uint8_t CODE = 0x0d; - - static constexpr char ReqChannel__C = 'C'; - static constexpr char ReqAddress__A = 'A'; - - static void execute(ZscriptCommandContext ctx) { uint16_t channelIndex; if (!ctx.getReqdFieldCheckLimit(ReqChannel__C, Zscript::zscript.getChannelCount(), &channelIndex)) { return; } ZscriptChannel *selectedChannel = Zscript::zscript.getChannels()[channelIndex]; - if (selectedChannel->getAssociatedModule() != i2c_module::MODULE_FULL_ID) { + if (selectedChannel->getAssociatedModule() != MODULE_FULL_ID) { ctx.status(ResponseStatus::VALUE_UNSUPPORTED); return; } @@ -54,4 +50,7 @@ class ZscriptI2cChannelSetupCommand { }; } + +} + #endif //SRC_MAIN_CPP_ARDUINO_I2C_MODULE_COMMANDS_ZSCRIPTI2CCHANNELSETUPCOMMAND_HPP_ diff --git a/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cSetupCommand.hpp b/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cSetupCommand.hpp index 4e8c9c2e0..6ad12b9b2 100644 --- a/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cSetupCommand.hpp +++ b/receivers/native/arduino/src/main/c++/arduino/i2c-module/commands/I2cSetupCommand.hpp @@ -14,66 +14,50 @@ #define COMMAND_EXISTS_0051 EXISTENCE_MARKER_UTIL namespace Zscript { -template -class ZscriptI2cSetupCommand { -public: - static constexpr uint8_t CODE = 0x01; - - // if set, determines which port to control; else set all ports - static constexpr char ParamInterface__I = 'I'; - // chooses comms frequency, from 10, 100, 400 and 1000 kHz - static constexpr char ParamFreq__F = 'F'; - // If present, sets whether addressing is enabled - static constexpr char ParamAddressingEnable__A = 'A'; - // If present, sets whether notifications are enabled - static constexpr char ParamNotificationEnable__N = 'N'; - static constexpr char RespFreqkHz__K = 'K'; +namespace i2c_module { +template +class ZscriptI2cSetupCommand: public I2cSetup_CommandDefs { +public: static void execute(ZscriptCommandContext ctx - #ifdef ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS , bool *addressing, bool *notifications #endif - ) { + ) { uint16_t freq; - if (ctx.getField(ParamFreq__F, &freq)) { + if (ctx.getField(ReqFrequency__F, &freq)) { uint32_t freqValue; switch (freq) { - case 0: - freqValue = 10000; - break; - case 1: - freqValue = 100000; - break; - case 2: - freqValue = 400000; - break; - case 3: - freqValue = 1000000; - break; - default: - ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE); - return; + case 0: + freqValue = 10000; + break; + case 1: + freqValue = 100000; + break; + case 2: + freqValue = 400000; + break; + case 3: + freqValue = 1000000; + break; + default: + ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE); + return; } uint16_t interface; - if (!ctx.getField(ParamInterface__I, &interface)) { - ctx.status(ResponseStatus::MISSING_KEY); - return; - } - if (interface != 0) { - ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE); + if (!ctx.getReqdFieldCheckLimit(ReqInterface__I, 1, &interface)) { return; } Wire.setClock(freqValue); - ctx.getOutStream().writeField(RespFreqkHz__K, freqValue / 1000); + ctx.getOutStream().writeField(RespFrequencykHz__K, freqValue / 1000); } #ifdef ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS uint16_t notif; - if (ctx.getField(ParamNotificationEnable__N, ¬if)) { + if (ctx.getField(ReqNotifications__N, ¬if)) { *notifications = (notif != 0); } #ifdef ZSCRIPT_SUPPORT_ADDRESSING @@ -88,4 +72,6 @@ class ZscriptI2cSetupCommand { } +} + #endif /* SRC_MAIN_CPP_ARDUINO_I2C_MODULE_COMMANDS_ZSCRIPTI2CSETUPCOMMAND_HPP_ */ diff --git a/receivers/native/arduino/src/main/c++/arduino/i2c-module/notifications/I2cNotification.hpp b/receivers/native/arduino/src/main/c++/arduino/i2c-module/notifications/I2cNotification.hpp index f6013dd6c..1370fb2f8 100644 --- a/receivers/native/arduino/src/main/c++/arduino/i2c-module/notifications/I2cNotification.hpp +++ b/receivers/native/arduino/src/main/c++/arduino/i2c-module/notifications/I2cNotification.hpp @@ -14,6 +14,8 @@ namespace Zscript { +namespace i2c_module { + template class ZscriptI2cNotification { static const uint8_t SMBUS_ALERT_ADDR = 0xC; @@ -77,5 +79,8 @@ class ZscriptI2cNotification { #endif } }; + +} + } #endif //SRC_MAIN_CPP_ARDUINO_I2C_MODULE_NOTIFICATIONS_ZSCRIPTI2CNOTIFICATION_HPP_ \ No newline at end of file