Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit61c2fe8b7893267b786abfbca3e75e5c7e4d0eae (patch)
Author
 Syndamia <kamen@syndamia.com>
2024-05-04 18:25:29 +0300
Committer
 Syndamia <kamen@syndamia.com>
2024-05-04 18:25:29 +0300
Filese4b51cc639e0b8e63333e131641fb74ccd7796aa
uni-projects-61c2fe8b7893267b786abfbca3e75e5c7e4d0eae.tar
uni-projects-61c2fe8b7893267b786abfbca3e75e5c7e4d0eae.tar.gz
uni-projects-61c2fe8b7893267b786abfbca3e75e5c7e4d0eae.zip
Parent67936a330a93c734c275182142b8b6ba52501ede (diff)
[iot/goam] Exported ptc06 camera logic to it's own file
Also prepared for all future commands
diff --git a/Year 3/Internet of things/goam/main/CMakeLists.txt b/Year 3/Internet of things/goam/main/CMakeLists.txt
index 8a3ab69..6819229 100644
--- a/Year 3/Internet of things/goam/main/CMakeLists.txt
+++ b/Year 3/Internet of things/goam/main/CMakeLists.txt
@@ -1,2 +1,3 @@
idf_component_register(SRCS "main.c"
- INCLUDE_DIRS "")
+ "ptc06.c"
+ INCLUDE_DIRS "")
diff --git a/Year 3/Internet of things/goam/main/main.c b/Year 3/Internet of things/goam/main/main.c
index 607adb0..01d3cfa 100644
--- a/Year 3/Internet of things/goam/main/main.c
+++ b/Year 3/Internet of things/goam/main/main.c
@@ -14,7 +14,7 @@
#include "driver/gpio.h"
#include "driver/uart.h"
-#include <string.h>
+#include "ptc06.h"
void print_chip_data() {
/* Print chip information */
@@ -74,45 +74,7 @@ void app_main(void)
gpio_config(&o_conf);
-
-
-
- uart_config_t uart_config = {
- .baud_rate = 115200,
- .data_bits = UART_DATA_8_BITS,
- .parity = UART_PARITY_DISABLE,
- .stop_bits = UART_STOP_BITS_1,
- .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
- .rx_flow_ctrl_thresh = 122,
- };
- // Configure UART parameters
- ESP_ERROR_CHECK(uart_param_config(UART_NUM_2, &uart_config));
-
- // Set UART pins(TX: IO17 (UART2 default), RX: IO16 (UART2 default), RTS: IO18, CTS: IO19)
- ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, 17, 16, 18, 19));
-
- // Setup UART buffered IO with event queue
- QueueHandle_t uart_queue;
- // Install UART driver using an event queue here
- ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, 1024 * 2, 1024 * 2, 10, &uart_queue, 0));
-
- printf("camera boot...\n");
- vTaskDelay(3000 / portTICK_PERIOD_MS);
-
- // Write data to UART.
- const char reset[4] = { 0x56, 0x00, 0x26, 0x00 };
- int am = uart_write_bytes(UART_NUM_2, (const char*)reset, 4);
- ESP_ERROR_CHECK(uart_wait_tx_done(UART_NUM_2, 100));
- printf("sent %d\n", am);
-
- // Read data from UART.
- uint8_t data[5] = { 0, 0, 0, 0, 0 };
- int length = 4;
- ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_NUM_2, (size_t*)&am));
- length = uart_read_bytes(UART_NUM_2, data, length, 5);
- printf("got %d\n", length);
-
- printf("result: %d %d %d %d\n", data[0], data[1], data[2], data[3]);
+ ptc06_init();
for (int i = 0; true; i++) {
gpio_set_level(GPIO_OUTPUT_CAMERA, (i / 2) % 2);
diff --git a/Year 3/Internet of things/goam/main/ptc06.c b/Year 3/Internet of things/goam/main/ptc06.c
new file mode 100644
index 0000000..8eb62b8
--- /dev/null
+++ b/Year 3/Internet of things/goam/main/ptc06.c
@@ -0,0 +1,132 @@
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "driver/uart.h"
+#include "esp_log.h"
+/* #include "esp_check.h" */
+
+#include "ptc06.h"
+
+#define UART_NUM UART_NUM_2
+#define UART_TX 17
+#define UART_RX 16
+#define UART_RTS 18
+#define UART_CTS 19
+#define UART_BUFFER_SIZE 2048
+
+// Setup UART buffered IO with event queue
+static QueueHandle_t uart_queue;
+#define QUEUE_SIZE 10
+
+static const char* TAG = "ptc06";
+
+#define VARP 0xFA
+static const char comm_reset[] = { 0x56, 0x00, 0x26, 0x00 };
+static const char resp_reset[] = { 0x76, 0x00, 0x26, 0x00 };
+/* static const char comm_capture[] = { 0x56, 0x00, 0x36, 0x01, 0x00 }; */
+/* static const char resp_capture[] = { 0x76, 0x00, 0x36, 0x00, 0x00 }; */
+/* static const char comm_data_len[] = { 0x56, 0x00, 0x34, 0x01, 0x00 }; */
+/* static const char resp_data_len[] = { 0x76, 0x00, 0x34, 0x00, 0x04, 0x00, 0x00, VARP, VARP }; */
+/* static const char comm_read_data[] = { 0x56, 0x00, 0x32, 0x0C, 0x00, 0x0D, 0x00, 0x00, VARP, VARP, 0x00, 0x00, VARP, VARP, 0x00, 0x0A }; */
+/* static const char resp_read_data_start[] = { 0x76, 0x00, 0x32, 0x00, 0x00, 0xFF, 0xD8 }; */
+/* static const char resp_read_data_end[] = { 0xFF, 0xD9, 0x76, 0x00, 0x32, 0x00, 0x00 }; */
+/* static const char comm_stop_cap[] = { 0x56, 0x00, 0x36, 0x01, 0x03 }; */
+/* static const char resp_stop_cap[] = { 0x76, 0x00, 0x36, 0x00, 0x00 }; */
+/* static const char comm_set_compress[] = { 0x56, 0x00, 0x31, 0x05, 0x01, 0x01, 0x12, 0x04, VARP }; */
+/* static const char resp_set_compress[] = { 0x76, 0x00, 0x31, 0x00, 0x00 }; */
+/* static const char comm_set_res[] = { 0x56, 0x00, 0x31, 0x05, 0x04, 0x01, 0x00, 0x19, VARP }; */
+/* static const char resp_set_res[] = { 0x76, 0x00, 0x31, 0x00, 0x00 }; */
+/* static const char comm_set_baud[] = { 0x56, 0x00, 0x31, 0x06, 0x04, 0x02, 0x00, 0x08, VARP, VARP }; */
+/* static const char resp_set_baud[] = { 0x76, 0x00, 0x31, 0x00 }; */
+
+static void init_uart() {
+ uart_config_t uart_config = {
+ .baud_rate = 115200,
+ .data_bits = UART_DATA_8_BITS,
+ .parity = UART_PARITY_DISABLE,
+ .stop_bits = UART_STOP_BITS_1,
+ .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
+ .rx_flow_ctrl_thresh = 122,
+ };
+ // Configure UART parameters
+ ESP_ERROR_CHECK(uart_param_config(UART_NUM, &uart_config));
+
+ // Set UART pins(TX: IO17 (UART2 default), RX: IO16 (UART2 default), RTS: IO18, CTS: IO19)
+ ESP_ERROR_CHECK(uart_set_pin(UART_NUM, UART_TX, UART_RX, UART_RTS, UART_CTS));
+
+ // Install UART driver using an event queue here
+ ESP_ERROR_CHECK(uart_driver_install(UART_NUM, UART_BUFFER_SIZE, UART_BUFFER_SIZE, QUEUE_SIZE, &uart_queue, 0));
+}
+
+static void init_camera() {
+ vTaskDelay(2500 / portTICK_PERIOD_MS);
+}
+
+static bool resp_eq(const char a[], const char b[]) {
+ /* printf("%d\n", sizeof(a)); */
+ if (sizeof(a) != sizeof(b)) {
+ return false;
+ }
+
+ for (size_t i = 0; i < sizeof(a); i++) {
+ if (a[i] != VARP && b[i] != VARP && a[i] != b[i])
+ return false;
+ }
+ return true;
+}
+
+static void send_comm(const char comm[]) {
+ uart_write_bytes(UART_NUM, comm, sizeof(comm));
+ ESP_ERROR_CHECK(uart_wait_tx_done(UART_NUM, 100));
+}
+
+static void send_commXX(const char comm[], char XX) {
+ char copy[sizeof(comm)];
+ for (int i = 0; i < sizeof(comm); i++) {
+ if (comm[i] == VARP) {
+ copy[i] = XX;
+ }
+ else {
+ copy[i] = comm[i];
+ }
+ }
+ send_comm(copy);
+}
+
+static void send_commXXYY(const char comm[], char XX, char YY) {
+ char copy[sizeof(comm)];
+ bool passXX = true;
+ for (int i = 0; i < sizeof(comm); i++) {
+ if (comm[i] == VARP && passXX) {
+ copy[i] = XX;
+ passXX = false;
+ }
+ else if (comm[i] == VARP) {
+ copy[i] = YY;
+ }
+ else {
+ copy[i] = comm[i];
+ }
+ }
+ send_comm(copy);
+}
+
+static void recv_ack(const char ack[]) {
+ char data[sizeof(ack)];
+ int read = uart_read_bytes(UART_NUM, data, sizeof(data), 100);
+ /* printf("%d %d %d %d\n", data[0], data[1], data[2], data[3]); */
+ ESP_ERROR_CHECK(read < 0 ? ESP_FAIL : ESP_OK);
+ esp_err_t RESPONSE_WAS_THE_EXPECTED_ONE = !resp_eq(data, ack) ? ESP_ERR_INVALID_STATE : ESP_OK;
+ ESP_ERROR_CHECK(RESPONSE_WAS_THE_EXPECTED_ONE);
+}
+
+static void ptc06_reset() {
+ send_comm(comm_reset);
+ recv_ack(resp_reset);
+}
+
+void ptc06_init() {
+ init_uart();
+ init_camera();
+ ptc06_reset();
+ ESP_LOGI(TAG, "Initialized ptc06 successfully. UART tx: %d, rx: %d, rts: %d, cts: %d", UART_TX, UART_RX, UART_RTS, UART_CTS);
+}
diff --git a/Year 3/Internet of things/goam/main/ptc06.h b/Year 3/Internet of things/goam/main/ptc06.h
new file mode 100644
index 0000000..371775a
--- /dev/null
+++ b/Year 3/Internet of things/goam/main/ptc06.h
@@ -0,0 +1,3 @@
+#pragma once
+
+void ptc06_init();