Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:156098cfb039, committed 2020-06-25
- Comitter:
- timo_k2
- Date:
- Thu Jun 25 15:51:09 2020 +0000
- Commit message:
- Flow control with three threats. Blinking LEDs in the main and a thread for the led. Reading analog input with 12 bits and calculating moving average in a thread for the potentiometer.
Changed in this revision
diff -r 000000000000 -r 156098cfb039 README.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Thu Jun 25 15:51:09 2020 +0000 @@ -0,0 +1,7 @@ +# Application Flow Control: Thread example +copied/forked from Mbed OS snippets "mbed-os-snippet-Flow-Control-Thread " 25.6.2020 + +A threaded Blinky implementation. +MIRRORED FROM MASTER EXAMPLE SNIPPETS REPOSITORY: mbed-os-examples-docs_only. + +Added a thread for potentiometer reading and value filtering. Works fine now with three threads.
diff -r 000000000000 -r 156098cfb039 main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Jun 25 15:51:09 2020 +0000
@@ -0,0 +1,80 @@
+/* Flow control with three threats
+* Blinking LEDs in the main and a thread for the led.
+* Reading analog input with 12 bits and calculating moving average in a thread for the potentiometer
+* connect:
+* D10 - 330 Ohm - LED - GND
+* D11 - 330 Ohm - LED - GND
+* GND - Pot 1
+* A3 - Pot 2
+* Vcc - Pot 3
+*
+* Tested with Nucleo-L432KC but should work with almost any board
+*
+* Thanks to "mbed-os-Flow-Control_Thread" example and AnalogIn API Reference
+* Timo Karppinen 25.6.2020, Apache 2.0
+*/
+
+#include "mbed.h"
+
+DigitalOut led1(D10);
+DigitalOut led2(D11);
+AnalogIn pot1(A3);
+#define numSamples 1024
+
+Thread threadL;
+Thread threadP;
+
+// Thread for the LED2. Sleeps for 1 second.
+void led2_thread()
+{
+ while (true) {
+ led2 = !led2;
+ printf("Led2 blink %d \n", led2.read());
+
+ ThisThread::sleep_for(1000);
+ }
+}
+
+
+//Thread for the Pot. Sleeps for one millisecond in the middle of processing.
+void pot1_thread()
+{
+ while(true) {
+ uint16_t pot1Samples[numSamples]; //unsigned short in the range [0x0, 0xFFFF], [0, 65535]
+ int sum;
+ int result;
+
+ for (int i = 0; i < numSamples; i++) {
+ pot1Samples[i] = pot1.read_u16()/16; // normalized into range [0x0, 0xFFFF] and to 12 bit [0x0, 0xFFF]
+ //printf("Pot1 sample %d \n", pot1Samples[i]); // prints every sample
+ ThisThread::sleep_for(1);
+ }
+ printf("Results: "); // always the average of last samples
+ sum = 0;
+ for (int i = 0; i < numSamples; i++) {
+ sum = sum + pot1Samples[i];
+ }
+ result = sum/numSamples;
+ printf("%d \n", result);
+ }
+}
+
+// The main() is in it's own thread.
+int main()
+{
+ // Create a thread to execute the function led2_thread
+ threadL.start(led2_thread);
+ // led2_thread is executing concurrently with the main and other threads
+
+
+ // Create a thread to execute the function pot1_thread
+ threadP.start(pot1_thread);
+ // pot1_thread is executing concurrently with the main and other threads
+
+
+ while (true) {
+ led1 = !led1;
+ printf("Led1 blink %d \n", led1.read());
+ ThisThread::sleep_for(500);
+ }
+}
diff -r 000000000000 -r 156098cfb039 mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Thu Jun 25 15:51:09 2020 +0000 @@ -0,0 +1,1 @@ +https://github.com/armmbed/mbed-os/