Statistics
| Revision:

root / logic / trunk / src / drv_comm / generator.c @ 71

History | View | Annotate | Download (1.32 KB)

1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <time.h>
4
#include <string.h>
5
#include <unistd.h>
6

    
7
#include "generator.h"
8

    
9
//funkcija:
10
int generate_value() {
11
        // nastavitev, minimalne in maksimalne temperature:
12
        //funkcija, torej od ure 00:00 raste od minimalne do maximalne, ki jo doseže pri uri 12:00 in nato spet pada do minimalne pri 24:00
13
        double minimum=20;
14
        double maximum=27;
15

    
16
        time_t     now;
17
    struct tm  *ts;
18
    char       buf1[80];
19

    
20
    now = time(NULL);
21

    
22
    ts = localtime(&now);
23
    strcpy(buf1, asctime(ts));
24

    
25
        int x1 = buf1[11]-48;
26
        int x2 = buf1[12]-48;
27
        int m1 = buf1[14]-48;
28
        int m2 = buf1[15]-48;
29

    
30
        int minutes = (x1*10+x2)*60 + m1*10+m2;
31
        int variable = minutes;
32

    
33
        if(variable>720) {
34
        variable= 720-(variable-720);
35
        }
36

    
37
        double k = ((double)variable)/((double)720);
38

    
39
    //funkcija vraca vrednost na eno decimalko natancno, podobno kot preproste i2c naprave
40
        return (int) (minimum + k * (maximum-minimum))*10;
41
}
42
//*
43
char *int_to_str(int nbr)
44
{
45
  int   div;
46
  int   len;
47
  int   i;
48
  char  *res;
49

    
50
  res = malloc(4 * sizeof(*res));
51
  i = 0;
52
  while (i < 3)
53
    res[i++] = '\0';
54
  res[3] = '\0';
55
  i = 0;
56
  div = 1;
57
  len = 1;
58
  while (nbr / div >= 10)
59
    {
60
      div *= 10;
61
      len++;
62
    }
63
  while (len)
64
    {
65
      res[i++] = '0' + (nbr / div);
66
      len--;
67
      nbr %= div;
68
      div /= 10;
69
    }
70
  return (res);
71
}
72
/**/