CARLO TARALLO / Mbed OS time_Vibra

main.cpp

Committer:
carlotara
Date:
2020-03-13
Revision:
7:c336250d2897
Parent:
6:f1b1ceaae23e
Child:
8:281ad9672a53

File content as of revision 7:c336250d2897:

/* mbed Example Program
 * Copyright (c) 2006-2014 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "mbed.h"


long dataUnix = 0;
int settore = 0;
char Source[50]="working with strings is fun 0123456789 9876543210";
char Destination[50];
int pos,len;

DigitalIn vibra(D7);
//AnalogIn vibration_Sensor(A5);
int present_condition = 0;
int previous_condition = 0;
//float present_condition = 0;
//float previous_condition = 0;

void substring(char *s,char *d,int pos,int len) {
//usage: substring(Source,Destination,pos,len);
 
    char *t;
 
    s=s+(pos-1);
    t=s+len;
    while (s!=t) {
        *d=*s;
        s++;
        d++;
    }
    *d='\0';
}

int main() {


        printf("Inserisci numero della data UNIX \r\n");
        scanf("%li", &dataUnix);
        printf("%li\n",dataUnix);

        printf("Inserisci numero Settore \r\n");
        scanf("%li", &settore);
        printf("%li\n",dataUnix);


//    set_time(1256729737);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
  //  set_time(1583715900);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
    set_time(dataUnix);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
   
    while (true) {
        previous_condition = present_condition;
        present_condition = vibra.read();
 //       present_condition = vibration_Sensor.read();        
                                printf(" VALORE : %d\r\n", present_condition);   
                           //      printf(" VALORE : %1.3f\r\n", present_condition);  
        if (previous_condition != present_condition) {
     //   if (present_condition == 1) {
                                printf(" Pillola caduta : %d\r\n", present_condition);  
            
   //         printf("Pillola caduta \n");
 
            }
   //          else {
  //      printf("Pillola NON caduta \n");
  //      }
 
 
        time_t seconds = time(NULL);
        
        printf("Time as seconds since January 1, 1970 = %u\n", (unsigned int)seconds);
        
        printf("Time as a basic string = %s", ctime(&seconds));

        char buf[3];

  

    // seconds = time(NULL);   // get current time from mbed RTC
        strftime(buf,3, "%a,%d %m %Y.%H:%M:%S\r", localtime(&seconds));

        char mon[30]="Mon";
        char tue[30]="Tue";
        char wed[30]="Wed";
        char thu[30]="Thu";
        char fri[30]="Fri";
        char sat[30]="Sat";
        char sun[30]="Sun";        
 


   if(strncmp(buf, mon, 3)==0)
    {
        printf("Ho letto LUNEDI  \r\n");    
    }
   if(strncmp(buf, tue, 3)==0)
    {
        printf("Ho letto MARTEDI  \r\n");    
    }

   if(strncmp(buf, wed, 3)==0)
    {
        printf("Ho letto MERCOLEDIì  \r\n");    
    }  
   if(strncmp(buf, thu, 3)==0)
    {
        printf("Ho letto GIOVEDI  \r\n");    
    }
   if(strncmp(buf, fri, 3)==0)
    {
        printf("Ho letto VENERDI  \r\n");    
    }

   if(strncmp(buf, sat, 3)==0)
    {
        printf("Ho letto SABATO  \r\n");    
    }  
   if(strncmp(buf, sun, 3)==0)
    {
        printf("Ho letto DOMENICA  \r\n");    
    }  

        
//   Sub‐Strings
char *buff = ctime(&seconds);
//char *buff = "Hello World";
char* substr;
strncpy(substr, buff+7, 5); //begin index: 7 substring length: 5     
 
         printf("MESE = %s", substr);
        
  //          int pos,len;
 /*
    usbUART.printf("----------------------------------------------------------\r\n");
    usbUART.printf("                 substring demo                           \r\n");
    usbUART.printf("----------------------------------------------------------\r\n");
    usbUART.printf("source: %s\r\n", Source);
    usbUART.printf("----------------------------------------------------------\r\n");
    printf("input the start position, tap enter, input length, tap enter ");
    scanf("%d%d",&pos,&len);
    printf("\n\n");
 
 pos=1;
 len=3;
 
    substring(Source,Destination,pos,len);
    Source = ctime(&seconds);
    printf("s= %s\n\r", Source);
    printf("d= %s\n\r", Destination);
*/
        char buffer[32];
        strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
        printf("Time as a custom formatted string = %s", buffer);
        
   //     wait(1);
    }
}