Skip to content

Commit

Permalink
Merge pull request #6 from aattww/general-fixes
Browse files Browse the repository at this point in the history
Various fixes and improvements:

* Fix and refine NTC sensor reading and calculations in NTCSensor.
* Fixed a possible memory allocation error in SensorsSRAMHandler.
* Update GPL to version 3 and bump copyright years to 2020.
  • Loading branch information
aattww committed Mar 31, 2020
2 parents 9c22916 + e1a80ab commit 7a78a7e
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 56 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Hardware is licensed under [Creative Commons Attribution-ShareAlike 4.0 Internat
Software is licensed under two different licenses:

* Internal libraries and example Python scripts are licensed under [MIT License](https://opensource.org/licenses/MIT).
* Everything else is licensed under [GNU General Public License, version 2](https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html).
* Everything else is licensed under [GNU General Public License, version 3](https://www.gnu.org/licenses/gpl-3.0.html).

See each source file for more specific information.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,12 @@ Start logging measurements to a MySQL database ([*save_modbus_to_db.py*](save_mo

# Version history

## v1.1.1 (2020-03-31)

* Fix and refine NTC sensor reading and calculations in NTCSensor.
* Fixed a possible memory allocation error in SensorsSRAMHandler.
* Update GPL to version 3 and bump copyright years to 2020.

## v1.1.0 (2020-02-15)

* Pulse 2 in gateways can be configured as an external interrupt (active low open drain) which informs upstream device of a new message. See gateway settings for more information.
Expand Down
14 changes: 7 additions & 7 deletions SensorsBattery/SensorsBattery.ino
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* Copyright (c) 2019 aattww (https://github.com/aattww/)
* Copyright (c) 2020 aattww (https://github.com/aattww/)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 as published by
* the Free Software Foundation.
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License, version 2 along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/*
Expand Down
14 changes: 7 additions & 7 deletions SensorsGateway/SensorsGateway.ino
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* Copyright (c) 2019 aattww (https://github.com/aattww/)
* Copyright (c) 2020 aattww (https://github.com/aattww/)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 as published by
* the Free Software Foundation.
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License, version 2 along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/*
Expand Down
14 changes: 7 additions & 7 deletions SensorsPulse/SensorsPulse.ino
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* Copyright (c) 2019 aattww (https://github.com/aattww/)
* Copyright (c) 2020 aattww (https://github.com/aattww/)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 as published by
* the Free Software Foundation.
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License, version 2 along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/*
Expand Down
25 changes: 15 additions & 10 deletions libraries/NTCSensor/NTCSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* MIT License
*
* Original work Copyright (c) 2019 Adafruit
* Modified work Copyright (c) 2019 aattww (https://github.com/aattww/)
* Modified work Copyright (c) 2020 aattww (https://github.com/aattww/)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -52,7 +52,10 @@
* Version history
* ---------------
*
* 1.1 2019-12-29 (CURRENT)
* 1.2 2020-03-15 (CURRENT)
* - Fixed a possible division by zero error if sensor is missing.
*
* 1.1 2019-12-29
* - Fixed a bug when enablePin was not in use but placeholder pin was still controlled.
*
* 1.0 2019-12-26
Expand Down Expand Up @@ -170,27 +173,29 @@ int16_t NTCSensor::readTemperature() {
delay(50);
}

float average = 0.0;
uint16_t rawValues = 0;

// Take 5 samples in a row for averaging
for (uint8_t i = 0; i < 5; i++) {
average += analogRead(_sensorPin);
rawValues += analogRead(_sensorPin);
delay(10);
}

// Turn off analog voltage if in use
if (_enablePin != NTC_NO_ENABLE_PIN) {
digitalWrite(_enablePin, LOW);
}

// Calculate average
average /= 5.0;

// In the unlikely event of measuring 0 resistance (sensor is shorted), return invalid value.
// This also prevents division by zero error below.
if (average == 0.0) {
// In the unlikely event of measuring almost 0 resistance (sensor is shorted),
// or almost 1023*5 (sensor is missing completely), return invalid value.
// This also prevents possible division by zero error below.
if ((rawValues <= 15) || (rawValues >= 1020*5)) {
return (-990);
}

// Calculate average
float average = rawValues / 5.0;


// Convert the value to resistance
average = 1023.0 / average - 1;
Expand Down
7 changes: 5 additions & 2 deletions libraries/NTCSensor/NTCSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* MIT License
*
* Original work Copyright (c) 2019 Adafruit
* Modified work Copyright (c) 2019 aattww (https://github.com/aattww/)
* Modified work Copyright (c) 2020 aattww (https://github.com/aattww/)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -52,7 +52,10 @@
* Version history
* ---------------
*
* 1.1 2019-12-29 (CURRENT)
* 1.2 2020-03-15 (CURRENT)
* - Fixed a possible division by zero error if sensor is missing.
*
* 1.1 2019-12-29
* - Fixed a bug when enablePin was not in use but placeholder pin was still controlled.
*
* 1.0 2019-12-26
Expand Down
2 changes: 1 addition & 1 deletion libraries/SensorsMemoryHandler/Sensors23K256Handler.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2019 aattww (https://github.com/aattww/)
* Copyright (c) 2020 aattww (https://github.com/aattww/)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion libraries/SensorsMemoryHandler/Sensors23K256Handler.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2019 aattww (https://github.com/aattww/)
* Copyright (c) 2020 aattww (https://github.com/aattww/)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion libraries/SensorsMemoryHandler/SensorsMemoryHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2019 aattww (https://github.com/aattww/)
* Copyright (c) 2020 aattww (https://github.com/aattww/)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion libraries/SensorsMemoryHandler/SensorsMemoryHandler.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2019 aattww (https://github.com/aattww/)
* Copyright (c) 2020 aattww (https://github.com/aattww/)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
50 changes: 39 additions & 11 deletions libraries/SensorsMemoryHandler/SensorsSRAMHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2019 aattww (https://github.com/aattww/)
* Copyright (c) 2020 aattww (https://github.com/aattww/)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -29,6 +29,17 @@
* This class handles memory by providing abstraction to save and restore node data.
*/

/*
* Version history
* ---------------
*
* 1.1 2020-03-15 (CURRENT)
* - Fixed a possible problem caused by not checking memory allocation for success.
*
* 1.0 2019-12-26
* Initial public release
*/

#include "SensorsSRAMHandler.h"

/*
Expand All @@ -40,13 +51,25 @@
*/
void SensorsSRAMHandler::init() {
for (uint8_t i = 0; i < POOL_CHUNKS; i++) {
// Allocate memory for chunks
// Allocate memory for a chunk
uint8_t* chunk = (uint8_t*)calloc(POOL_CHUNK_RAW_SIZE, sizeof(uint8_t));
_dataPool[i] = chunk;
// Check that memory was actually allocated
if (chunk != NULL) {
// Add chunk to pool
_dataPool[i] = chunk;
_nrOfChunks++;
}
// If failed to allocate new memory, no reason to keep trying
else {
break;
}
}

_freeChunks = POOL_CHUNKS;
_initialized = true;
// If we have allocated chunks
if (_nrOfChunks > 0) {
_freeChunks = _nrOfChunks;
_initialized = true;
}
}

/*
Expand All @@ -60,7 +83,7 @@ uint8_t* SensorsSRAMHandler::allocateDataChunk() {
return NULL;
}

for (uint8_t i = 0; i < POOL_CHUNKS; i++) {
for (uint8_t i = 0; i < _nrOfChunks; i++) {
// The first byte indicates node ID and can not be zero, meaning that chunk is currently not in use.
if (_dataPool[i][0] == 0) {
_freeChunks--;
Expand All @@ -84,7 +107,7 @@ void SensorsSRAMHandler::deallocateDataChunk(uint8_t* chunk) {
return;
}

for (uint8_t i = 0; i < POOL_CHUNKS; i++) {
for (uint8_t i = 0; i < _nrOfChunks; i++) {
if (_dataPool[i] == chunk) {
_freeChunks++;
// Clear the first byte (node ID) to indicate free chunk
Expand Down Expand Up @@ -130,7 +153,12 @@ uint8_t SensorsSRAMHandler::saveNodeData(uint8_t nodeId, uint8_t length, uint8_t
deleteNode(nodeId);

// Calculate how many chunks are needed for the amount to be saved
uint8_t neededChunks = ceil((double)length / POOL_CHUNK_DATA_SIZE);
uint8_t neededChunks = length / POOL_CHUNK_DATA_SIZE; // Floors division...
// ... therefore add one more chunk if needed
if ((neededChunks * POOL_CHUNK_DATA_SIZE) < length) {
neededChunks++;
}

uint8_t start = 0;
uint8_t end = 0;

Expand Down Expand Up @@ -177,7 +205,7 @@ uint8_t SensorsSRAMHandler::getNodeHeader(uint8_t nodeId) {
return 0;
}

for (uint8_t i = 0; i < POOL_CHUNKS; i++) {
for (uint8_t i = 0; i < _nrOfChunks; i++) {
if (_dataPool[i][0] == nodeId) {
// If this is the first chunk of node data (ie. it has the header)
if (_dataPool[i][1] == 0) {
Expand All @@ -204,7 +232,7 @@ void SensorsSRAMHandler::deleteNode(uint8_t nodeId) {
}

// Search every chunk associated to node ID
for (uint8_t i = 0; i < POOL_CHUNKS; i++) {
for (uint8_t i = 0; i < _nrOfChunks; i++) {
if (_dataPool[i][0] == nodeId) {
deallocateDataChunk(_dataPool[i]);
}
Expand Down Expand Up @@ -248,7 +276,7 @@ uint8_t SensorsSRAMHandler::getNodeData(uint8_t nodeId, uint8_t length, uint8_t*
uint8_t bytesWritten = 0;

for (uint8_t chunkNumber = 0; chunkNumber < neededChunks; chunkNumber++) {
for (uint8_t i = 0; i < POOL_CHUNKS; i++) {
for (uint8_t i = 0; i < _nrOfChunks; i++) {
if (_dataPool[i][0] == nodeId) {
if (_dataPool[i][1] == chunkNumber) {

Expand Down
18 changes: 15 additions & 3 deletions libraries/SensorsMemoryHandler/SensorsSRAMHandler.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2019 aattww (https://github.com/aattww/)
* Copyright (c) 2020 aattww (https://github.com/aattww/)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -29,6 +29,17 @@
* This class handles memory by providing abstraction to save and restore node data.
*/

/*
* Version history
* ---------------
*
* 1.1 2020-03-15 (CURRENT)
* - Fixed a possible problem caused by not checking memory allocation for success.
*
* 1.0 2019-12-26
* Initial public release
*/

#ifndef SENSORSSRAMHANDLER_H
#define SENSORSSRAMHANDLER_H

Expand Down Expand Up @@ -59,8 +70,9 @@ class SensorsSRAMHandler {
private:

uint8_t* _dataPool[POOL_CHUNKS]; // Memory pool for data
uint8_t _freeChunks = 0; // Number of free chunks
bool _initialized = false; // SRAM handler has been initialized
uint8_t _freeChunks = 0; // Number of free chunks
uint8_t _nrOfChunks = 0; // Number of chunks in total
bool _initialized = false; // SRAM handler has been initialized

/*
* Finds a free data chunk.
Expand Down
2 changes: 1 addition & 1 deletion libraries/SimpleModbusAsync/SimpleModbusAsync.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2019 aattww (https://github.com/aattww/)
* Copyright (c) 2020 aattww (https://github.com/aattww/)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion libraries/SimpleModbusAsync/SimpleModbusAsync.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2019 aattww (https://github.com/aattww/)
* Copyright (c) 2020 aattww (https://github.com/aattww/)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion read_modbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# MIT License
#
# Copyright (c) 2019 aattww (https://github.com/aattww/)
# Copyright (c) 2020 aattww (https://github.com/aattww/)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down
Loading

0 comments on commit 7a78a7e

Please sign in to comment.