Skip to content

Commit

Permalink
Minor adjustment after merging PR #116.
Browse files Browse the repository at this point in the history
- Renamed hello_world to starter_example since it clashes with IDF's component name.
- Changed NULL to nullptr.
- Use operator[] instead of at() since there should be no need to do index-checking.
- Use std::for_each instead of indexed loops.
- Updated README.md.
  • Loading branch information
PerMalmberg committed Nov 30, 2019
1 parent 279e992 commit c7d6da6
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 109 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)

# Select the test project to build
set(selected_test_project access_point)
set(selected_test_project starter_example)

# For Linux builds, you may enable address sanitizer
set(SMOOTH_ENABLE_ASAN 0)
set(SMOOTH_ASAN_OPTIMIZATION_LEVEL 1)

list(APPEND available_tests
hello_world
starter_example
access_point
logging
mqtt
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ Below is the list of individuals that have contributed to the project.
|[KerryRJ](https://github.com/KerryRJ)|[PR#71](https://github.com/PerMalmberg/Smooth/pull/71)|
|[jeremyjh](https://github.com/jeremyjh)|[PR#87](https://github.com/PerMalmberg/Smooth/pull/87)|
|[COM8](https://github.com/COM8)|[PR#98](https://github.com/PerMalmberg/Smooth/pull/98)|
|[enelson1001](https://github.com/enelson1001)|
|[enelson1001](https://github.com/enelson1001)|[PR#116](https://github.com/PerMalmberg/Smooth/pull/116)|
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Smooth is developed on a Linux machine so how well it compiles using the Windows
- Input
- Input with interrupt to event translation
- I2C Master Device class
- SPI Master Device class
- Flash and SDCard initialization.

### Application level
Expand All @@ -54,9 +55,16 @@ Smooth is developed on a Linux machine so how well it compiles using the Windows
- Simple templates
- Websocket support
- MQTT Client
- Sensor BME280
- 16 channel I/O expander MCP23017
- RGB LED, i.e. WS2812(B), SK6812, WS2813, (a.k.a NeoPixel).
- Device support
- SPI
- Sensors
- BME280
- Displays
- ILI9341
- I2C
- BME280
- MCP23017
- RGB LED, i.e. WS2812(B), SK6812, WS2813, (a.k.a NeoPixel).
- Filesystem helpers


Expand Down
148 changes: 75 additions & 73 deletions lib/smooth/application/display/ILI9341.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ namespace smooth::application::display
bool ILI9341::init(spi_host_device_t host)
{
// spi_transaction will not control chip select
bool res = initialize(host, GPIO_NUM_NC);

return res;
return initialize(host, GPIO_NUM_NC);
}

// I have found different displays set the value of madctl differently
Expand Down Expand Up @@ -146,7 +144,7 @@ namespace smooth::application::display
std::lock_guard<std::mutex> lock(get_guard());
spi_transaction_t trans;
std::memset(&trans, 0, sizeof(trans)); //Zero out the transaction
trans.rx_buffer = NULL;
trans.rx_buffer = nullptr;
trans.rxlength = 0;
trans.length = 8;
trans.tx_buffer = &cmd;
Expand All @@ -171,7 +169,7 @@ namespace smooth::application::display
std::lock_guard<std::mutex> lock(get_guard());
spi_transaction_t trans;
std::memset(&trans, 0, sizeof(trans)); //Zero out the transaction
trans.rx_buffer = NULL;
trans.rx_buffer = nullptr;
trans.rxlength = 0;
trans.length = length * 8;
trans.tx_buffer = data;
Expand All @@ -188,7 +186,7 @@ namespace smooth::application::display
// This routine queues these commands up as interrupt transactions so they get
// sent faster (compared to calling spi_device_transmit several times), and
// meanwhile the lines for next transactions can get calculated.
bool ILI9341::send_lines(int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint8_t* data, size_t length)
bool ILI9341::send_lines(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const uint8_t* data, size_t length)
{
// if length is 0 nothing to do and probably an error so return
bool res = length > 0;
Expand All @@ -204,43 +202,45 @@ namespace smooth::application::display
// stack; we need this memory even when this function is finished because
// the SPI driver needs access to it even while we're already calculating
// the next line.
static std::array<spi_transaction_t, 6> trans;
static std::array<spi_transaction_t, 6> trans{};

// In theory, it's better to initialize trans and data only once and hang
// on to the initialized variables. We allocate them on the stack, so we
// don't need to re-init them each call.
for (uint8_t x = 0; x < 6; x++)
{
//Zero out the transaction
std::memset(&trans.at(x), 0, sizeof(spi_transaction_t));

if ((x & 1) == 0)
{
// Even transfers are commands
trans.at(x).rx_buffer = NULL;
trans.at(x).rxlength = 0;
trans.at(x).length = 8;

// setup pre and post states
dc_pretrans_pin_states.at(x) = PIN_LOW;
cs_pretrans_pin_states.at(x) = PIN_LOW;
cs_posttrans_pin_states.at(x) = PIN_HIGH;
}
else
{
// Odd transfers are data
trans.at(x).rx_buffer = NULL;
trans.at(x).rxlength = 0;
trans.at(x).length = 8 * 4;

// setup pre and post states
dc_pretrans_pin_states.at(x) = PIN_HIGH;
cs_pretrans_pin_states.at(x) = PIN_LOW;
cs_posttrans_pin_states.at(x) = PIN_HIGH;
}

trans.at(x).flags = SPI_TRANS_USE_TXDATA;
}
std::size_t x = 0;
std::for_each(trans.begin(), trans.end(), [&](auto& current)
{
//Zero out the transaction
std::memset(&current, 0, sizeof(spi_transaction_t));

if ((x & 1) == 0)
{
// Even transfers are commands
current.rx_buffer = nullptr;
current.rxlength = 0;
current.length = 8;

// setup pre and post states
dc_pretrans_pin_states.at(x) = PIN_LOW;
cs_pretrans_pin_states.at(x) = PIN_LOW;
cs_posttrans_pin_states.at(x) = PIN_HIGH;
}
else
{
// Odd transfers are data
current.rx_buffer = nullptr;
current.rxlength = 0;
current.length = 8 * 4;

// setup pre and post states
dc_pretrans_pin_states.at(x) = PIN_HIGH;
cs_pretrans_pin_states.at(x) = PIN_LOW;
cs_posttrans_pin_states.at(x) = PIN_HIGH;
}

current.flags = SPI_TRANS_USE_TXDATA;
x++;
});

// Column addresses
trans.at(0).tx_data[0] = 0x2A;
Expand All @@ -265,10 +265,10 @@ namespace smooth::application::display
trans.at(5).flags = 0; // clear flags

// Queue all transactions.
for (uint8_t i = 0; i < 6; i++)
{
res &= queue_transaction(trans.at(i));
}
std::for_each(trans.begin(), trans.end(), [&](auto& t)
{
res &= queue_transaction(t);
});
}

return res;
Expand Down Expand Up @@ -322,7 +322,7 @@ namespace smooth::application::display
// don't want to copy the dummy byte but copy only the real data
for (size_t i = 0; i < param_count; i++)
{
data.push_back( static_cast<uint8_t>((rxdata[i] << 1) | (rxdata[i + 1] >> 7)));
data.push_back(static_cast<uint8_t>((rxdata[i] << 1) | (rxdata[i + 1] >> 7)));
}
}
else
Expand All @@ -344,51 +344,53 @@ namespace smooth::application::display
std::lock_guard<std::mutex> lock(get_guard());

// create 2 spi transactions; one for command and one for reading parameters
std::array<spi_transaction_t, 2> trans;
std::array<spi_transaction_t, 2> trans{};

for (uint8_t x = 0; x < 2; x++)
{
//Zero out the transaction
std::memset(&trans.at(x), 0, sizeof(spi_transaction_t));
}
std::for_each(trans.begin(), trans.end(), [](auto& t)
{
//Zero out the transaction
std::memset(&t, 0, sizeof(spi_transaction_t));
});

// reset current transaction counter
current_transaction = 0;

// configure command transaction
trans.at(0).rx_buffer = NULL;
trans.at(0).rxlength = 0;
trans.at(0).length = 8;
trans.at(0).tx_data[0] = cmd;
trans.at(0).flags = SPI_TRANS_USE_TXDATA;
auto& cmd_trans = trans[0];
cmd_trans.rx_buffer = nullptr;
cmd_trans.rxlength = 0;
cmd_trans.length = 8;
cmd_trans.tx_data[0] = cmd;
cmd_trans.flags = SPI_TRANS_USE_TXDATA;

// setup pre and post control pin states
dc_pretrans_pin_states.at(0) = PIN_LOW;
cs_pretrans_pin_states.at(0) = PIN_LOW;
cs_posttrans_pin_states.at(0) = PIN_LOW; // keep chip select low
dc_pretrans_pin_states[0] = PIN_LOW;
cs_pretrans_pin_states[0] = PIN_LOW;
cs_posttrans_pin_states[0] = PIN_LOW; // keep chip select low

// configure read parameters transaction
trans.at(1).rx_buffer = rxdata;
trans.at(1).rxlength = 8 * length;
trans.at(1).length = 8 * length;
trans.at(1).tx_buffer = NULL;
auto& read_params = trans[1];
read_params.rx_buffer = rxdata;
read_params.rxlength = 8 * length;
read_params.length = 8 * length;
read_params.tx_buffer = nullptr;

// setup pre and post control pin states
dc_pretrans_pin_states.at(1) = PIN_HIGH;
cs_pretrans_pin_states.at(1) = PIN_LOW;
cs_posttrans_pin_states.at(1) = PIN_HIGH;
dc_pretrans_pin_states[1] = PIN_HIGH;
cs_pretrans_pin_states[1] = PIN_LOW;
cs_posttrans_pin_states[1] = PIN_HIGH;

// Queue the 2 transactions to be sent
for (uint8_t i = 0; i < 2; i++)
{
res &= queue_transaction(trans.at(i));
}
std::for_each(trans.begin(), trans.end(), [&](auto& t)
{
res &= queue_transaction(t);
});

// wait for the 2 transaction to finish
for (uint8_t x = 0; x < 2; x++)
{
res &= wait_for_transaction_to_finish();
}
std::for_each(trans.begin(), trans.end(), [&](auto&)
{
res &= wait_for_transaction_to_finish();
});
}

return res;
Expand Down
5 changes: 0 additions & 5 deletions lib/smooth/application/io/spi/BME280Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ using namespace smooth::core;

namespace smooth::application::sensor
{
// Constructor
BME280Core::BME280Core()
{
}

// Get config_sensor datagram that contains register addresses and data that will be used to configure the BME280
void BME280Core::get_configure_sensor_datagram(std::vector<uint8_t>& datagram,
SensorMode mode,
Expand Down
12 changes: 6 additions & 6 deletions lib/smooth/application/io/spi/BME280SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace smooth::application::sensor
std::lock_guard<std::mutex> lock(get_guard());
spi_transaction_t trans;
std::memset(&trans, 0, sizeof(trans)); //Zero out the transaction
trans.rx_buffer = NULL;
trans.rx_buffer = nullptr;
trans.length = 8 * length;
trans.tx_buffer = const_cast<uint8_t*>(txdata);

Expand Down Expand Up @@ -134,7 +134,7 @@ namespace smooth::application::sensor
BME280Core::FilterCoeff filter_coeff,
BME280Core::SpiInterface spi_interface)
{
std::vector<uint8_t> datagram;
std::vector<uint8_t> datagram{};

bme280_core.get_configure_sensor_datagram(datagram,
mode,
Expand All @@ -146,7 +146,7 @@ namespace smooth::application::sensor
spi_interface);

// Since we chose to use DMA on this SPI Device use SpiDmaFixedBuffer for the tx_buffer in spi_transfer
SpiDmaFixedBuffer<uint8_t, 8> txdata;
SpiDmaFixedBuffer<uint8_t, 8> txdata{};

// for spi write the bit in position b7 of register address must be zero
txdata[0] = datagram.at(0) & 0x7F; // modify 0xF2 to 0x72
Expand Down Expand Up @@ -202,9 +202,9 @@ namespace smooth::application::sensor
if (!trimming_read)
{
// Since we chose to use DMA on this SPI Device use SpiDmaFixedBuffer for the rx_buffer in spi_transfer
SpiDmaFixedBuffer<uint8_t, 32> calib00_calib25_data; // 0x88-0xA1
SpiDmaFixedBuffer<uint8_t, 8> calib26_calib32_data; // 0xE1-0xE7
core::util::FixedBuffer<uint8_t, 32> calibration_data;
SpiDmaFixedBuffer<uint8_t, 32> calib00_calib25_data{}; // 0x88-0xA1
SpiDmaFixedBuffer<uint8_t, 8> calib26_calib32_data{}; // 0xE1-0xE7
core::util::FixedBuffer<uint8_t, 32> calibration_data{};

trimming_read = read(BME280Core::CALIB00_REG, calib00_calib25_data.data(), 27)
&& read(BME280Core::CALIB26_REG, calib26_calib32_data.data(), 8);
Expand Down
11 changes: 6 additions & 5 deletions lib/smooth/include/smooth/application/display/ILI9341.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ namespace smooth::application::display
/// \param data The pointer to the first byte in the data (color data)
/// \param length The number of bytes in the data
/// \return true on success, false on failure
bool send_lines(int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint8_t* data, size_t length);
bool send_lines(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const uint8_t* data, size_t length);

/// Wait for send lines to finish;
/// Waits for all SPI transactions in send lines to complete
Expand Down Expand Up @@ -152,9 +152,10 @@ namespace smooth::application::display
smooth::core::io::Output dc_pin;
smooth::core::io::Output cs_pin;

std::array<bool, 6> dc_pretrans_pin_states;
std::array<bool, 6> cs_pretrans_pin_states;
std::array<bool, 6> cs_posttrans_pin_states;
std::size_t current_transaction;
using PreTransPinState = std::array<bool, 6>;
PreTransPinState dc_pretrans_pin_states{};
PreTransPinState cs_pretrans_pin_states{};
PreTransPinState cs_posttrans_pin_states{};
std::size_t current_transaction{};
};
}
2 changes: 1 addition & 1 deletion lib/smooth/include/smooth/application/io/spi/BME280Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ namespace smooth::application::sensor
"SPI_3_WIRE"
};

BME280Core();
BME280Core() = default;

/// Get a datagram that contains register addresses and data that will be used to configure the BME280
/// \param datagram A vector that holds the configuration register addresses and data
Expand Down
10 changes: 5 additions & 5 deletions mock-idf/components/heap/heap_caps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "esp_heap_caps.h"
#include <memory>


void *heap_caps_malloc( size_t size, uint32_t caps )
void *heap_caps_malloc( size_t size, uint32_t /*caps*/ )
{
return NULL;
return malloc(size);
}

void heap_caps_free( void *ptr)
void heap_caps_free(void* ptr)
{

free(ptr);
}

Loading

0 comments on commit c7d6da6

Please sign in to comment.