From: Joey Castillo Date: Sat, 17 May 2025 21:55:02 +0000 (-0400) Subject: irda_demo -> irda_upload X-Git-Url: https://git.earman.xyz/?a=commitdiff_plain;h=cf26f28cd0a15f2462b36699ba83d97600f1962c;p=sensor-watch.git irda_demo -> irda_upload --- diff --git a/movement_faces.h b/movement_faces.h index c9ebe2e..a43fbfb 100644 --- a/movement_faces.h +++ b/movement_faces.h @@ -46,7 +46,7 @@ #include "preferences_face.h" #include "settings_face.h" #include "light_sensor_face.h" -#include "irda_demo_face.h" +#include "irda_upload_face.h" #include "file_demo_face.h" #include "chirpy_demo_face.h" // New includes go above this line. diff --git a/watch-faces.mk b/watch-faces.mk index eadd5b5..b90b33f 100644 --- a/watch-faces.mk +++ b/watch-faces.mk @@ -21,7 +21,7 @@ SRCS += \ ./watch-faces/settings/preferences_face.c \ ./watch-faces/settings/settings_face.c \ ./watch-faces/demo/light_sensor_face.c \ - ./watch-faces/demo/irda_demo_face.c \ ./watch-faces/demo/file_demo_face.c \ ./watch-faces/io/chirpy_demo_face.c \ + ./watch-faces/io/irda_upload_face.c \ # New watch faces go above this line. diff --git a/watch-faces/demo/file_demo_face.h b/watch-faces/demo/file_demo_face.h index 75995d6..1f06aaa 100644 --- a/watch-faces/demo/file_demo_face.h +++ b/watch-faces/demo/file_demo_face.h @@ -30,7 +30,7 @@ * FILE DISPLAY DEMO * * Displays the contents of a file on the watch. - * For use with the irda_demo_face that allows uploading files to the watch. + * For use with the irda_upload_face that allows uploading files to the watch. * */ diff --git a/watch-faces/demo/irda_demo_face.c b/watch-faces/demo/irda_demo_face.c deleted file mode 100644 index d6a0ac5..0000000 --- a/watch-faces/demo/irda_demo_face.c +++ /dev/null @@ -1,146 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2024 Joey Castillo - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include -#include -#include "irda_demo_face.h" -#include "tc.h" -#include "eic.h" -#include "usb.h" -#include "uart.h" -#include "filesystem.h" - -#ifdef HAS_IR_SENSOR - -void irda_demo_face_setup(uint8_t watch_face_index, void ** context_ptr) { - (void) watch_face_index; - if (*context_ptr == NULL) { - *context_ptr = malloc(sizeof(irda_demo_state_t)); - memset(*context_ptr, 0, sizeof(irda_demo_state_t)); - // Do any one-time tasks in here; the inside of this conditional happens only at boot. - } -} - -void irda_demo_face_activate(void *context) { - irda_demo_state_t *state = (irda_demo_state_t *)context; - (void) state; - HAL_GPIO_IR_ENABLE_out(); - HAL_GPIO_IR_ENABLE_clr(); - HAL_GPIO_IRSENSE_in(); - HAL_GPIO_IRSENSE_pmuxen(HAL_GPIO_PMUX_SERCOM_ALT); - uart_init_instance(0, UART_TXPO_NONE, UART_RXPO_0, 900); - uart_set_irda_mode_instance(0, true); - uart_enable_instance(0); -} - -bool irda_demo_face_loop(movement_event_t event, void *context) { - irda_demo_state_t *state = (irda_demo_state_t *)context; - (void) state; - - switch (event.event_type) { - case EVENT_NONE: - case EVENT_ACTIVATE: - case EVENT_TICK: - { - char data[64]; - size_t bytes_read = uart_read_instance(0, data, 64); - watch_clear_display(); - watch_display_text_with_fallback(WATCH_POSITION_TOP, "IrDA", "IR"); - - if (bytes_read) { - // data is in the following format, where S is Size, F is Filename and C is checksum: - // SSFFFFFFFFFFFFCC - // after that, we get SS bytes of data, followed by a two-byte checksum - uint16_t expected_size = ((uint16_t *)data)[0]; - uint16_t expected_checksum = ((uint16_t *)data)[7]; - - uint16_t checksum = 0; - - for (size_t i = 0; i < 14; i++) checksum += data[i]; - - if (checksum != expected_checksum) { - // failed - watch_display_text_with_fallback(WATCH_POSITION_TOP, "BAD ", "BA"); - watch_display_text(WATCH_POSITION_BOTTOM, "HEADER"); - break; - } - // valid header! To make it easier on ourselves, we're going to make byte 14 zero... - data[14] = 0; - // so that now buf[2] points to our null-terminated filename. - char *filename = data + 2; - - // now let's check the data. - checksum = 0; - memcpy(&expected_checksum, data + 16 + expected_size, 2); - for (uint16_t i = 16; i < 16 + expected_size; i++) checksum += data[i]; - - if (checksum != expected_checksum) { - // failed - watch_display_text_with_fallback(WATCH_POSITION_TOP, "BAD ", "BA"); - watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "DATA ", " Data "); - break; - } - - // Valid data! Write it to the file system. - filesystem_write_file(filename, data + 16, expected_size); - watch_display_text_with_fallback(WATCH_POSITION_TOP, "RECVD", "RC"); - - char buf[8]; - sprintf(buf, "%4db ", expected_size); - watch_display_text(WATCH_POSITION_BOTTOM, buf); - } else { - watch_display_text(WATCH_POSITION_FULL, " no dat"); - } - } - break; - case EVENT_LIGHT_BUTTON_UP: - break; - case EVENT_ALARM_BUTTON_UP: - break; - case EVENT_TIMEOUT: - // movement_move_to_face(0); - break; - case EVENT_LOW_ENERGY_UPDATE: - watch_display_text(WATCH_POSITION_TOP_RIGHT, " <"); - break; - default: - return movement_default_loop_handler(event); - } - - return false; -} - -void irda_demo_face_resign(void *context) { - (void) context; - uart_disable_instance(0); - HAL_GPIO_IRSENSE_pmuxdis(); - HAL_GPIO_IRSENSE_off(); - HAL_GPIO_IR_ENABLE_off(); -} - -void irq_handler_sercom0(void); -void irq_handler_sercom0(void) { - uart_irq_handler(0); -} -#endif // HAS_IR_SENSOR diff --git a/watch-faces/demo/irda_demo_face.h b/watch-faces/demo/irda_demo_face.h deleted file mode 100644 index 5c345b8..0000000 --- a/watch-faces/demo/irda_demo_face.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2024 Joey Castillo - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#pragma once - -#include "movement.h" - -#ifdef HAS_IR_SENSOR - -/* - * LIGHT SENSOR PLAYGROUND - * - * Temporary watch face for playing with the light sensor. - * - */ - -typedef struct { - // Anything you need to keep track of, put it here! - uint8_t unused; -} irda_demo_state_t; - -void irda_demo_face_setup(uint8_t watch_face_index, void ** context_ptr); -void irda_demo_face_activate(void *context); -bool irda_demo_face_loop(movement_event_t event, void *context); -void irda_demo_face_resign(void *context); - -#define irda_demo_face ((const watch_face_t){ \ - irda_demo_face_setup, \ - irda_demo_face_activate, \ - irda_demo_face_loop, \ - irda_demo_face_resign, \ - NULL, \ -}) - -#endif // HAS_IR_SENSOR diff --git a/watch-faces/io/irda_upload_face.c b/watch-faces/io/irda_upload_face.c new file mode 100644 index 0000000..89fc328 --- /dev/null +++ b/watch-faces/io/irda_upload_face.c @@ -0,0 +1,146 @@ +/* + * MIT License + * + * Copyright (c) 2024 Joey Castillo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include "irda_upload_face.h" +#include "tc.h" +#include "eic.h" +#include "usb.h" +#include "uart.h" +#include "filesystem.h" + +#ifdef HAS_IR_SENSOR + +void irda_upload_face_setup(uint8_t watch_face_index, void ** context_ptr) { + (void) watch_face_index; + if (*context_ptr == NULL) { + *context_ptr = malloc(sizeof(irda_demo_state_t)); + memset(*context_ptr, 0, sizeof(irda_demo_state_t)); + // Do any one-time tasks in here; the inside of this conditional happens only at boot. + } +} + +void irda_upload_face_activate(void *context) { + irda_demo_state_t *state = (irda_demo_state_t *)context; + (void) state; + HAL_GPIO_IR_ENABLE_out(); + HAL_GPIO_IR_ENABLE_clr(); + HAL_GPIO_IRSENSE_in(); + HAL_GPIO_IRSENSE_pmuxen(HAL_GPIO_PMUX_SERCOM_ALT); + uart_init_instance(0, UART_TXPO_NONE, UART_RXPO_0, 900); + uart_set_irda_mode_instance(0, true); + uart_enable_instance(0); +} + +bool irda_upload_face_loop(movement_event_t event, void *context) { + irda_demo_state_t *state = (irda_demo_state_t *)context; + (void) state; + + switch (event.event_type) { + case EVENT_NONE: + case EVENT_ACTIVATE: + case EVENT_TICK: + { + char data[64]; + size_t bytes_read = uart_read_instance(0, data, 64); + watch_clear_display(); + watch_display_text_with_fallback(WATCH_POSITION_TOP, "IrDA", "IR"); + + if (bytes_read) { + // data is in the following format, where S is Size, F is Filename and C is checksum: + // SSFFFFFFFFFFFFCC + // after that, we get SS bytes of data, followed by a two-byte checksum + uint16_t expected_size = ((uint16_t *)data)[0]; + uint16_t expected_checksum = ((uint16_t *)data)[7]; + + uint16_t checksum = 0; + + for (size_t i = 0; i < 14; i++) checksum += data[i]; + + if (checksum != expected_checksum) { + // failed + watch_display_text_with_fallback(WATCH_POSITION_TOP, "BAD ", "BA"); + watch_display_text(WATCH_POSITION_BOTTOM, "HEADER"); + break; + } + // valid header! To make it easier on ourselves, we're going to make byte 14 zero... + data[14] = 0; + // so that now buf[2] points to our null-terminated filename. + char *filename = data + 2; + + // now let's check the data. + checksum = 0; + memcpy(&expected_checksum, data + 16 + expected_size, 2); + for (uint16_t i = 16; i < 16 + expected_size; i++) checksum += data[i]; + + if (checksum != expected_checksum) { + // failed + watch_display_text_with_fallback(WATCH_POSITION_TOP, "BAD ", "BA"); + watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "DATA ", " Data "); + break; + } + + // Valid data! Write it to the file system. + filesystem_write_file(filename, data + 16, expected_size); + watch_display_text_with_fallback(WATCH_POSITION_TOP, "RECVD", "RC"); + + char buf[8]; + sprintf(buf, "%4db ", expected_size); + watch_display_text(WATCH_POSITION_BOTTOM, buf); + } else { + watch_display_text(WATCH_POSITION_FULL, " no dat"); + } + } + break; + case EVENT_LIGHT_BUTTON_UP: + break; + case EVENT_ALARM_BUTTON_UP: + break; + case EVENT_TIMEOUT: + // movement_move_to_face(0); + break; + case EVENT_LOW_ENERGY_UPDATE: + watch_display_text(WATCH_POSITION_TOP_RIGHT, " <"); + break; + default: + return movement_default_loop_handler(event); + } + + return false; +} + +void irda_upload_face_resign(void *context) { + (void) context; + uart_disable_instance(0); + HAL_GPIO_IRSENSE_pmuxdis(); + HAL_GPIO_IRSENSE_off(); + HAL_GPIO_IR_ENABLE_off(); +} + +void irq_handler_sercom0(void); +void irq_handler_sercom0(void) { + uart_irq_handler(0); +} +#endif // HAS_IR_SENSOR diff --git a/watch-faces/io/irda_upload_face.h b/watch-faces/io/irda_upload_face.h new file mode 100644 index 0000000..023ef42 --- /dev/null +++ b/watch-faces/io/irda_upload_face.h @@ -0,0 +1,56 @@ +/* + * MIT License + * + * Copyright (c) 2024 Joey Castillo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#pragma once + +#include "movement.h" + +#ifdef HAS_IR_SENSOR + +/* + * LIGHT SENSOR PLAYGROUND + * + * Temporary watch face for playing with the light sensor. + * + */ + +typedef struct { + // Anything you need to keep track of, put it here! + uint8_t unused; +} irda_demo_state_t; + +void irda_upload_face_setup(uint8_t watch_face_index, void ** context_ptr); +void irda_upload_face_activate(void *context); +bool irda_upload_face_loop(movement_event_t event, void *context); +void irda_upload_face_resign(void *context); + +#define irda_upload_face ((const watch_face_t){ \ + irda_upload_face_setup, \ + irda_upload_face_activate, \ + irda_upload_face_loop, \ + irda_upload_face_resign, \ + NULL, \ +}) + +#endif // HAS_IR_SENSOR