FSST Test

Committer:
Lukas_Adlboller
Date:
Tue Jan 28 09:23:17 2020 +0000
Revision:
0:90f7dbc52b88
FSST Test: Duty Cycle

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Lukas_Adlboller 0:90f7dbc52b88 1 #include "mbed.h"
Lukas_Adlboller 0:90f7dbc52b88 2
Lukas_Adlboller 0:90f7dbc52b88 3 Thread ledThread;
Lukas_Adlboller 0:90f7dbc52b88 4 DigitalOut led(LED2);
Lukas_Adlboller 0:90f7dbc52b88 5 Serial serialPC(USBTX, USBRX, 9600);
Lukas_Adlboller 0:90f7dbc52b88 6 Mutex dutyCycleMutex;
Lukas_Adlboller 0:90f7dbc52b88 7
Lukas_Adlboller 0:90f7dbc52b88 8 const int period = 20; // ms
Lukas_Adlboller 0:90f7dbc52b88 9 int dutyCycle = 50; // %
Lukas_Adlboller 0:90f7dbc52b88 10
Lukas_Adlboller 0:90f7dbc52b88 11
Lukas_Adlboller 0:90f7dbc52b88 12 void ledOutput()
Lukas_Adlboller 0:90f7dbc52b88 13 {
Lukas_Adlboller 0:90f7dbc52b88 14 while(true)
Lukas_Adlboller 0:90f7dbc52b88 15 {
Lukas_Adlboller 0:90f7dbc52b88 16 if(dutyCycle != 0)
Lukas_Adlboller 0:90f7dbc52b88 17 {
Lukas_Adlboller 0:90f7dbc52b88 18 dutyCycleMutex.lock();
Lukas_Adlboller 0:90f7dbc52b88 19 float onTime = (dutyCycle / 100.0f) * period;
Lukas_Adlboller 0:90f7dbc52b88 20 dutyCycleMutex.unlock();
Lukas_Adlboller 0:90f7dbc52b88 21
Lukas_Adlboller 0:90f7dbc52b88 22 led = 1;
Lukas_Adlboller 0:90f7dbc52b88 23 ThisThread::sleep_for(onTime);
Lukas_Adlboller 0:90f7dbc52b88 24 led = 0;
Lukas_Adlboller 0:90f7dbc52b88 25 ThisThread::sleep_for(period - onTime);
Lukas_Adlboller 0:90f7dbc52b88 26 }
Lukas_Adlboller 0:90f7dbc52b88 27 }
Lukas_Adlboller 0:90f7dbc52b88 28 }
Lukas_Adlboller 0:90f7dbc52b88 29
Lukas_Adlboller 0:90f7dbc52b88 30 int main()
Lukas_Adlboller 0:90f7dbc52b88 31 {
Lukas_Adlboller 0:90f7dbc52b88 32 ledThread.start(ledOutput);
Lukas_Adlboller 0:90f7dbc52b88 33
Lukas_Adlboller 0:90f7dbc52b88 34 while(true)
Lukas_Adlboller 0:90f7dbc52b88 35 {
Lukas_Adlboller 0:90f7dbc52b88 36 dutyCycleMutex.lock();
Lukas_Adlboller 0:90f7dbc52b88 37 serialPC.printf("Duty: %d\n", dutyCycle);
Lukas_Adlboller 0:90f7dbc52b88 38 dutyCycleMutex.unlock();
Lukas_Adlboller 0:90f7dbc52b88 39 char currentChar = serialPC.getc();
Lukas_Adlboller 0:90f7dbc52b88 40
Lukas_Adlboller 0:90f7dbc52b88 41 if(currentChar == 'Z')
Lukas_Adlboller 0:90f7dbc52b88 42 {
Lukas_Adlboller 0:90f7dbc52b88 43 dutyCycleMutex.lock();
Lukas_Adlboller 0:90f7dbc52b88 44 dutyCycle = 0;
Lukas_Adlboller 0:90f7dbc52b88 45 dutyCycleMutex.unlock();
Lukas_Adlboller 0:90f7dbc52b88 46 }
Lukas_Adlboller 0:90f7dbc52b88 47 else if(currentChar >= '1' && currentChar <= '9')
Lukas_Adlboller 0:90f7dbc52b88 48 {
Lukas_Adlboller 0:90f7dbc52b88 49 dutyCycleMutex.lock();
Lukas_Adlboller 0:90f7dbc52b88 50 dutyCycle = (currentChar - '0') * 10;
Lukas_Adlboller 0:90f7dbc52b88 51 dutyCycleMutex.unlock();
Lukas_Adlboller 0:90f7dbc52b88 52 }
Lukas_Adlboller 0:90f7dbc52b88 53 else if(currentChar == 'F')
Lukas_Adlboller 0:90f7dbc52b88 54 {
Lukas_Adlboller 0:90f7dbc52b88 55 dutyCycleMutex.lock();
Lukas_Adlboller 0:90f7dbc52b88 56 dutyCycle = 100;
Lukas_Adlboller 0:90f7dbc52b88 57 dutyCycleMutex.unlock();
Lukas_Adlboller 0:90f7dbc52b88 58 }
Lukas_Adlboller 0:90f7dbc52b88 59 }
Lukas_Adlboller 0:90f7dbc52b88 60 }