Browse Source

date and web

pull/1/head
Rafael Zurita 6 years ago
parent
commit
929d3c31ea
  1. 6
      README.md
  2. 1
      include/shprototypes.h
  3. 6
      index.html
  4. 121
      lib/date.c
  5. 41
      main/main.c
  6. 26
      main/xsh_date.c

6
README.md

@ -69,6 +69,12 @@ Notes about the port:
5. Several limits for buffers: 32bytes for tty input, 16bytes for names of devices, 1byte for the queues keys, and the list continues
6. sleepms() is now delay sleep100ms()
7. Many vars in data structures have a smaller size (e.g. before:int32, now:char)
8. sleep sleeps max. 20 seconds (date type)
9. most of the libc are from avr-libc
10. init (load bss and data from flash to ram) from avr-libc
11. shell manages max. 6 tokens
12. date and time is managed by a little lib. No NTP or RTC
13. Most of the const char in source code was moved to FLASH (program space) via __flash directive from gcc, or PROGMEM from avr-libc
**Acknowledgments**

1
include/shprototypes.h

@ -86,3 +86,4 @@ extern shellcmd xsh_free (int32, char *[]);
extern shellcmd xsh_forever (int32, char *[]);
extern shellcmd xsh_reboot (int32, char *[]);
extern shellcmd xsh_cal (int32, char *[]);
extern shellcmd xsh_date (int32, char *[]);

6
index.html

@ -90,6 +90,12 @@ Xinu uses powerful primitives to provides all the componentes and the same funct
<li>Several limits for buffers: 32bytes for tty input, 16bytes for names of devices, 1byte for the queues keys, and the list continues</li>
<li>sleepms() is now delay sleep100ms()</li>
<li>Many vars in data structures have a smaller size (e.g. before:int32, now:char)</li>
<li>sleep sleeps max. 20 seconds (date type)</li>
<li>most of the libc are from avr-libc</li>
<li>init (load bss and data from flash to ram) from avr-libc</li>
<li>shell manages max. 6 tokens</li>
<li>date and time is managed by a little lib. No NTP or RTC</li>
<li>Most of the const char in source code was moved to FLASH (program space) via __flash directive from gcc, or PROGMEM from avr-libc</li>
</ol>
<p><strong>Acknowledgments</strong></p>
<ul>

121
lib/date.c

