Revision 20

View differences:

logic/trunk/src/init/buf.h
1
#ifndef BUF_H_INCLUDED
2
#define BUF_H_INCLUDED
3

  
4
typedef int element_t;
5

  
6
//int *buf;
7

  
8
typedef struct d_buffer
9
{
10
    // buffer pointers
11
    int *start_p;
12
    int *end_p;
13
    // TODO (fix and remove this one?) pointer to the first element, had problems with malloc without it!
14
    int *start_el;
15
    // actual buffer
16
    element_t *elements;
17
    // attributes
18
    int buffer_size;
19
    int buffer_load;
20
}d_bufr;
21

  
22
void buf_put(element_t element, d_bufr *buf);
23
int  buf_get(element_t *element, d_bufr *buf);
24
void increase_start_p(d_bufr *buf);
25
int  increase_end_p(d_bufr *buf);
26
d_bufr *init_buf(int buf_size);
27
int  get_buffer_load(d_bufr *buf);
28

  
29
#endif // BUF_H_INCLUDED
logic/trunk/src/init/scenarios.xml
1
<?xml version="1.0"?>
2
<scenariotree>
3
    <scenario id="ac_01">
4
        <name nam="Air Conditioner"/>
5
        <minvalue minval="18"/>
6
        <maxvalue maxval="27"/>
7
	<optimal_value optVal="23"/>
8
	<idle_check_interval ici="600"/>
9
	<active_check_interval aci="600"/>
10
	<sensors chk_dev1="thermometer_01" chk_dev2="thermometer_01"/>
11
	...
12
	<reaction_function fr="linear"/>
13
        <action act="testAction_1"/>
14
        <param par="testParam_1"/>
15
    </scenario>
16
</scenariotree>
17

  
18
name
19
action path
20
param
21
idle check interval (s)
22
active check interval (s)
23
influence devices
24
reaction values //treba razdelat
25
function type //static (on - off), linear, log etc. (kako se spreminja moč na intervalu)
26

  
logic/trunk/src/init/buf.c
1
#include "buf.h"
2
#include <stdlib.h>
3

  
4
d_bufr *init_buf(int buf_size)
5
{
6
    printf("-----------------\n");
7
    printf("Creating buffer; size %d!\n", buf_size);
8

  
9
    d_bufr *buf;
10
    buf = (d_bufr *)malloc(sizeof(d_bufr));
11
    //*
12
    buf->buffer_size = buf_size;
13
    // TODO should allocate (element_t *) type instead of (int *)?
14
    // malloc(BUFFER_SIZE * sizeof (element_t));
15
    buf->elements = malloc(buf_size * sizeof (element_t));
16

  
17
    buf->start_p = &buf->elements;
18
    buf->start_el= &buf->elements;
19
    //buf->end_p = buf->start_p+buf->buffer_size-1;
20
    buf->end_p   = &buf->elements;
21
    buf->buffer_load = 0;
22

  
23
    /*
24
    // TODO just a bunch of tests for deletion
25
    //printf("buf addr: %d\n", buf);
26
    printf("buf start_p: %d\n", buf->start_p);
27
    //printf("buf &start_p: %d\n", &buf->start_p);
28
    //printf("buf *start_p: %d\n", *buf->start_p);
29
    printf("buf end_p: %d\n", buf->end_p);
30
    //printf("buf elements: %d\n", buf->elements);
31
    printf("buf &elements: %d\n", &buf->elements);
32
    //printf("buf size: %d\n", buf->buffer_size);
33
    //printf("buf load %d\n", buf->buffer_load);
34

  
35
    // TODO possilbe bug in buffer allocation! following sums should be the same
36
    // buf start_p + buffer_size - buf start_p should be 4 * buffer_size
37
    // in first case it is, but in second it is 2 times bigger!
38
    printf("buf start_p+buffer_size: %d\n", buf->start_p+buf_size);
39
    printf("&buf->elements+buf->buffer_size: %d\n", &buf->elements+buf->buffer_size);
40
    printf("sizeof(buf->elements) %d\n", sizeof(buf->elements));
41
    printf("sizeof(element_t) %d\n", sizeof(element_t));
42
    /**/
43
    return buf;
44
}
45

  
46
void increase_start_p(d_bufr *buf)
47
{
48
    /*
49
    printf("pre buf->start_p \t%d\n",buf->start_p);
50
    printf("pre buf->end_p \t\t%d\n",buf->end_p);
51
    printf("limit address \t\t%d\n", buf->start_el + buf->buffer_size);
52
    printf("-\n");
53
    /**/
54
    if((buf->start_p+1)!=(buf->start_el + buf->buffer_size)) // no loop
55
    {
56
        //pomakni se endp ce je isti naslov kot startp
57
        //printf("no loop\n");
58
        if((buf->start_p+1)==(buf->end_p))
59
        {
60
            //printf("lol fail\n");
61
            increase_end_p(buf);
62
        }
63
        else
64
        {
65
            //printf("ok zakon\n");
66
            buf->buffer_load++;
67
        }
68
        buf->start_p++;
69
    }
70
    else //loop
71
    {
72
        if(buf->end_p==buf->start_el)
73
            increase_end_p(buf);
74
        else
75
            buf->buffer_load++;
76
        buf->start_p = buf->start_el;
77
    }
78
    //printf("after buf->start_p \t%d\n",buf->start_p);
79
}
80

  
81
int increase_end_p(d_bufr *buf)
82
{
83
    //printf("pre buf->end_p \t\t%d\n",buf->end_p);
84
    if((buf->end_p+1)!=(buf->start_el + buf->buffer_size)) // no loop
85
    {
86
        if((buf->end_p+1)==(buf->start_p)) //buffer empty
87
            return 0;
88
        buf->end_p++;
89
    }
90
    else //loop
91
    {
92
        if(buf->start_p==buf->elements) //buffer empty
93
            return 0;
94
        buf->end_p = buf->elements;
95
    }
96
    //printf("after buf->end_p \t%d\n",buf->end_p);
97
    return 1;
98
}
99

  
100
void buf_put(element_t element, d_bufr *buf)
101
{
102
    *buf->start_p = element;
103
    increase_start_p(buf);
104
}
105

  
106
int buf_get(element_t *element, d_bufr *buf)
107
{
108
    *element = *buf->end_p;
109
    if(increase_end_p(buf)==1){
110
        buf->buffer_load--;
111
        return 1;
112
    }
113
    else //error
114
        return 0;
115
}
116

  
117
int get_buffer_load(d_bufr *buf)
118
{
119
    return buf->buffer_load;
120
}

Also available in: Unified diff