Compare commits
1 Commits
b3d830cdeb
...
old-graph-
| Author | SHA1 | Date | |
|---|---|---|---|
| 26839b82b7 |
21
cpp/problems/CMakeLists.txt
Normal file
21
cpp/problems/CMakeLists.txt
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
################################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: A root project for C++ practice problems and solutions ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
|
project(
|
||||||
|
#[[NAME]] Problems
|
||||||
|
VERSION 1.0
|
||||||
|
DESCRIPTION "Practice problems and solutions written in C++"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY, ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
add_compile_options("-Wall")
|
||||||
|
|
||||||
|
add_subdirectory(graphs)
|
||||||
24
cpp/problems/README.md
Normal file
24
cpp/problems/README.md
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# Problems
|
||||||
|
|
||||||
|
A collection of some example problems and solutions written in C++. Mostly these
|
||||||
|
are based off questions I found on [hackerrank](https://www.hackerrank.com),
|
||||||
|
[leetcode](https://leetcode.com/), [codility](https://www.codility.com/), or
|
||||||
|
similar programming practice platforms.
|
||||||
|
|
||||||
|
```
|
||||||
|
klips/cpp/problems
|
||||||
|
.
|
||||||
|
├── graphs # Graph implementations with related problems and solutions
|
||||||
|
└── README.md
|
||||||
|
```
|
||||||
|
|
||||||
|
We can build the examples with the following commands.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd /path/to/klips/cpp/problems/
|
||||||
|
mkdir build && cd build
|
||||||
|
cmake .. && cmake --build .
|
||||||
|
ls bin/
|
||||||
|
|
||||||
|
problems-graphs
|
||||||
|
```
|
||||||
21
cpp/problems/graphs/CMakeLists.txt
Normal file
21
cpp/problems/graphs/CMakeLists.txt
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
################################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Collection of problems and solutions to graph problems in C++ ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
|
project(
|
||||||
|
#[[NAME]] ProblemsGraphs
|
||||||
|
VERSION 1.0
|
||||||
|
DESCRIPTION "Problems and solutions using graphs in C++"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
add_compile_options("-Wall")
|
||||||
|
|
||||||
|
add_executable(problems-graphs driver.cpp lib-graph.cpp lib-graph.hpp)
|
||||||
32
cpp/problems/graphs/driver.cpp
Normal file
32
cpp/problems/graphs/driver.cpp
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Driver program solving various C++ graph problems ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "lib-graph.hpp"
|
||||||
|
|
||||||
|
int main(const int argc, const char * argv[]) {
|
||||||
|
Simple::Graph g({ {1,2,3}, {2,3,4} });
|
||||||
|
g.Print();
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
std::vector<int> graphA = {6,0,1,2,3,4,5};
|
||||||
|
std::vector<std::pair<int, int>> graphB = { {9, 2}, {2, 3}, {3, 1} };
|
||||||
|
std::vector<std::vector<int>> graphC = {{1}, {2, 3}, {3, 1, 0}};
|
||||||
|
g.ReadEdges(graphA);
|
||||||
|
g.Print();
|
||||||
|
std::cout << std::endl;
|
||||||
|
g.ReadEdges(graphB);
|
||||||
|
g.Print();
|
||||||
|
std::cout << std::endl;
|
||||||
|
g.ReadEdges(graphC);
|
||||||
|
g.Print();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
12
cpp/problems/graphs/lib-graph.cpp
Normal file
12
cpp/problems/graphs/lib-graph.cpp
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Graph implementations to solve various problems in C++ ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "lib-graph.hpp"
|
||||||
|
|
||||||
|
|
||||||
335
cpp/problems/graphs/lib-graph.hpp
Normal file
335
cpp/problems/graphs/lib-graph.hpp
Normal file
@@ -0,0 +1,335 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Graph implementations to solve various problems in C++ ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <map>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#ifndef GRAPHS_LIB_GRAPH_HPP
|
||||||
|
#define GRAPHS_LIB_GRAPH_HPP
|
||||||
|
|
||||||
|
|
||||||
|
namespace Simple {
|
||||||
|
typedef int32_t Node;
|
||||||
|
typedef std::vector<Node> Nodes;
|
||||||
|
typedef std::vector<Nodes> Edges;
|
||||||
|
|
||||||
|
class Graph {
|
||||||
|
public:
|
||||||
|
Graph() = default;
|
||||||
|
explicit Graph(Edges e) : edges(std::move(e)) { }
|
||||||
|
|
||||||
|
void Print()
|
||||||
|
{
|
||||||
|
for (size_t node = 0; node < edges.size(); node++) {
|
||||||
|
for (const auto & to : edges[node]) {
|
||||||
|
std::cout << "(" << node << ")-----(" << to << ")" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where graph[i] represents the connection between node i and graph[i]
|
||||||
|
// {1, 1, 2, 2}
|
||||||
|
void ReadEdges(const std::vector<int> & graph)
|
||||||
|
{
|
||||||
|
edges.clear();
|
||||||
|
edges.assign(graph.size(), {});
|
||||||
|
for (int i = 0; i < graph.size(); i++) {
|
||||||
|
if (i == graph[i]) continue;
|
||||||
|
edges[graph[i]].push_back(i);
|
||||||
|
edges[i].push_back(graph[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where each graph[i] represents a single edge between two nodes
|
||||||
|
// { {1, 2}, {2, 3}, {3, 1} }
|
||||||
|
void ReadEdges(const std::vector<std::pair<int, int>> & graph)
|
||||||
|
{
|
||||||
|
edges.clear();
|
||||||
|
for (const auto & edge : graph) {
|
||||||
|
while (edges.size() <= edge.first || edges.size() <= edge.second) {
|
||||||
|
edges.emplace_back();
|
||||||
|
}
|
||||||
|
edges[edge.first].push_back(edge.second);
|
||||||
|
edges[edge.second].push_back(edge.first);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where graph[node] holds all connected adjacent nodes
|
||||||
|
// {{1}, {2, 3}, {2, 1, 0}}
|
||||||
|
void ReadEdges(const std::vector<std::vector<int>> & graph)
|
||||||
|
{
|
||||||
|
edges.clear();
|
||||||
|
edges.assign(graph.size(), {});
|
||||||
|
for (size_t i = 0; i < graph.size(); i++) {
|
||||||
|
for (const auto & adj : graph[i]) {
|
||||||
|
if (adj == i) continue;
|
||||||
|
edges[i].push_back(adj);
|
||||||
|
edges[adj].push_back(int32_t(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Edges edges;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
namespace Object {
|
||||||
|
struct Node {
|
||||||
|
Node() : val(INT32_MIN), adj() { }
|
||||||
|
explicit Node(int32_t v) : val(v), adj() { }
|
||||||
|
Node(int32_t v, std::vector<int32_t> a) : val(v), adj(std::move(a)) { }
|
||||||
|
|
||||||
|
int32_t val;
|
||||||
|
std::vector<int32_t> adj;
|
||||||
|
|
||||||
|
// Define operator== for std::find; And comparisons between nodes
|
||||||
|
bool operator==(const Node & b) const { return this->val == b.val;}
|
||||||
|
bool operator!=(const Node & b) const { return this->val != b.val;}
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef std::vector<Node> Edges;
|
||||||
|
|
||||||
|
class Graph {
|
||||||
|
public:
|
||||||
|
Graph() = default;
|
||||||
|
explicit Graph(Edges e) : edges(std::move(e)) { }
|
||||||
|
|
||||||
|
void Print()
|
||||||
|
{
|
||||||
|
for (int32_t node = 0; node < edges.size(); node++) {
|
||||||
|
for (const auto & to : GetNode(node)->adj) {
|
||||||
|
std::cout << "(" << node << ")-----(" << to << ")" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Node * GetNode(const int32_t & nodeVal)
|
||||||
|
{
|
||||||
|
auto foundNode = std::find(edges.begin(), edges.end(), Node(nodeVal));
|
||||||
|
// [nodeVal](const Node & a)->bool { return a.val == nodeVal;});
|
||||||
|
if (foundNode == edges.end()) return nullptr; // Node does not exist
|
||||||
|
return &*foundNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node * CreateNode(const int32_t & nodeVal)
|
||||||
|
{
|
||||||
|
auto newNode = GetNode(nodeVal);
|
||||||
|
if (newNode != nullptr) return newNode;
|
||||||
|
// Create node if not found
|
||||||
|
edges.emplace_back(nodeVal); // Calls Node(int32_t) ctor
|
||||||
|
return &edges.back(); // Get ptr to our new node; Don't copy it
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where graph[i] represents the connection between node i and graph[i]
|
||||||
|
// {1, 1, 2, 2}
|
||||||
|
void ReadEdges(const std::vector<int> & graph)
|
||||||
|
{
|
||||||
|
edges.clear();
|
||||||
|
for (int i = 0; i < graph.size(); i++) {
|
||||||
|
if (i == graph[i]) continue;
|
||||||
|
// Check if nodes already exist; Create them if not found
|
||||||
|
auto nodeFrom = CreateNode(graph[i]);
|
||||||
|
auto nodeTo = CreateNode(i);
|
||||||
|
// Push node ptr to adjacent list
|
||||||
|
nodeFrom->adj.push_back(nodeTo->val);
|
||||||
|
nodeTo->adj.push_back(nodeFrom->val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where each graph[i] represents a single edge between two nodes
|
||||||
|
// { {1, 2}, {2, 3}, {3, 1} }
|
||||||
|
void ReadEdges(const std::vector<std::pair<int, int>> & graph)
|
||||||
|
{
|
||||||
|
edges.clear();
|
||||||
|
for (const auto & edge : graph) {
|
||||||
|
auto nodeFrom = CreateNode(edge.first);
|
||||||
|
auto nodeTo = CreateNode(edge.second);
|
||||||
|
nodeFrom->adj.push_back(nodeTo->val);
|
||||||
|
nodeTo->adj.push_back(nodeFrom->val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where graph[node] holds all connected adjacent nodes
|
||||||
|
// {{1}, {2, 3}, {2, 1, 0}}
|
||||||
|
void ReadEdges(const std::vector<std::vector<int>> & graph)
|
||||||
|
{
|
||||||
|
edges.clear();
|
||||||
|
edges.assign(graph.size(), {});
|
||||||
|
for (size_t i = 0; i < graph.size(); i++) {
|
||||||
|
for (const auto & adj : graph[i]) {
|
||||||
|
if (adj == i) continue;
|
||||||
|
auto nodeFrom = CreateNode(int32_t(i));
|
||||||
|
auto nodeTo = CreateNode(adj);
|
||||||
|
nodeFrom->adj.push_back(nodeTo->val);
|
||||||
|
nodeTo->adj.push_back(nodeFrom->val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Edges edges;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Weighted {
|
||||||
|
using Weight = int32_t;
|
||||||
|
using Adjacent = std::multimap<Weight, int32_t>;
|
||||||
|
struct Node {
|
||||||
|
Node() : val(INT32_MIN), adj() { }
|
||||||
|
explicit Node(int32_t v) : val(v), adj() { }
|
||||||
|
Node(int32_t v, Adjacent a) : val(v), adj(std::move(a)) { }
|
||||||
|
|
||||||
|
int32_t val;
|
||||||
|
Adjacent adj;
|
||||||
|
};
|
||||||
|
using Edge = std::pair<int, int>;
|
||||||
|
|
||||||
|
class Graph {
|
||||||
|
Graph() = default;
|
||||||
|
explicit Graph(Node n) : root(std::move(n)) { }
|
||||||
|
void ReadGraph(std::vector<std::vector<int>> nodeList)
|
||||||
|
{
|
||||||
|
// Read a 2D vector of nodes into a
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
Node root;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//namespace Object {
|
||||||
|
//struct Edge {
|
||||||
|
// friend struct Node;
|
||||||
|
// friend class Graph;
|
||||||
|
// Edge() : from(INT32_MIN), to(INT32_MIN) { }
|
||||||
|
// Edge(const int32_t & f, const int32_t & t) : from(f), to(t) { }
|
||||||
|
//
|
||||||
|
// private:
|
||||||
|
// int32_t from, to;
|
||||||
|
// };
|
||||||
|
// using Edges = std::vector<Edge>;
|
||||||
|
//
|
||||||
|
//// template <typename T, typename S>
|
||||||
|
//// struct Subscriptor {
|
||||||
|
//// T<int32_t, S> data;
|
||||||
|
//// };
|
||||||
|
//
|
||||||
|
// struct Node {
|
||||||
|
// using Adjacent = std::vector<Node *>;
|
||||||
|
// using NodeMap = std::unordered_map<int32_t, Node *>;
|
||||||
|
// friend class Graph; // Allow Graph to access protected / private members
|
||||||
|
// friend struct GraphData;
|
||||||
|
//
|
||||||
|
// // Struct is public by default
|
||||||
|
// Node () : val(0), adj() { }
|
||||||
|
// explicit Node(int32_t v) : val(v), adj() { }
|
||||||
|
// Node(int32_t v, Adjacent a) : val(v), adj(std::move(a)) { }
|
||||||
|
// inline void SetAdjacent(Adjacent a) { adj = std::move(a);}
|
||||||
|
// inline void SetVal(int32_t v) { val = v;}
|
||||||
|
// NodeMap GetNodeMap() {
|
||||||
|
// NodeMap result;
|
||||||
|
// BuildNodeMap(&result);
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
// void BuildNodeMap(NodeMap & nodeMap, Node * startNode=nullptr) {
|
||||||
|
// auto list = startNode == nullptr ? adj : startNode->adj;
|
||||||
|
// for (const auto & node : list) {
|
||||||
|
// if (!nodeMap.count(node->val)) {
|
||||||
|
// nodeMap[node->val] = node;
|
||||||
|
// BuildNodeMap(nodeMap, startNode);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// protected:
|
||||||
|
// int32_t val;
|
||||||
|
// Adjacent adj;
|
||||||
|
// Edges edges;
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// struct GraphData {
|
||||||
|
// GraphData() = default;
|
||||||
|
// explicit GraphData(const Node & n)
|
||||||
|
// {
|
||||||
|
// graphEdges n.edges;
|
||||||
|
// for (const auto & edge : n.edges) {
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Implement subscript operators for unordered_multimap
|
||||||
|
// struct GraphEdges {
|
||||||
|
// Edges * operator[](int32_t nodeVal) {
|
||||||
|
// auto found = graphEdges.find(nodeVal):
|
||||||
|
// if (found != graphEdges.end()) {
|
||||||
|
// return &*found->second;
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// return nullptr;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// std::unordered_multimap<int32_t, Edges *> graphEdges;
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// // Implement subscript operators for unordered_map
|
||||||
|
// struct GraphNodes {
|
||||||
|
// Node * operator[](int32_t nodeVal) {
|
||||||
|
// auto found = graphNodes.find(nodeVal):
|
||||||
|
// if (found != graphNodes.end()) {
|
||||||
|
// return &*found->second;
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// return nullptr;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// std::unordered_map<int32_t, Node *> graphNodes;
|
||||||
|
// };
|
||||||
|
// // unordered_* provides O(1) access and search
|
||||||
|
// GraphEdges graphEdges;
|
||||||
|
// GraphNodes graphNodes;
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// class Graph {
|
||||||
|
// // Class is private by default
|
||||||
|
// Node root;
|
||||||
|
// std::unordered_map<int32_t, Node *> graphNodes;
|
||||||
|
// std::multimap<int32_t, Edges> graphEdges;
|
||||||
|
//// std::unordered_map<int32_t, Node *> graphNodes;
|
||||||
|
//// GraphData data; // Struct containing all graph edges / nodes
|
||||||
|
//
|
||||||
|
// public:
|
||||||
|
// Graph() = default;
|
||||||
|
// explicit Graph(Node r) : root(std::move(r)) { }
|
||||||
|
//
|
||||||
|
// inline const Node & GetRoot() const { return root;}
|
||||||
|
//// inline Node * GetNode(int32_t nodeVal) { return data.graphNodes[nodeVal];}
|
||||||
|
//// inline const Node * GetConstNode(int32_t nodeVal)
|
||||||
|
//// { return data.graphNodes[nodeVal];}
|
||||||
|
//
|
||||||
|
// const Node * DFS(int32_t nodeVal, int32_t startNode=INT32_MIN)
|
||||||
|
// {
|
||||||
|
// // If startNode was not set, begin search from root node
|
||||||
|
// startNode = startNode == INT32_MIN ? root.val : startNode;
|
||||||
|
// if (startNode == nodeVal) {
|
||||||
|
// return graphNodes[nodeVal];
|
||||||
|
// }
|
||||||
|
// for (const auto & edge : root.edges) {
|
||||||
|
// return DFS(nodeVal, edge.to);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
//}
|
||||||
|
|
||||||
|
#endif // GRAPHS_LIB_GRAPH_HPP
|
||||||
4
esp/cpp/06_i2c-scanner/.gitignore
vendored
4
esp/cpp/06_i2c-scanner/.gitignore
vendored
@@ -1,4 +0,0 @@
|
|||||||
build
|
|
||||||
managed_components
|
|
||||||
dependencies.lock
|
|
||||||
sdkconfig.old
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
# For more information about build system see
|
|
||||||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
|
|
||||||
# The following five lines of boilerplate have to be in your project's
|
|
||||||
# CMakeLists in this exact order for cmake to work correctly
|
|
||||||
cmake_minimum_required(VERSION 3.26)
|
|
||||||
|
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
|
||||||
|
|
||||||
project(
|
|
||||||
#[[NAME]] i2c-scanner
|
|
||||||
VERSION 0.1
|
|
||||||
DESCRIPTION "Simple I2C device scanner"
|
|
||||||
LANGUAGES CXX
|
|
||||||
)
|
|
||||||
# For writing pure cmake components, see the documentation
|
|
||||||
# https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/build-system.html#writing-pure-cmake-components
|
|
||||||
idf_build_set_property(COMPILE_OPTIONS "-Wno-error" APPEND)
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
# 06_i2c-scanner
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
Simple I2C device scanner.
|
|
||||||
|
|
||||||
For this example I used this [SSD1306 OLED display](https://www.digikey.com/en/products/detail/winstar-display/WEA012864DWPP3N00003/20533255).
|
|
||||||
|
|
||||||
To build the example run the following commands.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
source ~/path/to/esp-idf/export.sh
|
|
||||||
|
|
||||||
mkdir build
|
|
||||||
cd build
|
|
||||||
cmake ..
|
|
||||||
make -j $(nproc)
|
|
||||||
|
|
||||||
# Flash to ESP32
|
|
||||||
make flash
|
|
||||||
|
|
||||||
# Open Serial Monitor, press CTRL+] to exit.
|
|
||||||
make monitor
|
|
||||||
```
|
|
||||||
|
|
||||||
Expected output in serial monitor at 115200 baud
|
|
||||||
|
|
||||||
```bash
|
|
||||||
Scanning I2C devices...
|
|
||||||
[0x3c]: Device found with clock rate 100000 and timeout 50
|
|
||||||
Done.
|
|
||||||
```
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
idf_component_register(
|
|
||||||
SRCS "main.cpp"
|
|
||||||
INCLUDE_DIRS "."
|
|
||||||
)
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
## IDF Component Manager Manifest File
|
|
||||||
dependencies:
|
|
||||||
## Required IDF version
|
|
||||||
idf:
|
|
||||||
version: '>=4.1.0'
|
|
||||||
# # Put list of dependencies here
|
|
||||||
# # For components maintained by Espressif:
|
|
||||||
# component: "~1.0.0"
|
|
||||||
# # For 3rd party components:
|
|
||||||
# username/component: ">=1.0.0,<2.0.0"
|
|
||||||
# username2/component2:
|
|
||||||
# version: "~1.0.0"
|
|
||||||
# # For transient dependencies `public` flag can be set.
|
|
||||||
# # `public` flag doesn't have an effect dependencies of the `main` component.
|
|
||||||
# # All dependencies of `main` are public by default.
|
|
||||||
# public: true
|
|
||||||
espressif/arduino-esp32: ^3.1.1
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
#include "Arduino.h"
|
|
||||||
#include "Wire.h"
|
|
||||||
|
|
||||||
[[maybe_unused]] static const char *TAG = "i2c-scanner";
|
|
||||||
|
|
||||||
void i2c_scan()
|
|
||||||
{
|
|
||||||
uint8_t device_num = 0;
|
|
||||||
Serial.println("Scanning I2C devices...");
|
|
||||||
for (byte address = 1; address < 127; address++) {
|
|
||||||
Wire.beginTransmission(address);
|
|
||||||
byte error = Wire.endTransmission();
|
|
||||||
if (error == 0) {
|
|
||||||
Serial.printf(
|
|
||||||
"[0x%.2x]: Device found with clock rate %lu and timeout %u\n",
|
|
||||||
address,
|
|
||||||
Wire.getClock(), Wire.getTimeOut());
|
|
||||||
device_num++;
|
|
||||||
} else if (error == 4) {
|
|
||||||
Serial.printf("[0x%.2x]: Unknown error.\n", address);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Serial.println(device_num > 0 ? "Done.\n" : "No I2C devices found.\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Serial.begin(115200);
|
|
||||||
Wire.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
i2c_scan();
|
|
||||||
}
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 227 KiB |
File diff suppressed because it is too large
Load Diff
4
esp/cpp/07_lcd-panel/.gitignore
vendored
4
esp/cpp/07_lcd-panel/.gitignore
vendored
@@ -1,4 +0,0 @@
|
|||||||
build
|
|
||||||
managed_components
|
|
||||||
dependencies.lock
|
|
||||||
sdkconfig.old
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
# For more information about build system see
|
|
||||||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
|
|
||||||
# The following five lines of boilerplate have to be in your project's
|
|
||||||
# CMakeLists in this exact order for cmake to work correctly
|
|
||||||
cmake_minimum_required(VERSION 3.26)
|
|
||||||
|
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
|
||||||
|
|
||||||
project(
|
|
||||||
#[[NAME]] lcd-panel
|
|
||||||
VERSION 0.1
|
|
||||||
DESCRIPTION "Example of using the SSD1306 LCD display with ESP-IDF and LVGL"
|
|
||||||
LANGUAGES CXX
|
|
||||||
)
|
|
||||||
# For writing pure cmake components, see the documentation
|
|
||||||
# https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/build-system.html#writing-pure-cmake-components
|
|
||||||
idf_build_set_property(COMPILE_OPTIONS "-Wno-error" APPEND)
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
# 06_lcd-panel
|
|
||||||
|
|
||||||
This example is largely adapted from those in [ESP32-basic-starter-kit.pdf](./ESP32-basic-starter-kit.pdf).
|
|
||||||
|
|
||||||
The APIs in the original examples paired with this PDF have changed, and I decided to do some different things with the code and/or circuits, but the original code can be [found here](https://www.dropbox.com/scl/fo/6znlij3eb23ih4jxcpv2w/AKvB1t9CCUgoVRVtGen8Yrw?rlkey=z84anl0hs940qf9fpl7l8q8q2&e=1&dl=0).
|
|
||||||
|
|
||||||
This is the same example in [03_temp-humidity-web](./../03_temp-humidity-web), ported to the cmake ESP-IDF build system.
|
|
||||||
|
|
||||||
For instructions on setting up the ESP-IDF see [04_-esp-idf-arduino](./../04_esp-idf-arduino)
|
|
||||||
|
|
||||||
This example is largely adapted from those in [ESP32-basic-starter-kit.pdf](./ESP32-basic-starter-kit.pdf).
|
|
||||||
|
|
||||||
The APIs in the original examples paired with this PDF have changed, and I decided to do some different things with the code and/or circuits, but the original code can be [found here](https://www.dropbox.com/scl/fo/6znlij3eb23ih4jxcpv2w/AKvB1t9CCUgoVRVtGen8Yrw?rlkey=z84anl0hs940qf9fpl7l8q8q2&e=1&dl=0).
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
Temperature and humidity sensor served on a web page within the local network.
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
To build this example run the following commands.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
source ~/path/to/esp-idf/export.sh
|
|
||||||
|
|
||||||
mkdir build
|
|
||||||
cd build
|
|
||||||
cmake ..
|
|
||||||
make -j $(nproc)
|
|
||||||
make flash
|
|
||||||
```
|
|
||||||
|
|
||||||
[ESP-IDF I2C documentation](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/i2c.html)
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
idf_component_register(
|
|
||||||
SRCS main.cpp display.cpp panel_device.cpp ssd1306.cpp
|
|
||||||
INCLUDE_DIRS .
|
|
||||||
REQUIRES driver
|
|
||||||
)
|
|
||||||
@@ -1,212 +0,0 @@
|
|||||||
|
|
||||||
#include "display.h"
|
|
||||||
#include "ssd1306.h"
|
|
||||||
|
|
||||||
#include "widgets/label/lv_label.h"
|
|
||||||
#include <esp_log.h>
|
|
||||||
#include <esp_lcd_panel_ops.h>
|
|
||||||
#include <esp_heap_caps.h>
|
|
||||||
#include <esp_lcd_panel_io.h>
|
|
||||||
#include <esp_timer.h>
|
|
||||||
#include <lv_init.h>
|
|
||||||
#include <display/lv_display.h>
|
|
||||||
#include <driver/i2c_master.h>
|
|
||||||
#include <mutex>
|
|
||||||
|
|
||||||
// LVGL library is not thread-safe, this example calls LVGL APIs from tasks.
|
|
||||||
// We must use a mutex to protect it.
|
|
||||||
_lock_t ScopedLock::lock_;
|
|
||||||
|
|
||||||
Display::Display(IPanelDevice *device) :
|
|
||||||
panel_(device)
|
|
||||||
{
|
|
||||||
if (!lv_is_initialized()) {
|
|
||||||
ESP_LOGI(TAG, "Initialize LVGL");
|
|
||||||
lv_init();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a lvgl display.
|
|
||||||
lv_display_ = panel_.device_->create_display();
|
|
||||||
// associate the i2c panel handle to the display
|
|
||||||
lv_display_set_user_data(lv_display_, panel_.esp_panel_);
|
|
||||||
|
|
||||||
// Create draw buffer.
|
|
||||||
ESP_LOGI(TAG, "Allocate separate LVGL draw buffers");
|
|
||||||
lv_buf_ = heap_caps_calloc(1, panel_.device_->lv_buf_size_,
|
|
||||||
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
|
||||||
assert(lv_buf_);
|
|
||||||
|
|
||||||
// LVGL9 suooprt new monochromatic format.
|
|
||||||
lv_display_set_color_format(lv_display_, LV_COLOR_FORMAT_I1);
|
|
||||||
// Initialize LVGL draw buffers.
|
|
||||||
lv_display_set_buffers(lv_display_, lv_buf_, nullptr,
|
|
||||||
panel_.device_->lv_buf_size_,
|
|
||||||
LV_DISPLAY_RENDER_MODE_FULL);
|
|
||||||
lv_display_set_rotation(lv_display_, LV_DISPLAY_ROTATION_0);
|
|
||||||
// Set callback which can copy the rendered image to an area of the display.
|
|
||||||
lv_display_set_flush_cb(lv_display_, Display::lvgl_flush_cb);
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Register io panel callback for LVGL flush ready notification");
|
|
||||||
const esp_lcd_panel_io_callbacks_t cbs = {
|
|
||||||
.on_color_trans_done = Display::lvgl_flush_ready,
|
|
||||||
};
|
|
||||||
/* Register done callback */
|
|
||||||
ESP_ERROR_CHECK(
|
|
||||||
esp_lcd_panel_io_register_event_callbacks(panel_.io_handle_, &cbs,
|
|
||||||
lv_display_));
|
|
||||||
|
|
||||||
// TODO: What is this
|
|
||||||
ESP_LOGI(TAG, "Use esp_timer as LVGL tick timer");
|
|
||||||
const esp_timer_create_args_t lvgl_tick_timer_args = {
|
|
||||||
.callback = &Display::lvgl_increase_tick,
|
|
||||||
.name = "lvgl_tick"
|
|
||||||
};
|
|
||||||
esp_timer_handle_t lvgl_tick_timer = nullptr;
|
|
||||||
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
|
|
||||||
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer,
|
|
||||||
LVGL_TICK_PERIOD_MS * 1000));
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Create LVGL task");
|
|
||||||
xTaskCreate(Display::lvgl_port_task, "LVGL", LVGL_TASK_STACK_SIZE,
|
|
||||||
nullptr, LVGL_TASK_PRIORITY, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Display::set_text(const char *text,
|
|
||||||
const char *name,
|
|
||||||
lv_label_long_mode_t long_mode,
|
|
||||||
lv_align_t align)
|
|
||||||
{
|
|
||||||
// Lock the mutex due to the LVGL APIs are not thread-safe.
|
|
||||||
ScopedLock lock;
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Display LVGL Scroll Text");
|
|
||||||
lv_obj_t *scr = lv_display_get_screen_active(lv_display_);
|
|
||||||
objects_[name] = lv_label_create(scr);
|
|
||||||
|
|
||||||
// Circular scroll.
|
|
||||||
auto obj = objects_[name];
|
|
||||||
lv_label_set_long_mode(obj, long_mode);
|
|
||||||
lv_label_set_text(obj, text);
|
|
||||||
|
|
||||||
// Set the size of the screen.
|
|
||||||
// If you use rotation 90 or 270 use lv_display_get_vertical_resolution.
|
|
||||||
lv_obj_set_width(obj, lv_display_get_horizontal_resolution(lv_display_));
|
|
||||||
lv_obj_align(obj, align, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Display::lvgl_flush_ready(esp_lcd_panel_io_handle_t,
|
|
||||||
esp_lcd_panel_io_event_data_t *,
|
|
||||||
void *user_ctx)
|
|
||||||
{
|
|
||||||
auto *disp = (lv_display_t *) user_ctx;
|
|
||||||
lv_display_flush_ready(disp);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Display::lvgl_flush_cb(lv_display_t *display, const lv_area_t *area,
|
|
||||||
uint8_t *px_map)
|
|
||||||
{
|
|
||||||
auto panel_handle =
|
|
||||||
(esp_lcd_panel_handle_t) lv_display_get_user_data(display);
|
|
||||||
|
|
||||||
// Necessary because LVGL reserves 2x4 bytes in the buffer for a palette.
|
|
||||||
// For more information about the monochrome, please refer to:
|
|
||||||
// https://docs.lvgl.io/9.2/porting/display.html#monochrome-displays
|
|
||||||
// Skip the palette here.
|
|
||||||
px_map += LVGL_PALETTE_SIZE;
|
|
||||||
|
|
||||||
uint16_t hor_res = lv_display_get_physical_horizontal_resolution(display);
|
|
||||||
int32_t x1 = area->x1;
|
|
||||||
int32_t x2 = area->x2;
|
|
||||||
int32_t y1 = area->y1;
|
|
||||||
int32_t y2 = area->y2;
|
|
||||||
|
|
||||||
for (int32_t y = y1; y <= y2; y++) {
|
|
||||||
for (int32_t x = x1; x <= x2; x++) {
|
|
||||||
/* The order of bits is MSB first.
|
|
||||||
MSB LSB
|
|
||||||
bits 7 6 5 4 3 2 1 0
|
|
||||||
pixels 0 1 2 3 4 5 6 7
|
|
||||||
Left Right
|
|
||||||
*/
|
|
||||||
bool chroma_color = (px_map[(hor_res >> 3) * y + (x >> 3)] &
|
|
||||||
1 << (7 - x % 8));
|
|
||||||
|
|
||||||
// Write to the buffer as required for the display.
|
|
||||||
// It writes only 1-bit for monochrome displays mapped vertically.
|
|
||||||
uint8_t *buf = SSD1306::oled_buffer_ + hor_res * (y >> 3) + (x);
|
|
||||||
if (chroma_color) {
|
|
||||||
(*buf) &= ~(1 << (y % 8));
|
|
||||||
} else {
|
|
||||||
(*buf) |= (1 << (y % 8));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Pass the draw buffer to the driver.
|
|
||||||
ESP_ERROR_CHECK(
|
|
||||||
esp_lcd_panel_draw_bitmap(panel_handle, x1, y1, x2 + 1, y2 + 1,
|
|
||||||
SSD1306::oled_buffer_));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Display::lvgl_increase_tick(void *)
|
|
||||||
{
|
|
||||||
// Tell LVGL how many milliseconds has elapsed
|
|
||||||
lv_tick_inc(LVGL_TICK_PERIOD_MS);
|
|
||||||
}
|
|
||||||
|
|
||||||
[[noreturn]] void Display::lvgl_port_task(void *)
|
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "Starting LVGL task");
|
|
||||||
for (uint32_t time_till_next_ms = 0; true;) {
|
|
||||||
_lock_acquire(&ScopedLock::lock_);
|
|
||||||
time_till_next_ms = lv_timer_handler();
|
|
||||||
_lock_release(&ScopedLock::lock_);
|
|
||||||
usleep(1000 * time_till_next_ms);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
I2C::I2C(gpio_num_t sda, gpio_num_t scl) :
|
|
||||||
i2c_bus_(nullptr),
|
|
||||||
bus_config_(
|
|
||||||
(i2c_master_bus_config_t) {
|
|
||||||
.i2c_port = I2C_BUS_PORT,
|
|
||||||
.sda_io_num = sda,
|
|
||||||
.scl_io_num = scl,
|
|
||||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
|
||||||
.glitch_ignore_cnt = 7,
|
|
||||||
.flags {
|
|
||||||
.enable_internal_pullup = true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "Initialize I2C bus");
|
|
||||||
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config_, &i2c_bus_));
|
|
||||||
}
|
|
||||||
|
|
||||||
Panel::Panel(IPanelDevice *device) :
|
|
||||||
device_(device),
|
|
||||||
io_handle_(nullptr),
|
|
||||||
esp_panel_(nullptr),
|
|
||||||
// According to SSD1306 datasheet
|
|
||||||
panel_config_(
|
|
||||||
(esp_lcd_panel_dev_config_t) {
|
|
||||||
.reset_gpio_num = device_->reset_gpio_num_,
|
|
||||||
.bits_per_pixel = 1,
|
|
||||||
// .vendor_config should be set in IPanelDevice::init_panel override
|
|
||||||
}
|
|
||||||
)
|
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "Install panel IO");
|
|
||||||
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(
|
|
||||||
device_->i2c_bus_, &device_->io_config_, &io_handle_));
|
|
||||||
|
|
||||||
device_->create_panel(panel_config_, io_handle_, esp_panel_);
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Resetting panel display");
|
|
||||||
ESP_ERROR_CHECK(esp_lcd_panel_reset(esp_panel_));
|
|
||||||
ESP_LOGI(TAG, "Initializing panel display");
|
|
||||||
ESP_ERROR_CHECK(esp_lcd_panel_init(esp_panel_));
|
|
||||||
ESP_LOGI(TAG, "Turning on panel display");
|
|
||||||
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(esp_panel_, true));
|
|
||||||
}
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
#ifndef DISPLAY_H
|
|
||||||
#define DISPLAY_H
|
|
||||||
|
|
||||||
#include <esp_lcd_types.h>
|
|
||||||
#include <esp_lcd_panel_ssd1306.h>
|
|
||||||
#include <driver/i2c_types.h>
|
|
||||||
#include <driver/i2c_master.h>
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <esp_lcd_panel_ops.h>
|
|
||||||
#include <esp_lcd_panel_io.h>
|
|
||||||
#include "misc/lv_types.h"
|
|
||||||
#include "misc/lv_area.h"
|
|
||||||
#include "display/lv_display.h"
|
|
||||||
#include "widgets/label/lv_label.h"
|
|
||||||
|
|
||||||
#include "panel_device.h"
|
|
||||||
|
|
||||||
#define I2C_BUS_PORT 0
|
|
||||||
#define LVGL_TICK_PERIOD_MS 5
|
|
||||||
#define LVGL_TASK_STACK_SIZE (4 * 1024)
|
|
||||||
#define LVGL_TASK_PRIORITY 2
|
|
||||||
|
|
||||||
struct I2C {
|
|
||||||
I2C(gpio_num_t sda, gpio_num_t scl);
|
|
||||||
|
|
||||||
~I2C() = default;
|
|
||||||
|
|
||||||
i2c_master_bus_handle_t i2c_bus_;
|
|
||||||
|
|
||||||
private:
|
|
||||||
i2c_master_bus_config_t bus_config_;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ScopedLock {
|
|
||||||
explicit ScopedLock() { _lock_acquire(&lock_); }
|
|
||||||
|
|
||||||
~ScopedLock() { _lock_release(&lock_); }
|
|
||||||
|
|
||||||
// LVGL library is not thread-safe, this example calls LVGL APIs from tasks.
|
|
||||||
// We must use a mutex to protect it.
|
|
||||||
static _lock_t lock_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Panel {
|
|
||||||
public:
|
|
||||||
explicit Panel(IPanelDevice *device);
|
|
||||||
|
|
||||||
~Panel() = default;
|
|
||||||
|
|
||||||
IPanelDevice *device_;
|
|
||||||
|
|
||||||
esp_lcd_panel_io_handle_t io_handle_;
|
|
||||||
|
|
||||||
esp_lcd_panel_handle_t esp_panel_;
|
|
||||||
|
|
||||||
private:
|
|
||||||
esp_lcd_panel_dev_config_t panel_config_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Display {
|
|
||||||
public:
|
|
||||||
explicit Display(IPanelDevice *device);
|
|
||||||
|
|
||||||
~Display() = default;
|
|
||||||
|
|
||||||
[[nodiscard]] inline const lv_display_t *get() const { return lv_display_; }
|
|
||||||
|
|
||||||
[[nodiscard]] inline lv_display_t *get() { return lv_display_; }
|
|
||||||
|
|
||||||
[[nodiscard]] inline const lv_display_t *operator*() const { return get(); }
|
|
||||||
|
|
||||||
[[nodiscard]] inline lv_display_t *operator*() { return get(); }
|
|
||||||
|
|
||||||
void set_text(const char *text,
|
|
||||||
const char *name,
|
|
||||||
lv_label_long_mode_t long_mode = LV_LABEL_LONG_SCROLL_CIRCULAR,
|
|
||||||
lv_align_t align = LV_ALIGN_TOP_MID);
|
|
||||||
|
|
||||||
static bool lvgl_flush_ready(esp_lcd_panel_io_handle_t panel,
|
|
||||||
esp_lcd_panel_io_event_data_t *data,
|
|
||||||
void *user_ctx);
|
|
||||||
|
|
||||||
static void lvgl_flush_cb(lv_display_t *display,
|
|
||||||
const lv_area_t *area,
|
|
||||||
uint8_t *px_map);
|
|
||||||
|
|
||||||
static void lvgl_increase_tick(void *arg);
|
|
||||||
|
|
||||||
[[noreturn]] static void lvgl_port_task(void *arg);
|
|
||||||
|
|
||||||
private:
|
|
||||||
Panel panel_;
|
|
||||||
|
|
||||||
lv_display_t *lv_display_;
|
|
||||||
|
|
||||||
// Draw buffer associated with the lv_display_t.
|
|
||||||
void *lv_buf_;
|
|
||||||
|
|
||||||
// Objects stored in the screen associated with this display.
|
|
||||||
// @sa Display::set_text
|
|
||||||
// @sa lv_display_get_screen_active
|
|
||||||
std::unordered_map<const char *, lv_obj_t *> objects_;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // DISPLAY_H
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
## IDF Component Manager Manifest File
|
|
||||||
dependencies:
|
|
||||||
espressif/arduino-esp32: ^3.1.1
|
|
||||||
lvgl/lvgl: "9.2.0"
|
|
||||||
esp_lcd_sh1107: "^1"
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
#include "display.h"
|
|
||||||
#include "ssd1306.h"
|
|
||||||
|
|
||||||
// Pin may vary based on your schematic.
|
|
||||||
#define PIN_SDA GPIO_NUM_21
|
|
||||||
#define PIN_SCL GPIO_NUM_22
|
|
||||||
#define PIN_RST -1
|
|
||||||
|
|
||||||
// TODO: Can this be static since there can only be one initialization?
|
|
||||||
// TODO: Store RST in I2C and retrieve within SSD instead of the #define
|
|
||||||
I2C i2c(PIN_SDA, PIN_SCL);
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
SSD1306 ssd1306(i2c.i2c_bus_);
|
|
||||||
Display d(&ssd1306);
|
|
||||||
|
|
||||||
d.set_text("Test test 12345678910",
|
|
||||||
"test-text1",
|
|
||||||
LV_LABEL_LONG_SCROLL,
|
|
||||||
LV_ALIGN_CENTER);
|
|
||||||
|
|
||||||
d.set_text("Hello hello hello hello hello hello hello hello!", "test-text2");
|
|
||||||
|
|
||||||
d.set_text("A random sentence with no meaning at all.",
|
|
||||||
"test-text3",
|
|
||||||
LV_LABEL_LONG_CLIP,
|
|
||||||
LV_ALIGN_BOTTOM_MID);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() { }
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
#ifndef MAIN_H
|
|
||||||
#define MAIN_H
|
|
||||||
|
|
||||||
#endif // MAIN_H
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
|
|
||||||
#include "panel_device.h"
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
#ifndef PANEL_DEVICE_H
|
|
||||||
#define PANEL_DEVICE_H
|
|
||||||
|
|
||||||
#include <esp_lcd_panel_dev.h>
|
|
||||||
#include <esp_lcd_panel_ops.h>
|
|
||||||
#include <esp_lcd_panel_ssd1306.h>
|
|
||||||
#include <driver/i2c_types.h>
|
|
||||||
#include <esp_lcd_panel_io.h>
|
|
||||||
#include <esp_log.h>
|
|
||||||
#include "display/lv_display.h"
|
|
||||||
|
|
||||||
#define LVGL_PALETTE_SIZE 8
|
|
||||||
|
|
||||||
static const char *TAG = "lcd-panel";
|
|
||||||
|
|
||||||
class IPanelDevice {
|
|
||||||
public:
|
|
||||||
explicit IPanelDevice(i2c_master_bus_handle_t i2c,
|
|
||||||
int reset_gpio_num,
|
|
||||||
esp_lcd_panel_io_i2c_config_t io_config) :
|
|
||||||
reset_gpio_num_(reset_gpio_num),
|
|
||||||
i2c_bus_(i2c),
|
|
||||||
io_config_(io_config) { }
|
|
||||||
|
|
||||||
virtual ~IPanelDevice() = default;
|
|
||||||
|
|
||||||
[[nodiscard]] lv_display_t *create_display() const
|
|
||||||
{
|
|
||||||
auto display = lv_display_create(width_, height_);
|
|
||||||
assert(display);
|
|
||||||
return display;
|
|
||||||
}
|
|
||||||
|
|
||||||
void create_panel(esp_lcd_panel_dev_config_t &config,
|
|
||||||
esp_lcd_panel_io_handle_t io,
|
|
||||||
esp_lcd_panel_handle_t &panel)
|
|
||||||
{
|
|
||||||
// If the passed handle is already allocated, delete it.
|
|
||||||
if (panel != nullptr) {
|
|
||||||
ESP_LOGI(TAG, "Removing unused panel");
|
|
||||||
esp_lcd_panel_del(panel);
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Install SSD1306 panel driver");
|
|
||||||
init_panel(config, io, panel);
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t width_;
|
|
||||||
int32_t height_;
|
|
||||||
int reset_gpio_num_;
|
|
||||||
|
|
||||||
// LVGL reserves 2x4 bytes in the buffer to be used as a palette.
|
|
||||||
size_t lv_buf_size_;
|
|
||||||
// TODO: Can we use a static accessor in I2C instead?
|
|
||||||
i2c_master_bus_handle_t i2c_bus_;
|
|
||||||
|
|
||||||
esp_lcd_panel_io_i2c_config_t io_config_;
|
|
||||||
|
|
||||||
private:
|
|
||||||
virtual void init_panel(esp_lcd_panel_dev_config_t &config,
|
|
||||||
esp_lcd_panel_io_handle_t io,
|
|
||||||
esp_lcd_panel_handle_t &panel) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // PANEL_DEVICE_H
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
|
|
||||||
#include "ssd1306.h"
|
|
||||||
|
|
||||||
// To use LV_COLOR_FORMAT_I1 we need an extra buffer to hold the converted data.
|
|
||||||
uint8_t SSD1306::oled_buffer_[LCD_H_RES * LCD_V_RES / 8];
|
|
||||||
|
|
||||||
SSD1306::SSD1306(i2c_master_bus_handle_t i2c,
|
|
||||||
esp_lcd_panel_ssd1306_config_t config,
|
|
||||||
int width,
|
|
||||||
int height) :
|
|
||||||
IPanelDevice(i2c,
|
|
||||||
PIN_RST,
|
|
||||||
(esp_lcd_panel_io_i2c_config_t) {
|
|
||||||
.dev_addr = I2C_HW_ADDR,
|
|
||||||
.control_phase_bytes = 1,
|
|
||||||
.dc_bit_offset = 6,
|
|
||||||
.lcd_cmd_bits = LCD_CMD_BITS,
|
|
||||||
.lcd_param_bits = LCD_CMD_BITS,
|
|
||||||
.scl_speed_hz = LCD_PIXEL_CLOCK_HZ,
|
|
||||||
}
|
|
||||||
),
|
|
||||||
ssd1306_config_(config)
|
|
||||||
{
|
|
||||||
this->width_ = width;
|
|
||||||
this->height_ = height;
|
|
||||||
this->reset_gpio_num_ = PIN_RST;
|
|
||||||
this->lv_buf_size_ = width_ * height_ / 8 + LVGL_PALETTE_SIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SSD1306::init_panel(esp_lcd_panel_dev_config_t &config,
|
|
||||||
esp_lcd_panel_io_handle_t io,
|
|
||||||
esp_lcd_panel_handle_t &panel)
|
|
||||||
{
|
|
||||||
// Allocate SSD1306 panel handle.
|
|
||||||
config.vendor_config = &ssd1306_config_;
|
|
||||||
ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(io, &config, &panel));
|
|
||||||
}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
#ifndef SSD1306_H
|
|
||||||
#define SSD1306_H
|
|
||||||
|
|
||||||
#include <esp_lcd_panel_ssd1306.h>
|
|
||||||
|
|
||||||
#include "panel_device.h"
|
|
||||||
|
|
||||||
// According to SSD1306 datasheet
|
|
||||||
// https://www.digikey.com/en/products/detail/winstar-display/WEA012864DWPP3N00003/20533255
|
|
||||||
// Bit number used to represent command and parameter
|
|
||||||
#define SCREEN_WIDTH 128 // OLED display width, in pixels.
|
|
||||||
#define SCREEN_HEIGHT 64 // OLED display height, in pixels.
|
|
||||||
#define LCD_H_RES SCREEN_WIDTH
|
|
||||||
#define LCD_V_RES SCREEN_HEIGHT
|
|
||||||
#define I2C_HW_ADDR 0x3C
|
|
||||||
#define LCD_PIXEL_CLOCK_HZ (400 * 1000)
|
|
||||||
#define LCD_CMD_BITS 8
|
|
||||||
#define LCD_PARAM_BITS 8
|
|
||||||
|
|
||||||
#define PIN_RST -1
|
|
||||||
|
|
||||||
class SSD1306 : public IPanelDevice {
|
|
||||||
public:
|
|
||||||
// Constructors allow overriding ssd1306 config.
|
|
||||||
explicit SSD1306(i2c_master_bus_handle_t i2c) :
|
|
||||||
SSD1306(i2c, {.height = SCREEN_HEIGHT}) { }
|
|
||||||
|
|
||||||
explicit SSD1306(i2c_master_bus_handle_t i2c,
|
|
||||||
esp_lcd_panel_ssd1306_config_t config,
|
|
||||||
int width = SCREEN_WIDTH,
|
|
||||||
int height = SCREEN_HEIGHT);
|
|
||||||
|
|
||||||
virtual ~SSD1306() = default;
|
|
||||||
|
|
||||||
// The configuration structure specific to the SSD1306.
|
|
||||||
esp_lcd_panel_ssd1306_config_t ssd1306_config_;
|
|
||||||
|
|
||||||
// For LV_COLOR_FORMAT_I1 we need an extra buffer to hold the converted data.
|
|
||||||
static uint8_t oled_buffer_[LCD_H_RES * LCD_V_RES / 8];
|
|
||||||
|
|
||||||
private:
|
|
||||||
void init_panel(esp_lcd_panel_dev_config_t &config,
|
|
||||||
esp_lcd_panel_io_handle_t io,
|
|
||||||
esp_lcd_panel_handle_t &panel) override;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // SSD1306_H
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 227 KiB |
File diff suppressed because it is too large
Load Diff
@@ -7,7 +7,6 @@ shaunrd0/klips/esp/
|
|||||||
├── 03_temp-humidity-web # Temperature and humidity sensor within a web browser.
|
├── 03_temp-humidity-web # Temperature and humidity sensor within a web browser.
|
||||||
├── 04_esp-idf-arduino # CMake example instead of Arduino IDE for ESP development.
|
├── 04_esp-idf-arduino # CMake example instead of Arduino IDE for ESP development.
|
||||||
├── 05_temp-humidity-web # Temperature and humidity sensor within a web browser.
|
├── 05_temp-humidity-web # Temperature and humidity sensor within a web browser.
|
||||||
├── 06_i2c-scanner # Simple I2C device scanner.
|
|
||||||
├── ESP32-basic-starter-kit.pdf # PDF for tutorials in ESP32 starter kit.
|
├── ESP32-basic-starter-kit.pdf # PDF for tutorials in ESP32 starter kit.
|
||||||
├── ESP32-dev-module.png
|
├── ESP32-dev-module.png
|
||||||
└── README.md
|
└── README.md
|
||||||
|
|||||||
Reference in New Issue
Block a user