Skip to content

Commit

Permalink
fix #25 buffer overflow examples (#26)
Browse files Browse the repository at this point in the history
- fix #25 - buffer overflow in examples
- update keywords.txt
- moved code to .cpp
- minor edits
  • Loading branch information
RobTillaart authored Jul 19, 2023
1 parent cdfd212 commit d183ea2
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 50 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.3.6] - 2023-07-19
- fix #25 - buffer overflow in examples
- update keywords.txt
- moved code to .cpp
- minor edits


## [0.3.5] - 2023-01-17
- update GitHub actions
- update license
- fix #23 add note about RP2040
- add **MCP4725_MIDPOINT** constant
- minor edits


## [0.3.4] - 2022-10-17
- Add RP2040 support (kudos to intubun)
- Add RP2040 in build-CI
Expand Down
15 changes: 14 additions & 1 deletion MCP4725.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// FILE: MCP4725.cpp
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for 12 bit I2C DAC - MCP4725
// VERSION: 0.3.5
// VERSION: 0.3.6
// URL: https://github.com/RobTillaart/MCP4725
//
// HISTORY see changelog.md
Expand Down Expand Up @@ -111,6 +111,12 @@ int MCP4725::setPercentage(float percentage)
}


float MCP4725::getPercentage()
{
return getValue() * (100.0 / MCP4725_MAXVALUE);
};


// unfortunately it is not possible to write a different value
// to the DAC and EEPROM simultaneously or write EEPROM only.
int MCP4725::writeDAC(const uint16_t value, const bool EEPROM)
Expand Down Expand Up @@ -147,6 +153,12 @@ uint16_t MCP4725::readEEPROM()
}


uint32_t MCP4725::getLastWriteEEPROM()
{
return _lastWriteEEPROM;
};


// depending on bool EEPROM the value of PDM is written to
// (false) DAC or
// (true) DAC & EEPROM,
Expand Down Expand Up @@ -198,6 +210,7 @@ int MCP4725::powerOnWakeUp()
return rv;
}


