Jozef Staník / Mbed OS Cvicenie_4_02

Files at this revision

API Documentation at this revision

Comitter:
joes
Date:
Tue Oct 27 12:47:27 2020 +0000
Parent:
3:84cd01f2ee7d
Commit message:
Uloha2CV04;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Oct 26 21:26:10 2020 +0000
+++ b/main.cpp	Tue Oct 27 12:47:27 2020 +0000
@@ -1,7 +1,13 @@
 #include "mbed.h"
 #include "MyLed.h"
+#include <string>
+#include <sstream>
+#include <iostream>
+#include <stdio.h>
+#include <stdlib.h>
 
-#define TICKER_TIME             60.0    // ticker period (1 second)
+
+#define TICKER_TIME             1.0    // ticker period (1 second)
 
 MyLed led(LED1);                    // TO DO: implement methotds and use this class
 
@@ -32,38 +38,72 @@
         counter = 0;
     }
 }
+PwmOut pwm(LED1);
+
+using namespace std;
 
 int main()
 {
     pc.printf("\r\n----- Start of application ----- \r\n");
     
     //    led = 0;
-        ticker.attach(&OnTick,TICKER_TIME);
+        //ticker.attach(&OnTick,TICKER_TIME);
     
     sw.rise(&RiseHandler);
 
     char z;
+    string a;
+    char buffer[128];
+    buffer[0]='\0';
+    uint32_t cislo;
     uint8_t period;
+    int len = 0;
+    int k;
+    int brightness = 0;
+    char pwmTxt[] = "pwm\r";
     while (true) {
         if(btnPressed) {
             pc.printf("Button was pressed. \r\n");
             btnPressed = false;
         }
         
-        if(tick) {
+        /*if(tick) {
             pc. printf("Counter value : %d \r\n", counter);
             tick = false;
-        }
-        /*
+        }*/
+        
         if(pc.readable()) {
             z = pc.getc();
-            pc.printf("Pressed key : %c \r\n", z);
-            period = z - '0';
-            if ((period >=0 )&&(period<=9)) {
-                pc.printf("\r\nperiod: %d \r\n",period);
-                led.SetBlinkPeriod(period);
-            } else
-                pc.printf("you entered the wrong value. \r\n");
-        }*/
+            buffer[len++] = z;
+            buffer[len] = '\0';
+            
+            if(z == '\r') {
+            pc.printf("%s\r\n",buffer);
+            len = 0;
+            
+            if(strcmp(buffer,pwmTxt)==0){
+                   while(1){
+                        for(int brightness=0; brightness<=100; brightness+=5){
+                            pwm=(brightness/100.0);
+                            ThisThread::sleep_for(50); 
+                        }    
+                        for(int brightness=100; brightness>=0; brightness-=5){
+                            pwm=(brightness/100.0);
+                            ThisThread::sleep_for(50);   
+                        }    
+                   }    
+            }
+            else{
+            cislo = std::atoi(buffer);
+                if((cislo>=0)&&(cislo<=100)){
+                    pwm.write(cislo/100.0);
+                } 
+                else
+                    pwm.write(1.0);   
+            }
+            
+          }
+        }
     }
+   
 }
\ No newline at end of file