@ -1,42 +1,32 @@
#include <stdint.h>
typedef long int32;
typedef unsigned long uint32;
typedef long unsigned int size_t;
typedef uint32_t time_t;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#define TRUE 1
#define FALSE 0
struct tm avr_tm;
char days_per_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/*
const char * strp_weekdays[] =
{ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
const char * strp_monthnames[] =
{ "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
*/
time_t seconds;
//time_t sec_stamp;
#define SECONDS_PER_DAY (60*60*24)
extern clktime;
unsigned long int seconds = 0;
unsigned int days = 0;
int get_date(char * s)
{
struct tm avr_tm;
// unsigned long long int seconds = 0;
// seconds = seconds + (clktime - sec_stamp);
seconds = seconds + clktime;
localtime_r(&seconds, &avr_tm);
asctime_r(&avr_tm, s);
}
int date(const char *s) {
int set_date(const char *s)
{
struct tm avr_tm;
int m, d, y;
int hour, min, sec;
int i;
char two[3];
two[2] = 0;
@ -55,13 +45,8 @@ int date(const char *s) {
/* year */
two[0] = *s; s++; two[1] = *s; s++;
// y = 2000 + atoi(two);
y = atoi(two);
/*
while (isspace((int)(*s)))
++s;
*/
s++;
/* hour */
@ -80,89 +65,21 @@ int date(const char *s) {
two[0] = *s; s++; two[1] = *s; s++;
sec = atoi(two);
for (i=2000; i<y; i++) {
if ( i % 4 == 0 && i % 100 != 0 || i % 400 == 0 )
days = days + 366;
//seconds = seconds + (366 * SECONDS_PER_DAY);
else
days = days + 365;
//seconds = seconds + (365 * SECONDS_PER_DAY);
}
for (i=1; i<m; i++) {
days = days + days_per_month[i-1];
//seconds = seconds + (days_per_month[i-1] * SECONDS_PER_DAY);
if ((i == 2) && ( y % 4 == 0 && y % 100 != 0 || y % 400 == 0 ))
days++;
//seconds = seconds + (SECONDS_PER_DAY);
}
days = days + d;
//seconds = seconds + (d * SECONDS_PER_DAY);
seconds = 0;
seconds = seconds + (hour * 3600) + (min * 60) + sec;
printf("h:%d\n", hour);
printf("m:%d\n", min);
printf("s:%d\n", sec);
printf("d:%d\n", d);
printf("m:%d\n", m);
printf("y:%d\n", y);
avr_tm.tm_sec = sec;
avr_tm.tm_min = min;
avr_tm.tm_hour = hour;
avr_tm.tm_mon = m - 1;
avr_tm.tm_mday = d;
avr_tm.tm_year = y + 100;
avr_tm.tm_isdst = 0;
// seconds = mktime(&avr_tm);
seconds = mktime(&avr_tm) - clktime;
// sec_stamp = clktime; /* WARNING: interrupts should be disable before */
return 0;
}
const unsigned int dinamonth[13] = {0,0,0,31,30,31,30,31,31,30,31,30,31};
//const unsigned long secinamonth[13] = {0,0,0,31*86400,30*86400,31*86400,30*86400,31*86400,31*86400,30*86400,31*86400,30*86400,31*86400};
void seconds_to_date(int *y, int *m, int *d, int *h, int *min, int *s) // unsigned 32bit input
{
unsigned long int dd = days;
unsigned long int ss = seconds;
char temp = 0; // start at year 2000
while (dd >= 1460 && temp <100) { dd -= 1460; temp +=4;} // four year blocks until 2100
// while (sec >= 126230400 && temp <100) { sec -= 126230400; temp +=4;} // four year blocks until 2100
if (temp == 100){while (dd >= 366 && temp <104){ dd -= 366; temp +=1;}}//no leapyears
//if (temp == 100){while (sec >= 31536000 && temp <104){ sec -= 31536000; temp +=1;}}//no leapyears
while (dd >= 1460){ dd -= 1460; temp +=4;}// four year blocks again
//while (sec >= 126230400){ sec -= 126230400; temp +=4;}// four year blocks again
if (dd >= 366){ dd -= 366; temp +=1; // first year after a 4yr block is a leapyear
// if (sec >= 31622400){ sec -= 31622400; temp +=1; // first year after a 4yr block is a leapyear
while (dd >= 366){ dd -= 366; temp +=1;}// then it can only be two years left
// while (sec >= 31536000){ sec -= 31536000; temp +=1;}// then it can only be two years left
}
*y = temp; temp = 1; // let it go past 99 to max 136 for year 2136
if (dd >= 31){ // January
//if (sec >= 86400*31){ // January
dd -= 31; temp +=1;
int tempfeb = 28;
if (*y !=100 && !(*y & 3)) tempfeb = 29;// 2100 is no leap year
//if (rtc.year !=100 && !(rtc.year & 3)) tempfeb = 29;// 2100 is no leap year
if (dd >= tempfeb){
//if (sec >= tempfeb){
dd -= tempfeb; temp +=1; // do the other months now
// sec -= tempfeb; temp +=1; // do the other months now
while (dd >= dinamonth[temp]){ dd -= dinamonth[temp]; temp +=1;}
//while (sec >= secinamonth[temp]){ sec -= secinamonth[temp]; temp +=1;}
}
}
*m = temp; temp = 1;
while (dd >= 1){ dd -= 1; temp +=1;} // days
*d = temp; temp = 0;
while (ss >= 3600){ ss -= 3600; temp +=1;} // hour
*h = temp; temp = 0;
while (ss >= 60){ ss -= 60; temp +=1;} // minute
*min = temp;
*s = ss; // seconds
}

41
main/main.c

@ -8,12 +8,9 @@
#include <time.h>
extern int date (char *s);
extern void seconds_to_date(int *y, int *m, int *d, int *h, int *min, int *s);
extern seconds;
extern int days;
extern struct tm avr_tm;
//extern long long int seconds;
extern int set_date (char *s);
extern int get_date (char *s);
// extern struct tm avr_tm;
void xsh_help(void);
shellcmd xsh_kill(int nargs, char *args[]);
@ -40,6 +37,7 @@ const __flash uint8_t * const __flash cmdtab_cname[] =
(const __flash uint8_t[]) { "clear" },
(const __flash uint8_t[]) { "ps" },
(const __flash uint8_t[]) { "echo" },
(const __flash uint8_t[]) { "date" },
(const __flash uint8_t[]) { "cal" }
};
@ -58,6 +56,7 @@ const __flash uint8_t * const __flash cmdtab_help[] =
(const __flash uint8_t[]) { ": clear the terminal screen" },
(const __flash uint8_t[]) { ": display current processes table" },
(const __flash uint8_t[]) { "[arg ...] : write arguments to standard output" },
(const __flash uint8_t[]) { "[MM/DD/YY HH:MM:SS] : set or get the date and time" },
(const __flash uint8_t[]) { "[mon] year : calendar" }
};
typedef int32 (*cmdfunc_t)(int32,char*[]);
@ -76,6 +75,7 @@ const cmdent_t __flash cmdtab[] = {
{TRUE, xsh_clear},
{TRUE, xsh_ps},
{FALSE, xsh_echo},
{FALSE, xsh_date},
{FALSE, xsh_cal}
};
@ -94,6 +94,7 @@ const __flash int cmdtab_stk[] = {
128,
256,
256,
256,
};
//const __flash int32 (*f2)(int32,char*[]);
@ -208,22 +209,22 @@ process main(void)
fprintf(dev, "%s\n\n", SHELL_STRTMSG);
*/
unsigned int ss = 0;
int y, m, d, h, min, s;
ss = date("06/30/20 13:21:30");
printf("dias:%i\n", days);
seconds_to_date(&y, &m, &d, &h, &min, &s);
printf("y:%i\n", y);
printf("m:%i\n", m);
printf("d:%i\n", d);
printf("h:%i\n", h);
printf("m:%i\n", min);
printf("s:%i\n", s);
// int ss = 0;
// int y, m, d, h, min, s;
//ss = set_date("06/30/20 13:21:30");
// printf("dias:%i\n", days);
// seconds_to_date(&y, &m, &d, &h, &min, &s);
// printf("y:%i\n", y);
// printf("m:%i\n", m);
// printf("d:%i\n", d);
// printf("h:%i\n", h);
// printf("m:%i\n", min);
// printf("s:%i\n", s);
char avr_date[80];
asctime_r(&avr_tm, avr_date);
printf("f:%s\n", avr_date);
// char avr_date[80];
// get_date(avr_date);
// printf("f:%s\n", avr_date);
/*
unsigned char destination[8];
for(int i=0; i<8; ++i) {

26
main/xsh_date.c

@ -0,0 +1,26 @@
/* xsh_date.c - xsh_date */
#include <xinu.h>
#include <stdio.h>
/*------------------------------------------------------------------------
* xsh_date - set or get the date and time
*------------------------------------------------------------------------
*/
shellcmd xsh_date(int nargs, char *args[])
{
char avr_date[80];
if (nargs == 1) {
get_date(avr_date);
printf("\n%s\n\n", avr_date);
return 0;
}
if (nargs != 3)
return -1;
set_date(args[1], args[2]);
return 0;
}
Loading…
Cancel
Save