Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • c3pb/GeavisionReverse
1 result
Show changes
# _Sample project_
(See the README.md file in the upper level 'examples' directory for more information about examples.)
This is the simplest buildable example. The example is used by command `idf.py create-project`
that copies the project to user specified path and set it's name. For more information follow the [docs page](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html#start-a-new-project)
## How to use example
We encourage the users to use the example as a template for the new projects.
A recommended way is to follow the instructions on a [docs page](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html#start-a-new-project).
## Example folder contents
The project **sample_project** contains one source file in C language [main.c](main/main.c). The file is located in folder [main](main).
ESP-IDF projects are built using CMake. The project build configuration is contained in `CMakeLists.txt`
files that provide set of directives and instructions describing the project's source files and targets
(executable, library, or both).
Below is short explanation of remaining files in the project folder.
```
├── CMakeLists.txt
├── main
│ ├── CMakeLists.txt
│ └── main.c
└── README.md This is the file you are currently reading
```
Additionally, the sample project contains Makefile and component.mk files, used for the legacy Make based build system.
They are not used or needed when building with CMake and idf.py.
idf_component_register(SRCS "main.cpp"
INCLUDE_DIRS "."
REQUIRES esp_wifi nvs_flash mqtt esp_driver_spi driver)
set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
\ No newline at end of file
menu "GeaVision"
config ESP_WIFI_SSID
string "WiFi SSID"
default "myssid"
help
SSID (network name).
config ESP_WIFI_PASSWORD
string "WiFi Password"
default "mypassword"
help
WiFi password.
config ESP_OTA_BASE_URL
string "Ota base url http://ota.local"
default "http://192.168.2.100"
help
Url form the orbiter webserver
config ESP_MQTT_URL
string "Mqtt base url mqtt://mqtt.local"
default "mqtt://192.168.2.100"
help
Url form the orbiter mqtt server
endmenu
\ No newline at end of file
#include <cmath>
#include <driver/ledc.h>
#include <esp_err.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include "driver/gpio.h"
#include "driver/spi_common.h"
#include "driver/spi_master.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "hal/spi_types.h"
#include "mqtt_client.h"
#include "nvs_flash.h"
extern "C" {
void app_main(void);
}
static const char* TAG = "GEA_VISION";
static const char* WIFI_TAG = "WIFI";
static const char* NVS_TAG = "NVS";
static const char* WIFI_EVENT_TAG = "WIFI_EVENT";
static const char* BLE_EVENT_TAG = "BLE_EVENT";
static const char* OTA_TAG = "OTA";
static const char* MQTT_TAG = "MQTT";
constexpr const ledc_mode_t LCD_LEDC_SPEED_MODE = LEDC_HIGH_SPEED_MODE;
constexpr const ledc_timer_t LCD_LEDC_TIMER = LEDC_TIMER_0;
constexpr const ledc_timer_bit_t LCD_LEDC_DUTY_RES = LEDC_TIMER_8_BIT;
constexpr const uint32_t LCD_LEDC_FREQUENCY = 64;
constexpr const ledc_channel_t LCD_LEDC_CHANNEL = LEDC_CHANNEL_0;
constexpr const uint8_t LCD_COUNT = 2;
constexpr const gpio_num_t LCD_MOSI = GPIO_NUM_25;
constexpr const gpio_num_t LCD_MISO = GPIO_NUM_NC;
constexpr const gpio_num_t LCD_SCLK = GPIO_NUM_33;
constexpr const gpio_num_t LCD_RESET = GPIO_NUM_26;
constexpr const gpio_num_t LCD_LATCH = GPIO_NUM_14;
constexpr const gpio_num_t LCD_CLOCK_64Hz = GPIO_NUM_27;
constexpr const spi_host_device_t LCD_SPI = SPI2_HOST;
esp_mqtt_client_handle_t client;
uint8_t mac[6] = {};
char hostname[64];
std::vector<uint8_t> data;
size_t buffer = LCD_COUNT * 40 / 8;
static void log_error_if_nonzero(const char* message, int error_code) {
if (error_code != 0) {
ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code);
}
}
static void mqtt_event_handler(void* handler_args, esp_event_base_t base, int32_t event_id, void* event_data) {
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id);
esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t) event_data;
esp_mqtt_client_handle_t client = event->client;
int msg_id;
switch ((esp_mqtt_event_id_t) event_id) {
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
// msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0);
// ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
// msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
// ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
// msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
// ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
// msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1");
// ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id);
// esp_mqtt_client_publish(client,
// topic.append(s_hostname).append().c_str(),
// "",
// 0,
// 0,
// 0);
msg_id = esp_mqtt_client_subscribe(client, "GeaVision/data/raw", 0);
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
break;
case MQTT_EVENT_SUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
// msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0);
// ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
break;
case MQTT_EVENT_UNSUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_PUBLISHED:
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_DATA:
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
printf("DATA=%.*s\r\n", event->data_len, event->data);
printf("DATA_len=%d\r\n", event->data_len);
data = std::vector<uint8_t>(event->data, event->data + event->data_len);
for (size_t i = 0; i < event->data_len; i++) {
if (i == 0) {
printf("RAW=");
}
printf(" %hhx", data[i]);
if (i == event->data_len - 1) {
printf("\n");
}
}
break;
case MQTT_EVENT_ERROR:
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err);
log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno);
ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
}
break;
default:
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
break;
}
}
std::string ip = "";
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
// ESP_LOGI(WIFI_EVENT_TAG, "%s_EVENT: id = %" PRIi32, event_base == WIFI_EVENT ? "WIFI" : "IP", event_id);
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
esp_wifi_connect();
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip: " IPSTR, IP2STR(&event->ip_info.ip));
// ip = &event->ip_info.ip;
esp_mqtt_client_start(client);
}
}
void spi_pre_transfer_callback(spi_transaction_t* t) {
int dc = (int) t->user;
gpio_set_level(LCD_LATCH, dc);
// ESP_LOGI(TAG, "DC: %d", dc);
}
void spi_post_transfer_callback(spi_transaction_t* t) {
int dc = (int) t->user;
if (dc == 1)
gpio_set_level(LCD_LATCH, 1);
vTaskDelay(1 / portTICK_PERIOD_MS);
gpio_set_level(LCD_LATCH, 0);
// ESP_LOGI(TAG, "DC: %d", dc);
}
spi_device_handle_t lcd_init() {
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1ULL << LCD_RESET) | (1ULL << LCD_LATCH);
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
gpio_config(&io_conf);
ledc_timer_config_t ledc_timer1 = {.speed_mode = LCD_LEDC_SPEED_MODE,
.duty_resolution = LCD_LEDC_DUTY_RES,
.timer_num = LCD_LEDC_TIMER,
.freq_hz = 64,
.clk_cfg = LEDC_AUTO_CLK};
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer1));
ledc_channel_config_t ledc_channel_lcd = {.gpio_num = LCD_CLOCK_64Hz,
.speed_mode = LCD_LEDC_SPEED_MODE,
.channel = LCD_LEDC_CHANNEL,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = LCD_LEDC_TIMER,
.duty = (uint32_t) pow(2, LCD_LEDC_DUTY_RES) / 2,
.hpoint = 0,
.flags = {.output_invert = 0}};
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel_lcd));
// ledc_fade_func_install(0);
ESP_ERROR_CHECK(ledc_set_duty(LCD_LEDC_SPEED_MODE, LCD_LEDC_CHANNEL, (uint32_t) pow(2, LCD_LEDC_DUTY_RES) / 2));
ESP_ERROR_CHECK(ledc_update_duty(LCD_LEDC_SPEED_MODE, LCD_LEDC_CHANNEL));
esp_err_t ret;
spi_device_handle_t spi;
spi_bus_config_t buscfg = {
.mosi_io_num = LCD_MOSI,
.miso_io_num = LCD_MISO,
.sclk_io_num = LCD_SCLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = LCD_COUNT * 40,
};
spi_device_interface_config_t devcfg = {
.mode = 0, // SPI mode 0
.clock_speed_hz = 130000,
.spics_io_num = GPIO_NUM_NC,
.queue_size = 7, // We want to be able to queue 7 transactions at a time
// .pre_cb = spi_pre_transfer_callback,
.post_cb = spi_post_transfer_callback,
};
// Initialize the SPI bus
ret = spi_bus_initialize(LCD_SPI, &buscfg, SPI_DMA_CH_AUTO);
ESP_ERROR_CHECK(ret);
// Attach the device to the SPI bus
ret = spi_bus_add_device(LCD_SPI, &devcfg, &spi);
ESP_ERROR_CHECK(ret);
return spi;
}
void lcd_write(spi_device_handle_t spi, const uint8_t* data, int len) {
esp_err_t ret;
spi_transaction_t t;
if (len == 0) {
return; // no need to send anything
}
memset(&t, 0, sizeof(t)); // Zero out the transaction
t.length = len * 8; // Len is in bytes, transaction length is in bits.
t.tx_buffer = data; // Data
t.user = (void*) 1; // D/C needs to be set to 1
ret = spi_device_polling_transmit(spi, &t); // Transmit!
assert(ret == ESP_OK); // Should have had no issues.
}
// struct GV33 {
// uint8_t SEG_1 : 1 = 0;
// uint8_t SEG_2 : 1 = 0;
// uint8_t SEG_3 : 1 = 0;
// uint8_t SEG_4 : 1 = 0;
// uint8_t SEG_5 : 1 = 0;
// uint8_t SEG_6 : 1 = 0;
// uint8_t SEG_7 : 1 = 0;
// uint8_t SEG_8 : 1 = 0;
// uint8_t SEG_9 : 1 = 0;
// uint8_t SEG_10 : 1 = 0;
// uint8_t SEG_11 : 1 = 0;
// uint8_t SEG_12 : 1 = 0;
// uint8_t SEG_13 : 1 = 0;
// uint8_t SEG_14 : 1 = 0;
// uint8_t SEG_15 : 1 = 0;
// uint8_t SEG_16 : 1 = 0;
// uint8_t SEG_17 : 1 = 0;
// uint8_t SEG_18 : 1 = 0;
// uint8_t SEG_19 : 1 = 0;
// uint8_t SEG_20 : 1 = 0;
// uint8_t SEG_21 : 1 = 0;
// uint8_t SEG_22 : 1 = 0;
// uint8_t SEG_23 : 1 = 0;
// uint8_t SEG_24 : 1 = 0;
// uint8_t SEG_25 : 1 = 0;
// uint8_t SEG_26 : 1 = 0;
// uint8_t SEG_27 : 1 = 0;
// uint8_t SEG_28 : 1 = 0;
// uint8_t SEG_29 : 1 = 0;
// uint8_t SEG_30 : 1 = 0;
// uint8_t SEG_31 : 1 = 0;
// uint8_t SEG_32 : 1 = 0;
// uint8_t SEG_33 : 1 = 0;
// uint8_t SEG_34 : 1 = 0;
// uint8_t SEG_35 : 1 = 0;
// uint8_t SEG_36 : 1 = 0;
// uint8_t SEG_37 : 1 = 0;
// uint8_t SEG_38 : 1 = 0;
// uint8_t SEG_39 : 1 = 0;
// uint8_t SEG_40 : 1 = 0;
// };
// uint64_t lcd_map() {
// }
void app_main(void) {
esp_log_level_set("wifi", ESP_LOG_WARN);
esp_log_level_set("wifi_init", ESP_LOG_WARN);
// app_desc = esp_app_get_description();
ESP_LOGI(TAG, "Starting WLAN MAIN APP.");
ESP_LOGI(NVS_TAG, "NVS INIT!");
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_LOGI(NVS_TAG, "NVS ERROR: %s", esp_err_to_name(err));
ESP_LOGI(NVS_TAG, "NVS ERASE!");
ESP_ERROR_CHECK(nvs_flash_erase());
ESP_LOGI(NVS_TAG, "NVS INIT!");
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_t* sta_netif = esp_netif_create_default_wifi_sta();
assert(sta_netif);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(
esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
ESP_ERROR_CHECK(
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
// ESP_ERROR_CHECK(esp_event_handler_register(ESP_HTTPS_OTA_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
wifi_config_t wifi_config;
memset(&wifi_config, 0, sizeof(wifi_config_t)); // Speicher sauber machen damit kein mist drin steht.
memcpy(wifi_config.sta.ssid, CONFIG_ESP_WIFI_SSID, std::min(strlen(CONFIG_ESP_WIFI_SSID), size_t(32)));
memcpy(wifi_config.sta.password, CONFIG_ESP_WIFI_PASSWORD, std::min(strlen(CONFIG_ESP_WIFI_PASSWORD), size_t(64)));
wifi_config.sta.rm_enabled = true;
wifi_config.sta.btm_enabled = true;
wifi_config.sta.rm_enabled = true;
wifi_config.sta.pmf_cfg.capable = true;
wifi_config.sta.pmf_cfg.required = false;
wifi_config.sta.scan_method = WIFI_FAST_SCAN;
wifi_config.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
wifi_config.sta.threshold.rssi = -127;
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_WIFI_STA));
snprintf(hostname, 64, "esp_%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
ESP_LOGI(WIFI_TAG, "HOSTNAME: %s", hostname);
ESP_LOGI(WIFI_TAG, "MAC: %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
ESP_LOGI(WIFI_TAG, "SSID: %s", wifi_config.sta.ssid);
// ESP_LOGI(WIFI_TAG, "PSK: %s", wifi_config.sta.password);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, hostname));
ESP_ERROR_CHECK(esp_wifi_start());
std::string mqtt_url = std::string(CONFIG_ESP_MQTT_URL);
ESP_LOGI(MQTT_TAG, "MQTT URL: %s", mqtt_url.c_str());
const esp_mqtt_client_config_t mqtt_cfg = {
.broker =
{
.address =
{
.uri = mqtt_url.c_str(),
},
},
};
client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(client, MQTT_EVENT_ANY, mqtt_event_handler, client);
ESP_LOGI(TAG, "LCD INTI");
spi_device_handle_t spi;
spi = lcd_init();
// GV33 display;
data.resize(buffer);
data = {
0b10100000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b10100000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
};
while (1) {
lcd_write(spi, data.data(), data.size());
vTaskDelay(200 / portTICK_PERIOD_MS);
}
// while (1) {
// for (uint8_t j = 0; j < 40; j++) {
// for (uint8_t i = 0; i < buffer; i++) {
// data[i] = 0x00;
// }
// data[(uint8_t) j / 8] = 0x01 << (uint8_t) j % 8;
// data[0] = data[0] | 0b10000000;
// lcd_write(spi, data.data(), data.size());
// vTaskDelay(200 / portTICK_PERIOD_MS);
// }
// }
}
\ No newline at end of file