From 5addcbc556b26b9cb48fc14a810a99468ad8f21c Mon Sep 17 00:00:00 2001 From: tock Date: Thu, 17 Sep 2020 05:22:43 +0800 Subject: [PATCH 1/8] Adds all relevant qdec files --- examples/tests/qdec_callback/Makefile | 11 +++++++ examples/tests/qdec_callback/main.c | 46 +++++++++++++++++++++++++++ examples/tests/qdec_timer/Makefile | 11 +++++++ examples/tests/qdec_timer/main.c | 46 +++++++++++++++++++++++++++ libtock/qdec.c | 21 ++++++++++++ libtock/qdec.h | 19 +++++++++++ 6 files changed, 154 insertions(+) create mode 100644 examples/tests/qdec_callback/Makefile create mode 100644 examples/tests/qdec_callback/main.c create mode 100644 examples/tests/qdec_timer/Makefile create mode 100644 examples/tests/qdec_timer/main.c create mode 100644 libtock/qdec.c create mode 100644 libtock/qdec.h diff --git a/examples/tests/qdec_callback/Makefile b/examples/tests/qdec_callback/Makefile new file mode 100644 index 000000000..54d6a7969 --- /dev/null +++ b/examples/tests/qdec_callback/Makefile @@ -0,0 +1,11 @@ +# Makefile for user application + +# Specify this directory relative to the current application. +TOCK_USERLAND_BASE_DIR = ../../.. + +# Which files to compile. +C_SRCS := $(wildcard *.c) + +# Include userland master makefile. Contains rules and flags for actually +# building the application. +include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk diff --git a/examples/tests/qdec_callback/main.c b/examples/tests/qdec_callback/main.c new file mode 100644 index 000000000..1225565f8 --- /dev/null +++ b/examples/tests/qdec_callback/main.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +static void qdec_callback(int qdec_num, + __attribute__ ((unused)) int val, + __attribute__ ((unused)) int arg2, + __attribute__ ((unused)) void *ud) { + printf("QDEC Absolute Position: %d\n", qdec_num); + + // Set LEDs based on acc value + // Note: LED rules arbitrarily chosen by developer + if (qdec_num > 0) { + led_on(0); + led_off(1); + led_off(2); + } else if (qdec_num == 0) { + led_on(1); + led_off(0); + led_off(2); + } else if (qdec_num < 0) { + led_on(2); + led_off(0); + led_off(1); + } +} + +int main(void) { + printf("Starting The [QDEC] Test App for Callbacks\n"); + if (!qdec_exists()) { + printf("No QDEC driver\n"); + return -1; + } + qdec_enable(); + if (qdec_subscribe(qdec_callback, NULL) != 0) { + printf("Can't subscribe to interrupt results!"); + return -1; + } + if (!qdec_interrupt_enable()) { + printf("Can't enable interrupts!\n"); + return -1; + } + printf("QDEC has been fully initiated!\n"); + return 0; +} diff --git a/examples/tests/qdec_timer/Makefile b/examples/tests/qdec_timer/Makefile new file mode 100644 index 000000000..54d6a7969 --- /dev/null +++ b/examples/tests/qdec_timer/Makefile @@ -0,0 +1,11 @@ +# Makefile for user application + +# Specify this directory relative to the current application. +TOCK_USERLAND_BASE_DIR = ../../.. + +# Which files to compile. +C_SRCS := $(wildcard *.c) + +# Include userland master makefile. Contains rules and flags for actually +# building the application. +include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk diff --git a/examples/tests/qdec_timer/main.c b/examples/tests/qdec_timer/main.c new file mode 100644 index 000000000..d21984cb4 --- /dev/null +++ b/examples/tests/qdec_timer/main.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +static void timer_fired ( __attribute__ ((unused)) int arg0, + __attribute__ ((unused)) int arg1, + __attribute__ ((unused)) int arg2, + __attribute__ ((unused)) void *ud) { + int qdec_num = qdec_displacement(); + printf("Displacement val: %d\n", qdec_num); + + // Set LEDs based on acc value + // Note: LED rules arbitrarily chosen by developer + if (qdec_num%2 == 0 && qdec_num > 0) { + led_on(0); + led_off(1); + led_off(2); + } else if (qdec_num%2 != 0) { + led_on(1); + led_off(0); + led_off(2); + } else if (qdec_num%2 == 0 && qdec_num < 0) { + led_on(2); + led_off(0); + led_off(1); + } +} + +int main(void) { + printf("Starting The [QDEC] Test App for Timers\n"); + if (!qdec_exists()) { + printf("No QDEC driver\n"); + return -1; + } + qdec_enable(); + if (!qdec_interrupt_enable()) { + printf("Can't enable interrupts!\n"); + return -1; + } + printf("QDEC has been fully initiated!\n"); + static tock_timer_t timer; + timer_every(500, timer_fired, NULL, &timer); + printf("Timer set!\n"); + return 0; +} diff --git a/libtock/qdec.c b/libtock/qdec.c new file mode 100644 index 000000000..ca15a48a3 --- /dev/null +++ b/libtock/qdec.c @@ -0,0 +1,21 @@ +#include "qdec.h" + +bool qdec_exists(void) { + return command(DRIVER_NUM_QDEC, 1, 0, 0) >= 0; +} + +bool qdec_enable(void) { + return command(DRIVER_NUM_QDEC, 2, 0, 0) >= 0; +} + +bool qdec_interrupt_enable(void) { + return command(DRIVER_NUM_QDEC, 3, 0, 0) >= 0; +} + +int qdec_subscribe(subscribe_cb callback, void* callback_args) { + return subscribe(DRIVER_NUM_QDEC, 0, callback, callback_args); +} + +int qdec_displacement(void) { + return command(DRIVER_NUM_QDEC, 4, 0, 0); +} diff --git a/libtock/qdec.h b/libtock/qdec.h new file mode 100644 index 000000000..a07d05cc1 --- /dev/null +++ b/libtock/qdec.h @@ -0,0 +1,19 @@ +#pragma once + +#include "tock.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define DRIVER_NUM_QDEC 0x90003 + +bool qdec_exists(void); +bool qdec_enable(void); +bool qdec_interrupt_enable(void); +int qdec_subscribe(subscribe_cb callback, void* callback_args); +int qdec_displacement(void); + +#ifdef __cplusplus +} +#endif From 5488c0d10dc27938e86134e806a4a1b401d97f02 Mon Sep 17 00:00:00 2001 From: tock Date: Thu, 17 Sep 2020 09:52:29 +0800 Subject: [PATCH 2/8] Formats files --- examples/tests/qdec_callback/main.c | 2 +- examples/tests/qdec_timer/main.c | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/tests/qdec_callback/main.c b/examples/tests/qdec_callback/main.c index 1225565f8..edcd2f8f5 100644 --- a/examples/tests/qdec_callback/main.c +++ b/examples/tests/qdec_callback/main.c @@ -1,7 +1,7 @@ #include #include -#include #include +#include static void qdec_callback(int qdec_num, __attribute__ ((unused)) int val, diff --git a/examples/tests/qdec_timer/main.c b/examples/tests/qdec_timer/main.c index d21984cb4..e1c09be5a 100644 --- a/examples/tests/qdec_timer/main.c +++ b/examples/tests/qdec_timer/main.c @@ -1,7 +1,7 @@ #include #include -#include #include +#include static void timer_fired ( __attribute__ ((unused)) int arg0, __attribute__ ((unused)) int arg1, @@ -12,19 +12,19 @@ static void timer_fired ( __attribute__ ((unused)) int arg0, // Set LEDs based on acc value // Note: LED rules arbitrarily chosen by developer - if (qdec_num%2 == 0 && qdec_num > 0) { + if (qdec_num % 2 == 0 && qdec_num > 0) { led_on(0); led_off(1); led_off(2); - } else if (qdec_num%2 != 0) { + } else if (qdec_num % 2 != 0) { led_on(1); led_off(0); led_off(2); - } else if (qdec_num%2 == 0 && qdec_num < 0) { + } else if (qdec_num % 2 == 0 && qdec_num < 0) { led_on(2); led_off(0); led_off(1); - } + } } int main(void) { From 0d90890d4e719c95c62ad0bf1fa48e1415591330 Mon Sep 17 00:00:00 2001 From: tock Date: Sat, 19 Sep 2020 11:47:35 +0800 Subject: [PATCH 3/8] Updates available qdec functions and their comments as well as led code in the main of qdec_callback --- examples/tests/qdec_callback/main.c | 23 ++++++++++------------- libtock/qdec.c | 6 +++++- libtock/qdec.h | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/examples/tests/qdec_callback/main.c b/examples/tests/qdec_callback/main.c index edcd2f8f5..e16f3840b 100644 --- a/examples/tests/qdec_callback/main.c +++ b/examples/tests/qdec_callback/main.c @@ -9,20 +9,17 @@ static void qdec_callback(int qdec_num, __attribute__ ((unused)) void *ud) { printf("QDEC Absolute Position: %d\n", qdec_num); - // Set LEDs based on acc value + // Alter LED blink rate based on acc value // Note: LED rules arbitrarily chosen by developer - if (qdec_num > 0) { - led_on(0); - led_off(1); - led_off(2); - } else if (qdec_num == 0) { - led_on(1); - led_off(0); - led_off(2); - } else if (qdec_num < 0) { - led_on(2); - led_off(0); - led_off(1); + int num_leds = led_count(); + if (num_leds > 0) { + if (qdec_num > 0) { + led_off(1); + } else if (qdec_num == 0) { + led_on(1); + } else if (qdec_num < 0) { + led_off(1); + } } } diff --git a/libtock/qdec.c b/libtock/qdec.c index ca15a48a3..9ea5f765e 100644 --- a/libtock/qdec.c +++ b/libtock/qdec.c @@ -1,10 +1,14 @@ #include "qdec.h" bool qdec_exists(void) { - return command(DRIVER_NUM_QDEC, 1, 0, 0) >= 0; + return command(DRIVER_NUM_QDEC, 0, 0, 0) >= 0; } bool qdec_enable(void) { + return command(DRIVER_NUM_QDEC, 1, 0, 0) >= 0; +} + +bool qdec_disable(void) { return command(DRIVER_NUM_QDEC, 2, 0, 0) >= 0; } diff --git a/libtock/qdec.h b/libtock/qdec.h index a07d05cc1..73ce58c81 100644 --- a/libtock/qdec.h +++ b/libtock/qdec.h @@ -1,3 +1,14 @@ +/** + * Quadrature Decoder (QDEC) + * + * A QDEC provides buffered decoding of quadrature-encoded signals. It is suitable + * for mechanical and optical sensors. + * + * Use for the QDEC would be hooking up a rotary encoder or other optical encoder to + * the respective ports and using the below series of function to decode the signals + * sent by the encoders and turn them into useful metrics (i.e. displacement) + * + */ #pragma once #include "tock.h" @@ -8,10 +19,17 @@ extern "C" { #define DRIVER_NUM_QDEC 0x90003 +/*Verifies the existence of the QDEC*/ bool qdec_exists(void); +/*Enables the QDEC*/ bool qdec_enable(void); +/*Disables the QDEC*/ +bool qdec_disable(void); +/*Enables QDEC interrupts needed to get displacement values*/ bool qdec_interrupt_enable(void); +/*Subscribes the app to QDEC interrupt calls*/ int qdec_subscribe(subscribe_cb callback, void* callback_args); +/*Gets the displacement of the QDEC from its last stopped position*/ int qdec_displacement(void); #ifdef __cplusplus From dba1e6e75cae7633a4a8cc991ca3290f152fe5d7 Mon Sep 17 00:00:00 2001 From: tock Date: Sat, 19 Sep 2020 11:51:53 +0800 Subject: [PATCH 4/8] Removes all led code --- examples/tests/qdec_callback/main.c | 13 ------------- examples/tests/qdec_timer/main.c | 16 ---------------- 2 files changed, 29 deletions(-) diff --git a/examples/tests/qdec_callback/main.c b/examples/tests/qdec_callback/main.c index e16f3840b..9ff890459 100644 --- a/examples/tests/qdec_callback/main.c +++ b/examples/tests/qdec_callback/main.c @@ -8,19 +8,6 @@ static void qdec_callback(int qdec_num, __attribute__ ((unused)) int arg2, __attribute__ ((unused)) void *ud) { printf("QDEC Absolute Position: %d\n", qdec_num); - - // Alter LED blink rate based on acc value - // Note: LED rules arbitrarily chosen by developer - int num_leds = led_count(); - if (num_leds > 0) { - if (qdec_num > 0) { - led_off(1); - } else if (qdec_num == 0) { - led_on(1); - } else if (qdec_num < 0) { - led_off(1); - } - } } int main(void) { diff --git a/examples/tests/qdec_timer/main.c b/examples/tests/qdec_timer/main.c index e1c09be5a..59006cd7d 100644 --- a/examples/tests/qdec_timer/main.c +++ b/examples/tests/qdec_timer/main.c @@ -9,22 +9,6 @@ static void timer_fired ( __attribute__ ((unused)) int arg0, __attribute__ ((unused)) void *ud) { int qdec_num = qdec_displacement(); printf("Displacement val: %d\n", qdec_num); - - // Set LEDs based on acc value - // Note: LED rules arbitrarily chosen by developer - if (qdec_num % 2 == 0 && qdec_num > 0) { - led_on(0); - led_off(1); - led_off(2); - } else if (qdec_num % 2 != 0) { - led_on(1); - led_off(0); - led_off(2); - } else if (qdec_num % 2 == 0 && qdec_num < 0) { - led_on(2); - led_off(0); - led_off(1); - } } int main(void) { From adc18582f8f78be190fbbb7823fb7781edd808ac Mon Sep 17 00:00:00 2001 From: tock Date: Sat, 19 Sep 2020 12:20:48 +0800 Subject: [PATCH 5/8] Adds shell README files --- examples/tests/qdec_callback/README.md | 1 + examples/tests/qdec_timer/README.md | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 examples/tests/qdec_callback/README.md create mode 100644 examples/tests/qdec_timer/README.md diff --git a/examples/tests/qdec_callback/README.md b/examples/tests/qdec_callback/README.md new file mode 100644 index 000000000..2f376469d --- /dev/null +++ b/examples/tests/qdec_callback/README.md @@ -0,0 +1 @@ +QDEC Callback Test diff --git a/examples/tests/qdec_timer/README.md b/examples/tests/qdec_timer/README.md new file mode 100644 index 000000000..426902c2e --- /dev/null +++ b/examples/tests/qdec_timer/README.md @@ -0,0 +1,7 @@ +QDEC Timer Test + +This test's purposed is to ensure the interrupts work +as expected and the approximately correct displacement +is being returned with each fire of the timer. + +To verify the test is working properly From 4384599e5c03de45aa6e491e314c448078e2e43c Mon Sep 17 00:00:00 2001 From: mmurray22 <36782608+mmurray22@users.noreply.github.com> Date: Sun, 20 Sep 2020 13:57:45 -0700 Subject: [PATCH 6/8] Updates README --- examples/tests/qdec_timer/README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/examples/tests/qdec_timer/README.md b/examples/tests/qdec_timer/README.md index 426902c2e..17b788863 100644 --- a/examples/tests/qdec_timer/README.md +++ b/examples/tests/qdec_timer/README.md @@ -4,4 +4,13 @@ This test's purposed is to ensure the interrupts work as expected and the approximately correct displacement is being returned with each fire of the timer. -To verify the test is working properly +To verify the test is working properly, fire up the QDEC +timer program, and without touching the encoder, you should +see the statement 'Displacement val: 0' print on the screen +every 500 seconds. Turning the QDEC in the positive direction +one click will change the print statement to 'Displacement +val: 4' for one timer fired instance and then return back to +'Displacement val: 0'. Similarly, turning it in the negative +direction one click will change the print statement to +'Displacement val: -4' for one timer fired instance and then +return back to 'Displacement val: 0'.. From 3a34b952bde0deea219d570cea947a42e67df6cc Mon Sep 17 00:00:00 2001 From: mmurray22 <36782608+mmurray22@users.noreply.github.com> Date: Sun, 20 Sep 2020 14:04:50 -0700 Subject: [PATCH 7/8] Removes further period --- examples/tests/qdec_timer/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tests/qdec_timer/README.md b/examples/tests/qdec_timer/README.md index 17b788863..04780af0f 100644 --- a/examples/tests/qdec_timer/README.md +++ b/examples/tests/qdec_timer/README.md @@ -13,4 +13,4 @@ val: 4' for one timer fired instance and then return back to 'Displacement val: 0'. Similarly, turning it in the negative direction one click will change the print statement to 'Displacement val: -4' for one timer fired instance and then -return back to 'Displacement val: 0'.. +return back to 'Displacement val: 0'. From 9c5f2e9b39f39ef0c307e5c88f50a30d60cf8b89 Mon Sep 17 00:00:00 2001 From: mmurray22 <36782608+mmurray22@users.noreply.github.com> Date: Sun, 20 Sep 2020 14:13:21 -0700 Subject: [PATCH 8/8] Updates README for qdec callback program --- examples/tests/qdec_callback/README.md | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/examples/tests/qdec_callback/README.md b/examples/tests/qdec_callback/README.md index 2f376469d..64684509a 100644 --- a/examples/tests/qdec_callback/README.md +++ b/examples/tests/qdec_callback/README.md @@ -1 +1,35 @@ QDEC Callback Test + +This test's purposed is to ensure the interrupts work +as expected and the callbacks work as expected every +time the QDEC is rotated. + +To verify the test is working properly, fire up the QDEC +timer program, and without touching the encoder, you should +see the following initial statements (If anything else is printed, +that is a signal that there was an error when setting up the QDEC): + +'Starting The [QDEC] Test App for Callbacks +QDEC has been fully initiated!' + +Then, turning the QDEC in the positive direction +one click will invoke the callback method and a new statment will +be printed: + +QDEC Absolute Position: 1 +QDEC Absolute Position: 2 +QDEC Absolute Position: 3 +QDEC Absolute Position: 4 + +Similarly, turning the QDEC in the negative direction (from the initial +starting location) one click will invoke the callback method and a new +statment will be printed: + +QDEC Absolute Position: -1 +QDEC Absolute Position: -2 +QDEC Absolute Position: -3 +QDEC Absolute Position: -4 + +These are good initial tests to ensure the QDEC is working as expected. +Continue the QDEC in both directions and see if the progression of values +displayed on the screen are logical.