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.
Dependencies: mbed
main.cpp@0:943cf409664f, 2019-03-19 (annotated)
- Committer:
- faraonemena
- Date:
- Tue Mar 19 10:37:59 2019 +0000
- Revision:
- 0:943cf409664f
thread
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| faraonemena | 0:943cf409664f | 1 | /* Copyright (c) 2017 STMicroelectronics |
| faraonemena | 0:943cf409664f | 2 | * |
| faraonemena | 0:943cf409664f | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| faraonemena | 0:943cf409664f | 4 | * you may not use this file except in compliance with the License. |
| faraonemena | 0:943cf409664f | 5 | * You may obtain a copy of the License at |
| faraonemena | 0:943cf409664f | 6 | * |
| faraonemena | 0:943cf409664f | 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| faraonemena | 0:943cf409664f | 8 | * |
| faraonemena | 0:943cf409664f | 9 | * Unless required by applicable law or agreed to in writing, software |
| faraonemena | 0:943cf409664f | 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| faraonemena | 0:943cf409664f | 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| faraonemena | 0:943cf409664f | 12 | * See the License for the specific language governing permissions and |
| faraonemena | 0:943cf409664f | 13 | * limitations under the License. |
| faraonemena | 0:943cf409664f | 14 | */ |
| faraonemena | 0:943cf409664f | 15 | |
| faraonemena | 0:943cf409664f | 16 | /**************************************************** |
| faraonemena | 0:943cf409664f | 17 | * RAPID PROTOTYPING WITH NUCLEO * |
| faraonemena | 0:943cf409664f | 18 | * Example Code 07: Thread usage * |
| faraonemena | 0:943cf409664f | 19 | * Author: Mauro D'Angelo * |
| faraonemena | 0:943cf409664f | 20 | * Organization: STMicroelectronics * |
| faraonemena | 0:943cf409664f | 21 | *****************************************************/ |
| faraonemena | 0:943cf409664f | 22 | |
| faraonemena | 0:943cf409664f | 23 | #include "mbed.h" |
| faraonemena | 0:943cf409664f | 24 | |
| faraonemena | 0:943cf409664f | 25 | // It declares a thread |
| faraonemena | 0:943cf409664f | 26 | Thread thread; |
| faraonemena | 0:943cf409664f | 27 | |
| faraonemena | 0:943cf409664f | 28 | // mybutton variable's type is DigitalIn. It points to the user button available on the board |
| faraonemena | 0:943cf409664f | 29 | DigitalIn mybutton(USER_BUTTON); |
| faraonemena | 0:943cf409664f | 30 | |
| faraonemena | 0:943cf409664f | 31 | DigitalOut myled(LED1); |
| faraonemena | 0:943cf409664f | 32 | |
| faraonemena | 0:943cf409664f | 33 | // It is the interval |
| faraonemena | 0:943cf409664f | 34 | float delay = 0; |
| faraonemena | 0:943cf409664f | 35 | |
| faraonemena | 0:943cf409664f | 36 | // Implementa la funzione associata al Thread |
| faraonemena | 0:943cf409664f | 37 | // Quando viene premuto il pulsante (mybutton == 0) |
| faraonemena | 0:943cf409664f | 38 | // viene impostato il nuovo valore della variabile delay |
| faraonemena | 0:943cf409664f | 39 | |
| faraonemena | 0:943cf409664f | 40 | // This is the function started as thread |
| faraonemena | 0:943cf409664f | 41 | void button_thread(void) { |
| faraonemena | 0:943cf409664f | 42 | while(true) { |
| faraonemena | 0:943cf409664f | 43 | if (mybutton == 0) { // Button is pressed |
| faraonemena | 0:943cf409664f | 44 | if (delay == 1.0) |
| faraonemena | 0:943cf409664f | 45 | delay = 0.2; // 200 ms |
| faraonemena | 0:943cf409664f | 46 | else |
| faraonemena | 0:943cf409664f | 47 | delay = 1.0; // 1 sec |
| faraonemena | 0:943cf409664f | 48 | wait(.2); |
| faraonemena | 0:943cf409664f | 49 | }// if button |
| faraonemena | 0:943cf409664f | 50 | wait(.05); |
| faraonemena | 0:943cf409664f | 51 | } |
| faraonemena | 0:943cf409664f | 52 | } |
| faraonemena | 0:943cf409664f | 53 | |
| faraonemena | 0:943cf409664f | 54 | int main() { |
| faraonemena | 0:943cf409664f | 55 | // Threads start here. Here it is attached the function |
| faraonemena | 0:943cf409664f | 56 | thread.start(button_thread); |
| faraonemena | 0:943cf409664f | 57 | |
| faraonemena | 0:943cf409664f | 58 | // Blinking led |
| faraonemena | 0:943cf409664f | 59 | while(true) { |
| faraonemena | 0:943cf409664f | 60 | myled = !myled; |
| faraonemena | 0:943cf409664f | 61 | wait(delay); |
| faraonemena | 0:943cf409664f | 62 | } |
| faraonemena | 0:943cf409664f | 63 | } |