diff options
| Commit | fb789d59604ae4141cf56981beec4293b235f55b (patch) | |
|---|---|---|
| Author | 2024-05-05 08:21:29 +0300 | |
| Committer | 2024-05-05 08:21:29 +0300 | |
| Files | c6d659a54491c12f9f325893bbcd64498924e8cf | |
| uni-projects-fb789d59604ae4141cf56981beec4293b235f55b.tar uni-projects-fb789d59604ae4141cf56981beec4293b235f55b.tar.gz uni-projects-fb789d59604ae4141cf56981beec4293b235f55b.zip | ||
| Parent | 61c2fe8b7893267b786abfbca3e75e5c7e4d0eae (diff) | |
[iot/goam] Failed logic for taking snapshots
3 files changed, 171 insertions, 52 deletions
| -rw-r--r-- | Year 3/Internet of things/goam/main/main.c | 6 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/ptc06.c | 215 | ||||
| -rw-r--r-- | Year 3/Internet of things/goam/main/ptc06.h | 2 |
diff --git a/Year 3/Internet of things/goam/main/main.c b/Year 3/Internet of things/goam/main/main.c index 01d3cfa..9b08713 100644 --- a/Year 3/Internet of things/goam/main/main.c +++ b/Year 3/Internet of things/goam/main/main.c @@ -75,11 +75,13 @@ void app_main(void) ptc06_init(); + char* img = ptc06_shoot_photo(); + free(img); for (int i = 0; true; i++) { - gpio_set_level(GPIO_OUTPUT_CAMERA, (i / 2) % 2); + ptc06_reset(); printf("%d seconds, %d\n", i, gpio_get_level(GPIO_INPUT_CAMERA)); - vTaskDelay(1000 / portTICK_PERIOD_MS); + /* vTaskDelay(1000 / portTICK_PERIOD_MS); */ } printf("Restarting now.\n"); diff --git a/Year 3/Internet of things/goam/main/ptc06.c b/Year 3/Internet of things/goam/main/ptc06.c index 8eb62b8..652a065 100644 --- a/Year 3/Internet of things/goam/main/ptc06.c +++ b/Year 3/Internet of things/goam/main/ptc06.c @@ -20,113 +20,228 @@ static QueueHandle_t uart_queue; 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_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, 0x00, 0x00, 0x00, VARP, VARP, 0x00, 0x00, VARP, VARP, 0x30, 0x00 }; +static const char resp_read_data[] = { 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 size_t size(const char* a) { + if (a == comm_reset ) return 4; + if (a == resp_reset ) return 4; + if (a == comm_capture ) return 5; + if (a == resp_capture ) return 5; + if (a == comm_data_len ) return 5; + if (a == resp_data_len ) return 9; + if (a == comm_read_data ) return 16; + if (a == resp_read_data ) return 5; + if (a == comm_stop_cap ) return 5; + if (a == resp_stop_cap ) return 5; + if (a == comm_set_compress) return 9; + if (a == resp_set_compress) return 5; + if (a == comm_set_res ) return 9; + if (a == resp_set_res ) return 5; + return 0; +} + 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, + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, + .source_clk = UART_SCLK_DEFAULT, }; // 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)); + ESP_ERROR_CHECK(uart_set_pin(UART_NUM, UART_TX, UART_RX, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); // 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)); + + uart_flush(UART_NUM); } static void init_camera() { vTaskDelay(2500 / portTICK_PERIOD_MS); } -static bool resp_eq(const char a[], const char b[]) { +static void fully_flush_uart() { + vTaskDelay(100 / portTICK_PERIOD_MS); + size_t length; + while (length > 0) { + uart_flush(UART_NUM); + vTaskDelay(1000 / portTICK_PERIOD_MS); + ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_NUM, &length)); + } +} + +#include <stdio.h> + +static bool resp_eq(const char a[], size_t sizea, const char b[], size_t sizeb) { /* printf("%d\n", sizeof(a)); */ - if (sizeof(a) != sizeof(b)) { + if (sizea != sizeb) { return false; } - for (size_t i = 0; i < sizeof(a); i++) { + for (size_t i = 0; i < sizea; 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)); +static void send_commSIZE(const char comm[], size_t size) { + uart_write_bytes(UART_NUM, comm, size); 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++) { +static void send_comm(const char comm[]) { + send_commSIZE(comm, size(comm)); +} + +static void send_commVARS(const char comm[], const char vars[]) { + size_t commsize = size(comm); + char copy[commsize]; + printf("send: "); + size_t varsi = 0; + for (size_t i = 0; i < commsize; i++) { if (comm[i] == VARP) { - copy[i] = XX; + copy[i] = vars[varsi++]; } else { copy[i] = comm[i]; } + printf("%X ", copy[i]); } - send_comm(copy); + printf("\n"); + send_commSIZE(copy, commsize); } -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]; - } +static void recv_ackXXYY(const char ack[], char* XX, char* YY) { + char data[size(ack)]; + int read = uart_read_bytes(UART_NUM, data, size(ack), 100); + printf("recv: "); + for (int i = 0; i < size(ack); i++) { + printf("%X ", data[i]); } - send_comm(copy); + printf("\n"); + + ESP_ERROR_CHECK(read < 0 ? ESP_FAIL : ESP_OK); + esp_err_t RESPONSE_WAS_NOT_THE_EXPECTED_ONE = !resp_eq(data, size(ack), ack, size(ack)) ? ESP_ERR_INVALID_STATE : ESP_OK; + ESP_ERROR_CHECK(RESPONSE_WAS_NOT_THE_EXPECTED_ONE); + + if (XX) *XX = data[size(ack)-2]; + if (YY) *YY = data[size(ack)-1]; } 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); + recv_ackXXYY(ack, NULL, NULL); } -static void ptc06_reset() { +void ptc06_reset() { + fully_flush_uart(); send_comm(comm_reset); recv_ack(resp_reset); + fully_flush_uart(); +} + +static void ptc06_set_resolution() { + char res[] = { 0x22 }; + send_commVARS(comm_set_res, res); + recv_ack(resp_set_res); + fully_flush_uart(); +} + +static void ptc06_set_compressibility() { + char compr[] = { 0xFF }; + send_commVARS(comm_set_compress, compr); + recv_ack(resp_set_compress); + fully_flush_uart(); +} + +static void ptc06_capture() { + send_comm(comm_capture); + recv_ack(resp_capture); + fully_flush_uart(); + ESP_LOGI(TAG, "Captured"); +} + +static void ptc06_read_image_length(char* XX, char* YY) { + send_comm(comm_data_len); + recv_ackXXYY(resp_data_len, XX, YY); + fully_flush_uart(); + ESP_LOGI(TAG, "Read length"); +} + +#define min(a, b) a < b ? a : b + +static char* ptc06_read_image_data(const char XX, const char YY) { + size_t length = (XX << 8) | YY; + char* image = (char*)malloc(length * sizeof(char)); + printf("%d\n", length); + + char vars[] = { 0x00, 0x00, XX, YY }; + send_commVARS(comm_read_data, vars); + + recv_ack(resp_read_data); + /* vTaskDelay(123 / portTICK_PERIOD_MS); */ + + int current = 0; + size_t available = 1; + while (current < length && available > 0) { + ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_NUM, &available)); + current += uart_read_bytes(UART_NUM, image + current, available, 100); + vTaskDelay(1 / portTICK_PERIOD_MS); + } + for (int i = 0; i < 10; i++) + printf("%d\n", image[i]); + printf("\n"); + + recv_ack(resp_read_data); + fully_flush_uart(); + + ESP_LOGI(TAG, "Read image"); + return image; +} + +static void ptc06_stop_capture() { + send_comm(comm_stop_cap); + recv_ack(resp_stop_cap); + fully_flush_uart(); + ESP_LOGI(TAG, "Stopped capture"); } void ptc06_init() { init_uart(); init_camera(); ptc06_reset(); + + /* ptc06_set_resolution(); */ + /* ptc06_set_compressibility(); */ + ESP_LOGI(TAG, "Initialized ptc06 successfully. UART tx: %d, rx: %d, rts: %d, cts: %d", UART_TX, UART_RX, UART_RTS, UART_CTS); } + +char* ptc06_shoot_photo() { + ptc06_capture(); + char XX, YY; + ptc06_read_image_length(&XX, &YY); + char* img = ptc06_read_image_data(XX, YY); + ptc06_stop_capture(); + return img; +} diff --git a/Year 3/Internet of things/goam/main/ptc06.h b/Year 3/Internet of things/goam/main/ptc06.h index 371775a..631ba7a 100644 --- a/Year 3/Internet of things/goam/main/ptc06.h +++ b/Year 3/Internet of things/goam/main/ptc06.h @@ -1,3 +1,5 @@ #pragma once void ptc06_init(); +void ptc06_reset(); +char* ptc06_shoot_photo(); |
