Skip to content

Commit

Permalink
[#149] I2C Module updated to new include scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
susanw1 committed Oct 9, 2024
1 parent c8a38be commit aabb781
Show file tree
Hide file tree
Showing 20 changed files with 497 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//#define ZSCRIPT_PIN_SUPPORT_NOTIFICATIONS
//#define ZSCRIPT_PIN_SUPPORT_ANALOG_NOTIFICATIONS

//#define ZSCRIPT_HAVE_I2C_MODULE
//#define ZSCRIPT_HAVE_I2C_GENERAL
//#define ZSCRIPT_HAVE_I2C_CHANNEL
//#define ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
#include "arduino/pins-module/PinModule.hpp"
#endif

#if defined(ZSCRIPT_HAVE_I2C_MODULE) || defined(ZSCRIPT_HAVE_I2C_CHANNEL)
#include "arduino/i2c-module/I2cModule.hpp"
#endif
#include "arduino/i2c-module/I2cModule_all.hpp"


#if defined(ZSCRIPT_HAVE_UART_MODULE) || defined(ZSCRIPT_HAVE_UART_CHANNEL)
Expand All @@ -47,9 +45,7 @@

// INCLUDE ALL CHANNELS BELOW HERE!

#if defined(ZSCRIPT_HAVE_I2C_CHANNEL)
#include <arduino/i2c-module/channels/I2cChannel.hpp>
#endif
#include <arduino/i2c-module/channels/I2cChannel.hpp>

#if defined(ZSCRIPT_HAVE_UART_CHANNEL)
#include "arduino/uart-module/channels/UartChannel.hpp"
Expand Down Expand Up @@ -127,7 +123,7 @@ class ArduinoZscriptBasicSetup {
Zscript::servo_module::ZscriptServoModule<ZscriptParams>::setup();
#endif
uint8_t i = 0;
#if defined(ZSCRIPT_HAVE_I2C_MODULE) || defined(ZSCRIPT_HAVE_I2C_CHANNEL)
#if defined(ZSCRIPT_HAVE_I2C_GENERAL) || defined(ZSCRIPT_HAVE_I2C_CHANNEL)
Zscript::i2c_module::I2cModule<ZscriptParams>::setup();
#endif
#if defined(ZSCRIPT_HAVE_I2C_CHANNEL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
* SPDX-License-Identifier: MIT
*/

#ifndef SRC_MAIN_CPP_ARDUINO_ARDUINO_CORE_MODULE_COMMANDS_ZSCRIPTRESETCOMMAND_HPP_
#define SRC_MAIN_CPP_ARDUINO_ARDUINO_CORE_MODULE_COMMANDS_ZSCRIPTRESETCOMMAND_HPP_
#if defined(SRC_ZSCRIPT_BASE_RESET_COMMAND)
#error This file should not be included more than once
#endif
#define SRC_ZSCRIPT_BASE_RESET_COMMAND

#include <zscript/modules/ZscriptCommand.hpp>
#include <net/zscript/model/modules/base/OuterCoreModule.hpp>
Expand All @@ -32,4 +34,3 @@ class ZscriptResetCommand: public Reset_CommandDefs {

#define ZSCRIPT_RESET_COMMAND

#endif /* SRC_MAIN_CPP_ARDUINO_ARDUINO_CORE_MODULE_COMMANDS_ZSCRIPTRESETCOMMAND_HPP_ */

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,50 @@
* SPDX-License-Identifier: MIT
*/

#ifndef SRC_MAIN_CPP_ARDUINO_I2C_MODULE_ZSCRIPTI2CMODULE_HPP_
#define SRC_MAIN_CPP_ARDUINO_I2C_MODULE_ZSCRIPTI2CMODULE_HPP_


#ifdef ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS
#define ZSCRIPT_HAVE_I2C_MODULE
#endif

#include <zscript/modules/ZscriptModule.hpp>
#include <zscript/execution/ZscriptCommandContext.hpp>

#if defined(ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS)
#if defined(ZSCRIPT_SUPPORT_ADDRESSING)
#include <zscript/execution/ZscriptAddressingContext.hpp>
#endif

#if defined(ZSCRIPT_SUPPORT_NOTIFICATIONS)
#include <zscript/notifications/ZscriptNotificationSource.hpp>
#include <zscript/execution/ZscriptNotificationContext.hpp>
#include "notifications/I2cNotification.hpp"
#endif
#endif

#if defined(ZSCRIPT_HAVE_I2C_CHANNEL)
#include "commands/I2cChannelInfo.hpp"
#include "commands/I2cChannelSetup.hpp"
#endif

#if defined(ZSCRIPT_HAVE_I2C_MODULE)
#include "commands/I2cSetupCommand.hpp"
#include "commands/GeneralI2cAction.hpp"
#if defined(SRC_ZSCRIPT_BASE_I2C_MODULE)
#error I2C Module definition should not be included more than once
#endif

#include "commands/I2cCapabilitiesCommand.hpp"
#define SRC_ZSCRIPT_BASE_I2C_MODULE

#ifdef ZSCRIPT_HPP_INCLUDED
#error Must be included before Zscript.hpp
#endif

#include <zscript/modules/ZscriptModule.hpp>
#include <net/zscript/model/modules/base/I2cModule.hpp>
#include <Wire.h>

#define MODULE_EXISTS_005 EXISTENCE_MARKER_UTIL
#define MODULE_SWITCH_005 MODULE_SWITCH_UTIL(i2c_module::I2cModule<ZP>::execute)

#if defined(ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS)
#if defined(ZSCRIPT_SUPPORT_ADDRESSING)
#define MODULE_ADDRESS_EXISTS_005 EXISTENCE_MARKER_UTIL
#define MODULE_ADDRESS_SWITCH_005 ADDRESS_SWITCH_UTIL(i2c_module::I2cModule<ZP>::address)
#endif
// Notifications
#include <arduino/i2c-module/notifications/I2cNotification.hpp>

#define MODULE_NOTIFICATION_EXISTS_005 EXISTENCE_MARKER_UTIL
#define MODULE_NOTIFICATION_SWITCH_005 NOTIFICATION_SWITCH_UTIL(i2c_module::I2cModule<ZP>::notification)

// Addressing
#if defined(ZSCRIPT_SUPPORT_ADDRESSING)
#include <zscript/execution/ZscriptAddressingContext.hpp>
#define MODULE_ADDRESS_EXISTS_005 EXISTENCE_MARKER_UTIL
#define MODULE_ADDRESS_SWITCH_005 ADDRESS_SWITCH_UTIL(i2c_module::I2cModule<ZP>::address)
#endif

// Channel commands
#include <arduino/i2c-module/commands/I2cChannelInfo.hpp>
#include <arduino/i2c-module/commands/I2cChannelSetup.hpp>

// General commands
#include <arduino/i2c-module/commands/GeneralI2cAction.hpp>
#include <arduino/i2c-module/commands/I2cSetupCommand.hpp>
#include <arduino/i2c-module/commands/I2cCapabilitiesCommand.hpp>

namespace Zscript {

namespace i2c_module {

template<class ZP>
class I2cModule : public ZscriptModule<ZP> {
#if defined(ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS)
static bool isAddressing;
static bool giveNotifs;

Expand All @@ -72,53 +58,32 @@ class I2cModule : public ZscriptModule<ZP> {
}
}

#endif

public:
#if defined(ZSCRIPT_HAVE_I2C_CHANNEL)
static I2cChannel<ZP> channel;
#endif


#if defined(ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS)
static GenericCore::ZscriptNotificationSource<ZP> notifSrc;
#endif

static void setup() {
#if defined(ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS)
pinMode(ZP::i2cAlertInPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ZP::i2cAlertInPin), &smBusAlertReceived, FALLING);
pinMode(ZP::i2cAlertInPin, INPUT_PULLUP);
#endif
#if defined(ZSCRIPT_HAVE_I2C_CHANNEL)

channel.setup();
channel.setAddress(ZscriptParams::i2cChannelAddress);
#else
Wire.begin();
#endif
}

static void poll() {
#if defined(ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS)
if (digitalRead(ZP::i2cAlertInPin) == LOW) {
smBusAlertReceived();
}
#endif
}

static void execute(ZscriptCommandContext<ZP> ctx, uint8_t bottomBits) {
switch (bottomBits) {
case ZscriptI2cCapabilitiesCommand<ZP>::CODE:
ZscriptI2cCapabilitiesCommand<ZP>::execute(ctx, MODULE_CAPABILITIES(005));
break;
#if defined(ZSCRIPT_HAVE_I2C_MODULE)
case ZscriptI2cSetupCommand<ZP>::CODE:
ZscriptI2cSetupCommand<ZP>::execute(ctx

#if defined(ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS)
, &isAddressing, &giveNotifs
#endif
);
ZscriptI2cSetupCommand<ZP>::execute(ctx, &isAddressing, &giveNotifs);
break;
case GeneralI2cAction<ZP>::SEND_CODE:
GeneralI2cAction<ZP>::executeSendReceive(ctx, GeneralI2cAction<ZP>::ActionType::SEND);
Expand All @@ -129,44 +94,36 @@ class I2cModule : public ZscriptModule<ZP> {
case GeneralI2cAction<ZP>::SEND_RECEIVE_CODE:
GeneralI2cAction<ZP>::executeSendReceive(ctx, GeneralI2cAction<ZP>::ActionType::SEND_RECEIVE);
break;
#endif
#if defined(ZSCRIPT_HAVE_I2C_CHANNEL)
case ZscriptI2cChannelInfoCommand<ZP>::CODE:
ZscriptI2cChannelInfoCommand<ZP>::execute(ctx);
break;
case ZscriptI2cChannelSetupCommand<ZP>::CODE:
ZscriptI2cChannelSetupCommand<ZP>::execute(ctx);
break;
#endif
default:
ctx.status(ResponseStatus::COMMAND_NOT_FOUND);
break;
}
}

#if defined(ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS)
#if defined(ZSCRIPT_SUPPORT_ADDRESSING)
#if defined(ZSCRIPT_SUPPORT_ADDRESSING)
static void address(ZscriptAddressingContext<ZP> ctx) {
if (!isAddressing) {
ctx.status(ResponseStatus::ADDRESS_NOT_FOUND);
return;
}
GeneralI2cAction<ZP>::executeAddressing(ctx);
}
#endif
#endif

static void notification(ZscriptNotificationContext<ZP> ctx, bool moveAlong) {
ZscriptI2cNotification<ZP>::notification(ctx, moveAlong, isAddressing);
}
#endif
};

#if defined(ZSCRIPT_HAVE_I2C_CHANNEL)
template<class ZP>
I2cChannel<ZP> I2cModule<ZP>::channel;
#endif

#if defined(ZSCRIPT_I2C_SUPPORT_NOTIFICATIONS)
template<class ZP>
bool I2cModule<ZP>::isAddressing = false;

Expand All @@ -175,9 +132,8 @@ bool I2cModule<ZP>::giveNotifs = false;

template<class ZP>
GenericCore::ZscriptNotificationSource<ZP> I2cModule<ZP>::notifSrc;
#endif

}

}

#endif /* SRC_MAIN_CPP_ARDUINO_I2C_MODULE_ZSCRIPTI2CMODULE_HPP_ */
Loading

0 comments on commit aabb781

Please sign in to comment.