davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

a few hints needed: reading input string, parse to ints

Fri Apr 22, 2016 7:00 pm

hi,
I would need a few hints about how to read an input string, parse several substr data to ints, then pass them to a function.

I have a RTC and I want to read a new time+date string, parse the time data to ints, and pass them to a set_time function



edit,
I now made it this way, it works not perfect but reasonable:

Code: Select all

/*
 * 
 * RTC DS3231
 * 
 * test demo
 * ver 0001
 * 
 */
 
 
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>

#include <wiringPi.h>
#include <wiringPiI2C.h>

#define  byte  uint8_t
 
// RTC DS3231
#define ADDR_RTCDS3231 0x68
int     frtcds3231;



//=====================================================================================
// Convert normal decimal numbers to binary coded decimal
//=====================================================================================
byte decToBcd(byte val) {  return( (val/10*16) + (val%10) );  }


//=====================================================================================
// Convert binary coded decimal to normal decimal numbers
//=====================================================================================
byte bcdToDec(byte val)  {  return( (val/16*10) + (val%16) ); }


//=====================================================================================




void setDS3231time( byte year, byte month, byte dayOfMonth, byte hour, byte
minute, byte second, byte dayOfWeek)
{
  // sets time and date data to DS3231  
  
  wiringPiI2CWriteReg8(frtcds3231, 0, decToBcd(second));      // set seconds
  wiringPiI2CWriteReg8(frtcds3231, 1, decToBcd(minute));      // set minutes
  wiringPiI2CWriteReg8(frtcds3231, 2, decToBcd(hour));        // set hours
  wiringPiI2CWriteReg8(frtcds3231, 3, decToBcd(dayOfWeek));   // ( 1=Sunday, 7=Saturday)
  wiringPiI2CWriteReg8(frtcds3231, 4, decToBcd(dayOfMonth));  // set dayOfMonth (1 to 31)
  wiringPiI2CWriteReg8(frtcds3231, 5, decToBcd(month));       // set month
  wiringPiI2CWriteReg8(frtcds3231, 6, decToBcd(year));        // set year (0 to 99)
   
}


//=====================================================================================



int main() {
   int  year, month, dayOfMonth, hour, minute, second, dayOfWeek;   
   int i=0, check;
   char sbuf[100];   

   frtcds3231 = wiringPiI2CSetupInterface( "/dev/i2c-0",  ADDR_RTCDS3231 );	   
   
   printf(" RTC DS3231 \n");
   printf("Set new Date/Time:   enter 1\n");
   printf("else:   display time\n\n");

   i = getchar();
   
   //debug
   //printf("%d \n", i);
   
   while (i=='1') {
      // get string yy mm dd hh mm ss dw : gets() ?
      printf("yy mm dd hh mm ss dw (DayOfWeek) \n");
      check=scanf("%d %d %d %d %d %d %d", &year, &month, &dayOfMonth,  &hour, &minute, &second, &dayOfWeek);
      
      getchar();
      printf("check=%d\n", check);
      
      if(check==7) {     
		 printf("%d \n", year); 
		 printf("%d \n", month); 
		 printf("%d \n", dayOfMonth); 
		 printf("%d \n", hour); 
		 printf("%d \n", minute); 
		 printf("%d \n", second); 
		 printf("%d \n", dayOfWeek); 
         setDS3231time( year, month, dayOfMonth, hour, minute, second, dayOfWeek );
      }      // parse string to decimal values: scanf() ?
      // pass values to setDS3231time()
      
      
      printf(" RTC DS3231 \n");
      printf("Set new Date/Time:   enter 1\n");
      printf("else:   display time\n\n");
      i=0;
      i = getchar();
      
      
   }
   

 
   while(1) {
     second =     bcdToDec(wiringPiI2CReadReg8 (frtcds3231, 0) & 0x7f );
     minute =     bcdToDec(wiringPiI2CReadReg8 (frtcds3231, 1) );
     hour =       bcdToDec(wiringPiI2CReadReg8 (frtcds3231, 2) & 0x3f );
     dayOfWeek =  bcdToDec(wiringPiI2CReadReg8 (frtcds3231, 3) );
     dayOfMonth = bcdToDec(wiringPiI2CReadReg8 (frtcds3231, 4) );
     month =      bcdToDec(wiringPiI2CReadReg8 (frtcds3231, 5) );
     year =       bcdToDec(wiringPiI2CReadReg8 (frtcds3231, 6) );  
	 -
     sprintf(sbuf, "20%02d/%02d/%02d  %02d:%02d:%02d", year, month, dayOfMonth, hour, minute, second); 
     printf(sbuf);
     
     printf(" Day of week %1d: ", dayOfWeek);
     switch(dayOfWeek){
     
     case 1:
       printf("Sunday");
       break;
     case 2:
       printf("Monday");
       break;
     case 3:
       printf("Tuesday");
       break;
     case 4:
       printf("Wednesday");
       break;
     case 5:
       printf("Thursday");
       break;
     case 6:
       printf("Friday");
       break;
     case 7:
       printf("Saturday");
       break;
     }
     printf("\n");		
     
     delay (1000);	     
     
	    	 
  }

  return (0);
}
//=====================================================================================
//=====================================================================================

#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: a few hints needed: reading input string, parse to ints

Fri Apr 22, 2016 8:14 pm

- resolved, more or less -
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

swampdog
Posts: 230
Joined: Fri Dec 04, 2015 11:22 am

Re: a few hints needed: reading input string, parse to ints

Sat Apr 23, 2016 11:03 am

This is how to load a file into memory, with & without exceptions...

bool FC
sdFileLoad(
sdConStr * cs,
const sdStr & fname,
bool append,
BARF barf
) SD_THROW ((sde_Null,sde_File))
{
SDE_NULL(cs);
sdBifs ifs (fname.c_str());
sdStr s;

if (!ifs) {
if (barf)
throw sde_File(SD_SFL(sdFileLoad),fname)
;
return false;
}

if (!append)
cs->clear()
;

while (std::getline(ifs,s))
cs->push_back(s)
;

if (ifs.bad()) {
if (barf)
throw sde_File(SD_SFL(sdFileLoad),fname)
;
return false;
}

return true;
}

..it shows all the tests. Fwiw "sdConStr" is an array of deque strings (actually a template)

Return to “C/C++”