]> git.earman.xyz Git - sensor-watch.git/commitdiff
add ability to read from USB serial
authorJoey Castillo <joeycastillo@utexas.edu>
Mon, 9 May 2022 00:19:20 +0000 (20:19 -0400)
committerJoey Castillo <joeycastillo@utexas.edu>
Mon, 9 May 2022 00:19:20 +0000 (20:19 -0400)
watch-library/hardware/watch/watch_private.c
watch-library/shared/watch/watch.h
watch-library/shared/watch/watch_private.h

index e4a03926a2f1019cdbadaaf9dcc57e337e639049..a0525d3c005d7340ed58c1d0b98fd31f4141ff16 100644 (file)
@@ -255,8 +255,15 @@ int _write(int file, char *ptr, int len) {
     return 0;
 }
 
-// this method could be overridden to read stuff from the USB console? but no need rn.
-int _read(void) {
+char buf[256] = {0};
+
+int _read(int file, char *ptr, int len) {
+    (void)file;
+    int actual_length = strlen(buf);
+    if (actual_length) {
+        memcpy(ptr, buf, min(len, actual_length));
+        return actual_length;
+    }
     return 0;
 }
 
@@ -264,8 +271,18 @@ void USB_Handler(void) {
     tud_int_handler(0);
 }
 
+static void cdc_task(void) {
+    if (tud_cdc_n_available(0)) {
+        tud_cdc_n_read(0, buf, sizeof(buf));
+    } else {
+        memset(buf, 0, 64);
+        // buf[0] = 0;
+    }
+}
+
 void TC0_Handler(void) {
     tud_task();
+    cdc_task();
     TC0->COUNT8.INTFLAG.reg |= TC_INTFLAG_OVF;
 }
 
index 1dd8e7f74d9bd2bbc70c60a09804dc9b3713f149..b307feca9ac7f0574d2040d611f68fd2bb79c887 100644 (file)
   */
 bool watch_is_buzzer_or_led_enabled(void);
 
+/** @brief Reads up to len bytes from the USB serial.
+  * @param file ignored, you can pass in 0
+  * @param ptr pointer to a buffer of at least len bytes
+  * @param len the number of bytes you wish to read, max 256.
+  * @return The number of bytes read, or zero if no bytes were read.
+  */
+int read(int file, char *ptr, int len);
+
 #endif /* WATCH_H_ */
\ No newline at end of file
index 7bb91d1f3bbdd227ddeb083eabe1915a855732fa..9d55bc212372f706da5866c4592616b155a46189 100644 (file)
@@ -44,7 +44,8 @@ void _watch_enable_usb(void);
 // this function ends up getting called by printf to log stuff to the USB console.
 int _write(int file, char *ptr, int len);
 
-// this method could be overridden to read stuff from the USB console? but no need rn.
-int _read(void);
+// i thought this would be called by gets but it doesn't? anyway it does get called by read()
+// so that's our mechanism for reading data from the USB serial console.
+int _read(int file, char *ptr, int len);
 
 #endif