// PAGE 18 DATASHEET
int MCP4725::_writeFastMode(const uint16_t value)
{
Expand Down
8 changes: 4 additions & 4 deletions MCP4725.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// FILE: MCP4725.h
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for 12 bit I2C DAC - MCP4725
// VERSION: 0.3.5
// VERSION: 0.3.6
// URL: https://github.com/RobTillaart/MCP4725
//

Expand All @@ -12,7 +12,7 @@
#include "Arduino.h"


#define MCP4725_VERSION (F("0.3.5"))
#define MCP4725_VERSION (F("0.3.6"))


// CONSTANTS
Expand Down Expand Up @@ -62,14 +62,14 @@ class MCP4725

// 0..100.0% - no input check.
int setPercentage(float percentage = 0);
float getPercentage() { return getValue() * (100.0 / MCP4725_MAXVALUE); };
float getPercentage();


int writeDAC(const uint16_t value, const bool EEPROM = false);
bool ready();
uint32_t getLastWriteEEPROM() { return _lastWriteEEPROM; };
uint16_t readDAC();
uint16_t readEEPROM();
uint32_t getLastWriteEEPROM();


// experimental
Expand Down
46 changes: 25 additions & 21 deletions examples/MCP4725_wave_generator/MCP4725_wave_generator.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,33 @@
#include "MCP4725.h"
#include "Wire.h"

uint16_t freq = 100;
uint32_t period = 0;
uint32_t halvePeriod = 0;

// q = square
// s = sinus
// w = sawtooth
// t = stair
// r = random
uint16_t freq = 100;
uint32_t period = 0;
uint32_t halvePeriod = 0;


// q = square z = zero
// s = sinus m = mid
// w = sawtooth h = high
// t = stair
// r = random
char mode = 'q';


MCP4725 MCP(0x63);
uint16_t count;
uint32_t lastTime = 0;

// LOOKUP TABLE SINE
uint16_t sine[360];

// LOOKUP TABLE SINE
uint16_t sine[361];


void setup()
{
Serial.begin(115200);

// fill table
// fill table
for (int i = 0; i < 361; i++)
{
sine[i] = 2047 + round(2047 * sin(i * PI / 180));
Expand Down Expand Up @@ -72,7 +75,8 @@ void setup()
if (now - lastTime > 100000)
{
lastTime = now;
// Serial.println(count); // show # updates per 0.1 second
// show # updates per 0.1 second
// Serial.println(count);
count = 0;
if (Serial.available())
{
Expand Down Expand Up @@ -144,23 +148,23 @@ void setup()
case 'r':
MCP.setValue(random(4096));
break;
case 'z': // zero
case 'z': // zero
MCP.setValue(0);
break;
case 'h': // high
case 'h': // high
MCP.setValue(4095);
break;
case 'm': // mid
case 'm': // mid
MCP.setValue(2047);
break;
default:
case 's':
// reference
// float f = ((PI * 2) * t)/period;
// MCP.setValue(2047 + 2047 * sin(f));
// reference
// float f = ((PI * 2) * t)/period;
// MCP.setValue(2047 + 2047 * sin(f));
//
int idx = (360 * t) / period;
MCP.setValue(sine[idx]); // lookuptable
MCP.setValue(sine[idx]); // fetch from lookup table
break;
}
}
Expand All @@ -172,5 +176,5 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --

Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,26 @@
#include "MCP4725.h"
#include "Wire.h"

uint16_t freq = 100;
uint32_t period = 0;
uint32_t halvePeriod = 0;

// q = square
// s = sinus
// w = sawtooth
// t = stair
// r = random
uint16_t freq = 100;
uint32_t period = 0;
uint32_t halvePeriod = 0;


// q = square z = zero
// s = sinus m = mid
// w = sawtooth h = high
// t = stair
// r = random
char mode = 'q';


MCP4725 MCP(0x63, &Wire1);
uint16_t count;
uint32_t lastTime = 0;

// LOOKUP TABLE SINE
uint16_t sine[360];

// LOOKUP TABLE SINE
uint16_t sine[361];


void setup()
Expand Down Expand Up @@ -68,7 +71,8 @@ void setup()
if (now - lastTime > 100000)
{
lastTime = now;
// Serial.println(count); // show # updates per 0.1 second
// show # updates per 0.1 second
// Serial.println(count);
count = 0;
if (Serial.available())
{
Expand Down Expand Up @@ -156,7 +160,7 @@ void setup()
// MCP.setValue(2047 + 2047 * sin(f));
//
int idx = (360 * t) / period;
MCP.setValue(sine[idx]); // lookuptable
MCP.setValue(sine[idx]); // fetch from lookup table
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/mcp4725_isConnected/mcp4725_isConnected.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
// DATE: 2020-12-26
// URL: https://github.com/RobTillaart/MCP4725
//
// test to see behaviour when sensor is not connected and reconnected again. E.g. loose wires..
// test to see behaviour when sensor is not connected and reconnected again. E.g. loose wires..


#include "Wire.h"
#include "MCP4725.h"

MCP4725 MCP(0x62); // 0x62 or 0x63
MCP4725 MCP(0x62); // 0x62 or 0x63

bool connected = false;

Expand Down Expand Up @@ -48,5 +48,5 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --

8 changes: 4 additions & 4 deletions examples/smooth2Value/smooth2Value.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "Wire.h"
#include "MCP4725.h"

MCP4725 MCP(0x62); // 0x62 or 0x63
MCP4725 MCP(0x62); // 0x62 or 0x63


void setup()
Expand Down Expand Up @@ -57,7 +57,7 @@ void setup()

void loop()
{
// different gradients
// different gradients
smooth2Value(4095, 4096);
smooth2Value(0, 4096);
smooth2Value(4095, 2048);
Expand All @@ -81,10 +81,10 @@ int smooth2Value(uint16_t value, uint16_t steps)
MCP.setValue( round(startValue + i * delta) );
}
}
// get the end value right
// get the end value right
return MCP.setValue(value);
}


// -- END OF FILE --
// -- END OF FILE --

5 changes: 4 additions & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ MCP4725 KEYWORD1
# Methods and Functions (KEYWORD2)
begin KEYWORD2
isConnected KEYWORD2
read KEYWORD2

setValue KEYWORD2
getValue KEYWORD2
Expand All @@ -34,9 +33,13 @@ powerOnWakeUp KEYWORD2

# Constants (LITERAL1)
MCP4725_VERSION LITERAL1

MCP4725_MAXVALUE LITERAL1

MCP4725_OK LITERAL1
MCP4725_VALUE_ERROR LITERAL1
MCP4725_REG_ERROR LITERAL1
MCP4725_NOT_CONNECTED LITERAL1

MCP4725_PDMODE_NORMAL LITERAL1
MCP4725_PDMODE_1K LITERAL1
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/MCP4725.git"
},
"version": "0.3.5",
"version": "0.3.6",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=MCP4725
version=0.3.5
version=0.3.6
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for 12 bit I2C DAC - MCP4725
Expand Down

0 comments on commit d183ea2

Please sign in to comment.