Skip to content

Commit

Permalink
General updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Apr 4, 2024
1 parent 42232e3 commit 15d2700
Show file tree
Hide file tree
Showing 88 changed files with 1,325 additions and 1,040 deletions.
23 changes: 12 additions & 11 deletions Sming/Libraries/CS5460/samples/generic/app/application.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
#include <SmingCore.h>
#include <CS5460.h>

namespace
{
CS5460 powerMeter(PIN_NDEFINED, PIN_NDEFINED, PIN_NDEFINED, PIN_NDEFINED);

Timer printVoltageTimer;
SimpleTimer printVoltageTimer;

void printVoltage()
{
debugf("Measured RMS voltage is: %f", powerMeter.getRMSVoltage());
Serial << _F("Measured RMS voltage is: ") << powerMeter.getRMSVoltage() << endl;
}

} // namespace

void init()
{
Serial.begin(SERIAL_BAUD_RATE, SERIAL_8N1,
SERIAL_FULL); // 115200 by default, GPIO1,GPIO3, see Serial.swap(), HardwareSerial
Serial.begin(SERIAL_BAUD_RATE);
Serial.systemDebugOutput(true);

powerMeter.init();
powerMeter.setCurrentGain(190.84); //0.25 / shunt (0.00131)
powerMeter.setVoltageGain(500); //0.25V (Veff max) * dividerGain
uint32_t conf = 0;
conf = powerMeter.readRegister(CONFIG_REGISTER);
conf |= ENABLE_VOLTAGE_HPF;
conf |= ENABLE_CURRENT_HPF;
powerMeter.setCurrentGain(190.84); // 0.25 / shunt (0.00131)
powerMeter.setVoltageGain(500); // 0.25V (Veff max) * dividerGain

uint32_t conf = powerMeter.readRegister(CONFIG_REGISTER);
conf |= ENABLE_VOLTAGE_HPF | ENABLE_CURRENT_HPF;
powerMeter.writeRegister(CONFIG_REGISTER, conf);
powerMeter.startMultiConvert();

Expand Down
47 changes: 25 additions & 22 deletions Sming/Libraries/ModbusMaster/samples/generic/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@
#define MB_SLAVE_ADDR 1
#define SLAVE_REG_ADDR 1

Timer mbLoopTimer;

namespace
{
SimpleTimer mbLoopTimer;
ModbusMaster mbMaster;
HardwareSerial modbusComPort(UART_ID_0);
HardwareSerial debugComPort(UART_ID_1);

uint16_t globalSeconds = 0;
uint16_t globalSeconds;

void mbLoop()
{
globalSeconds++;

uint8_t numberOfRegistersToWrite = 1;
uint8_t bufferPosition = 0;
const uint8_t numberOfRegistersToWrite = 1;
const uint8_t bufferPosition = 0;
mbMaster.begin(MB_SLAVE_ADDR, modbusComPort);
mbMaster.setTransmitBuffer(bufferPosition, globalSeconds);
mbMaster.writeMultipleRegisters(SLAVE_REG_ADDR, numberOfRegistersToWrite);

uint8_t nrOfRegistersToRead = 1;
uint8_t mbResult = mbMaster.readHoldingRegisters(SLAVE_REG_ADDR, nrOfRegistersToRead); //see also readInputRegisters
// see also readInputRegisters
const uint8_t nrOfRegistersToRead = 1;
uint8_t mbResult = mbMaster.readHoldingRegisters(SLAVE_REG_ADDR, nrOfRegistersToRead);

if(mbResult == mbMaster.ku8MBSuccess) {
/*
Expand All @@ -34,9 +36,9 @@ void mbLoop()
debugComPort.printf("Reg %d: %d\r\n", i, buffer[i]);
}
*/
debugf("Data from slave: %d", mbMaster.getResponseBuffer(0));
debug_i("Data from slave: %d", mbMaster.getResponseBuffer(0));
} else {
debugf("Res err: %d", mbResult);
debug_i("Res err: %d", mbResult);
}

mbMaster.clearResponseBuffer();
Expand All @@ -57,51 +59,52 @@ void mbLogReceive(const uint8_t* adu, size_t aduSize, uint8_t status)
if(status != mbMaster.ku8MBSuccess) {
switch(status) {
case mbMaster.ku8MBIllegalFunction:
debugf("MB Illegal Function");
debug_i("MB Illegal Function");
break;
case mbMaster.ku8MBIllegalDataAddress:
debugf("MB Illegal Address");
debug_i("MB Illegal Address");
break;
case mbMaster.ku8MBIllegalDataValue:
debugf("MB Illegal Data Value");
debug_i("MB Illegal Data Value");
break;
case mbMaster.ku8MBSlaveDeviceFailure:
debugf("MB Slave Device Failure");
debug_i("MB Slave Device Failure");
break;
case mbMaster.ku8MBInvalidSlaveID:
debugf("MB Invalid Slave ID");
debug_i("MB Invalid Slave ID");
break;
case mbMaster.ku8MBInvalidFunction:
debugf("MB Invalid function");
debug_i("MB Invalid function");
break;
case mbMaster.ku8MBResponseTimedOut:
debugf("MB Response Timeout");
debug_i("MB Response Timeout");
break;
case mbMaster.ku8MBInvalidCRC:
debugf("MB Invalid CRC");
debug_i("MB Invalid CRC");
break;
case mbMaster.ku8MBResponseTooLarge:
debugf("MB Response too large");
debug_i("MB Response too large");
break;
}
debugf("ADU Size: %d, status: %d ", aduSize, status);
debug_i("ADU Size: %d, status: %d ", aduSize, status);
debug_hex(INFO, "RX ADU", adu, aduSize);
}
debugf("\r\n");
debug_i("\r\n");
}

void mbLogTransmit(const uint8_t* adu, size_t aduSize)
{
debug_hex(INFO, "TX ADU", adu, aduSize);
}

} // namespace

void init()
{
pinMode(RS485_RE_PIN, OUTPUT);
digitalWrite(RS485_RE_PIN, LOW);
modbusComPort.begin(MODBUS_COM_SPEED, SERIAL_8N1, SERIAL_FULL);
debugComPort.begin(SERIAL_BAUD_RATE, SERIAL_8N1,
SERIAL_TX_ONLY); // 115200 by default, GPIO1,GPIO3, see Serial.swap(), HardwareSerial
debugComPort.begin(SERIAL_BAUD_RATE, SERIAL_8N1, SERIAL_TX_ONLY);
debugComPort.systemDebugOutput(true);

mbMaster.preTransmission(preTransmission);
Expand Down
3 changes: 2 additions & 1 deletion samples/Accel_Gyro_MPU6050/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ void init()

Wire.begin(DEFAULT_SDA_PIN, DEFAULT_SCL_PIN);
mpu.initialize();
Serial.println(mpu.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
bool success = mpu.testConnection();
Serial << _F("MPU6050 connection ") << (success ? _F("successful") : _F("failed")) << endl;

mainLoopTimer.initializeMs<mainLoopInterval>(mainLoop).start();
}
4 changes: 2 additions & 2 deletions samples/Accelerometer_MMA7455/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

// For more information read: https://code.google.com/p/mma-7455-arduino-library/
MMA_7455 accel;
Timer procTimer;
SimpleTimer procTimer;

void readSensor()
{
Serial.println("Reading..");
Serial.println(_F("Reading.."));

int8_t x = accel.readAxis('x');
int8_t y = accel.readAxis('y');
Expand Down
18 changes: 11 additions & 7 deletions samples/Basic_APA102/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

#define SPI_CS 2

Timer procTimer;
namespace
{
SimpleTimer procTimer;

// in this demo, the same ports for HW and SW SPI are used
#ifdef _USE_SOFTSPI
Expand All @@ -34,12 +36,12 @@ APA102 LED(NUM_LED); // APA102 constructor, call with number of LEDs
//APA102 LED(NUM_LED, SPI);
#endif

static SPISettings SPI_1MHZ = SPISettings(1000000, MSBFIRST, SPI_MODE3);
static SPISettings SPI_2MHZ = SPISettings(2000000, MSBFIRST, SPI_MODE3);
SPISettings SPI_1MHZ{1000000, MSBFIRST, SPI_MODE3};
SPISettings SPI_2MHZ{2000000, MSBFIRST, SPI_MODE3};

/* color wheel function:
* (simple) three 120° shifted colors -> color transitions r-g-b-r */
static col_t colorWheel(uint16_t step, uint16_t numStep)
col_t colorWheel(uint16_t step, uint16_t numStep)
{
col_t col = {0};
col.br = 10;
Expand All @@ -63,10 +65,10 @@ static col_t colorWheel(uint16_t step, uint16_t numStep)
return col;
}

static void updateLED()
void updateLED()
{
static unsigned state = 0;
static unsigned cnt = 0;
static unsigned state;
static unsigned cnt;

switch(state++) {
case 0:
Expand Down Expand Up @@ -113,6 +115,8 @@ static void updateLED()
}
}

} // namespace

void init()
{
Serial.begin(SERIAL_BAUD_RATE);
Expand Down
8 changes: 4 additions & 4 deletions samples/Basic_AWS/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ void startMqttClient()
Url url;
url.Scheme = URI_SCHEME_MQTT_SECURE;
url.Host = awsEndpoint;
mqtt.connect(url, "Basic_AWS");
mqtt.connect(url, F("Basic_AWS"));

// Assign a disconnect callback function
// mqtt.setCompleteDelegate(checkMQTTDisconnect);
mqtt.subscribe("thing/fish/test");
mqtt.subscribe(F("thing/fish/test"));
}

// Publish our message
Expand All @@ -41,8 +41,8 @@ void publishMessage()
startMqttClient();
}

Serial.println("publish message");
mqtt.publish("version", "ver.1.2");
Serial.println(_F("publish message"));
mqtt.publish(F("version"), F("ver.1.2"));
}

// Callback for messages, arrived from MQTT server
Expand Down
48 changes: 30 additions & 18 deletions samples/Basic_Audio/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
// If using an external DAC, set this to 0 - you'll probably need to change other settings as well
#define ENABLE_DELTA_SIGMA

namespace
{
// Set this to 0 to output fixed values for easier checking on a scope or signal analyzer
//#define GENERATE_FIXED_VALUES
constexpr i2s_sample_t fixedSampleValue = {0xF55F0AA0};
Expand All @@ -30,17 +32,17 @@ constexpr unsigned targetSampleRate = 44100;
constexpr float sineWaveFrequency = 440;

// Measure time taken to fill the I2S DMA buffers
static Profiling::MicroTimes fillTime("Fill Time");
Profiling::MicroTimes fillTime("Fill Time");

// Measure time taken between I2S callback (interrupt) and our task callback getting executed
static Profiling::MicroTimes callbackLatency("Callback latency");
Profiling::MicroTimes callbackLatency("Callback latency");

// Report status periodically
static SimpleTimer statusTimer;
static constexpr unsigned statusIntervalMs = 5000;
SimpleTimer statusTimer;
constexpr unsigned statusIntervalMs = 5000;

// One full sine-wave cycle
static struct {
struct SineWaveTable {
std::unique_ptr<uint16_t[]> samples;
unsigned sampleCount = 0;
unsigned readPos = 0;
Expand Down Expand Up @@ -81,7 +83,24 @@ static struct {
}
return value;
}
} sineWaveTable;
};

SineWaveTable sineWaveTable;

#ifdef GENERATE_FIXED_VALUES

void writeFixedValues()
{
i2s_buffer_info_t info;
while(i2s_dma_write(&info, UINT_MAX)) {
memset(info.samples, 0, info.size);
// for(unsigned i = 0; i < info.size / sizeof(i2s_sample_t); ++i) {
// info.samples[i] = fixedSampleValue;
// }
}
}

#else

/*
* Outputs a 172.266Hz sine wave (256 samples at 44100 samples/sec)
Expand All @@ -105,16 +124,7 @@ void writeSine()
}
}

void writeFixedValues()
{
i2s_buffer_info_t info;
while(i2s_dma_write(&info, UINT_MAX)) {
memset(info.samples, 0, info.size);
// for(unsigned i = 0; i < info.size / sizeof(i2s_sample_t); ++i) {
// info.samples[i] = fixedSampleValue;
// }
}
}
#endif // GENERATE_FIXED_VALUES

void fillBuffers()
{
Expand All @@ -128,7 +138,7 @@ void fillBuffers()
fillTime.update();
}

static void checkReceive()
void checkReceive()
{
unsigned total = 0;
i2s_buffer_info_t info;
Expand All @@ -154,7 +164,7 @@ void IRAM_ATTR i2sCallback(void* param, i2s_event_type_t event)
}
}

static void initialiseI2S()
void initialiseI2S()
{
i2s_config_t config;
memset(&config, 0, sizeof(config));
Expand Down Expand Up @@ -231,6 +241,8 @@ static void initialiseI2S()
i2s_start();
}

} // namespace

void init()
{
/*
Expand Down
2 changes: 1 addition & 1 deletion samples/Basic_Blink/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define LED_PIN 2 // GPIO2
#endif

Timer procTimer;
SimpleTimer procTimer;
bool state = true;

void blink()
Expand Down
15 changes: 11 additions & 4 deletions samples/Basic_Capsense/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@
// clock speed on the ESP means we need a higher charge current than arduino ??
// Further investigation required.

CapacitiveSensor cs_0_2 = CapacitiveSensor(0, 2); //Send pin 0, Receive Pin 2.
#define PIN_SEND 0
#define PIN_RECEIVE 2
#define SAMPLES_TO_READ 30

Timer procTimer;
namespace
{
CapacitiveSensor cs_0_2(PIN_SEND, PIN_RECEIVE);
SimpleTimer procTimer;

void capsense()
{
long total = cs_0_2.capacitiveSensor(30); //Read sensor with 30 samples
Serial << _F("Sense Value: ") << total << endl; // print sensor output
long total = cs_0_2.capacitiveSensor(SAMPLES_TO_READ);
Serial << _F("Sense Value: ") << total << endl;
}

} // namespace

void init()
{
procTimer.initializeMs<100>(capsense).start();
Expand Down
Loading

0 comments on commit 15d2700

Please sign in to comment.