Skip to content

Commit

Permalink
Added function to return module slot properties (#20)
Browse files Browse the repository at this point in the history
Add readSlotProps function and related example
  • Loading branch information
bbbowden authored Jun 12, 2023
1 parent 19f81e0 commit 8edef52
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
93 changes: 93 additions & 0 deletions examples/Utility/slotProperties/slotProperties.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Example: Slot Properties
This example shows how to access the module properties at a given slot.
You can then use the module properties in your code without needing to know
what is in the slot at compile time. This example uses readBlockData to efficiently
scan through the inputs for changes.
This example works with all P1000 Series:
- Discrete input modules such as P1-08SIM, P1-08ND3, P1-08NA, etc
- Discrete combo modules such as P1-15CDD1, P1-15CDD2, P1-16CDR, etc.
This example will detect the change of any input and then print the byte, bit, and value.
_____ _____ _____
| P || S || S |
| 1 || L || L |
| A || O || O |
| M || T || T |
| - || || |
| 1 || 0 || 0 |
| 0 || 1 || 2 |
| 0 || || | etc...
¯¯¯¯¯ ¯¯¯¯¯ ¯¯¯¯¯
Written by Brett Bowden and Adam Cummick
Licensed under the MIT license.
*/


#include <P1AM.h>

uint8_t digitalInputBytes = 0;
uint8_t digitalOutputBytes = 0;
uint8_t analogInputBytes = 0;
uint8_t analogOutputBytes = 0;

uint8_t dataInputArray[30] = {0};
uint8_t dataInputArray_old[30] = {0};

uint32_t checkInputsTimer = 0;

void setup(){

Serial.begin(115200);
while (!Serial) {}// wait for serial port to connect.

uint8_t numberOfModules = 0;
while(numberOfModules == 0) {
numberOfModules = P1.init();
}

for (uint8_t slotIndex = 1; slotIndex <= numberOfModules; slotIndex++) {
moduleProps slotProps = P1.readSlotProps(slotIndex);
Serial.print("Slot: "); Serial.print(slotIndex); Serial.print(" Module: "); Serial.println(slotProps.moduleName);
Serial.print("Digital Input Bytes: "); Serial.println(slotProps.diBytes, DEC);
Serial.print("Digital Output Bytes: "); Serial.println(slotProps.doBytes, DEC);
Serial.print("Analog Input Bytes: "); Serial.println(slotProps.aiBytes, DEC);
Serial.print("Analog Output Bytes: "); Serial.println(slotProps.aoBytes, DEC);
Serial.println();

digitalInputBytes += (uint8_t)slotProps.diBytes;
digitalOutputBytes += (uint8_t)slotProps.doBytes;
analogInputBytes += (uint8_t)slotProps.aiBytes;
analogOutputBytes += (uint8_t)slotProps.aoBytes;
}

Serial.print("Total Digital Input Bytes: "); Serial.println(digitalInputBytes);
Serial.print("Total Digital Output Bytes: "); Serial.println(digitalOutputBytes);
Serial.print("Total Analog Input Bytes: "); Serial.println(analogInputBytes);
Serial.print("Total Analog Output Bytes: "); Serial.println(analogOutputBytes);

P1.readBlockData((char*)dataInputArray, (uint16_t)digitalInputBytes, 0, DISCRETE_IN_BLOCK); // Read all inputs
memcpy(dataInputArray_old, dataInputArray, sizeof(dataInputArray)); // Initialize with current readings

}

void loop(){
if (millis() - checkInputsTimer > 10UL) { // Check inputs every 10ms
P1.readBlockData((char*)dataInputArray, (uint16_t)digitalInputBytes, 0, DISCRETE_IN_BLOCK); // Read all inputs
checkInputsTimer = millis();

for (uint8_t i = 0; i < digitalInputBytes; i++) {
if (dataInputArray[i] != dataInputArray_old[i]) {
for (uint8_t bitIndex = 0; bitIndex < 8; bitIndex++) {
if (bitRead(dataInputArray[i], bitIndex) != bitRead(dataInputArray_old[i], bitIndex)) {
Serial.print("DI Byte: "); Serial.print(i);
Serial.print(" Bit: "); Serial.print(bitIndex);
Serial.print(" Changed to: "); Serial.println(bitRead(dataInputArray[i], bitIndex));
}
}
dataInputArray_old[i] = dataInputArray[i];
}
}
}
}
31 changes: 31 additions & 0 deletions src/P1AM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,37 @@ uint8_t P1AM::printModules(){
return goodSlots;
}


/*******************************************************************************
Description: Return the module properties for a given slot
Parameters: -uint8_t slot - Slot of which you want the module parameters.
Slot 1 is module closest to processor.
Returns: -moduleProps - moduleProps structure containing the specific paramters
at the give slot.
*******************************************************************************/
moduleProps P1AM::readSlotProps(uint8_t slot){

uint8_t dbLoc = 0;
moduleProps _slotProps;

if((slot < 1) || (slot > 15)){
debugPrintln("Slots must be between 1 and 15");
_slotProps = mdb[0];
return _slotProps;
}

if (baseSlot[slot-1].dbLoc != 0) {
dbLoc = baseSlot[slot-1].dbLoc;
_slotProps = mdb[dbLoc];
}
else {
_slotProps = mdb[0];
}
return _slotProps;
}

/*******************************************************************************
Description: Read a single status byte from a single module
Expand Down
1 change: 1 addition & 0 deletions src/P1AM.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class P1AM{
bool isBaseActive(); //Check if Base Controller is currently configured. If not, call init.
uint8_t checkConnection(uint8_t numberOfModules = 0); //Checks the modules to see if a connection has been lost. Returns first missing module.
bool Base_Controller_FW_UPDATE(unsigned int fwLen); //For FW update of Base Controller
moduleProps readSlotProps(uint8_t slot); //Returns the module properties at the given slot location.

//Label functions - functionally the same as the above Data IO but use the channelLabel datatype for easier to read code.
uint32_t readDiscrete(channelLabel label);
Expand Down

0 comments on commit 8edef52

Please sign in to comment.