~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to plugin_w1retap.c

  • Committer: Reinhard Tartler
  • Date: 2011-04-27 17:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 750.
  • Revision ID: siretart@tauware.de-20110427172415-6n4aptmvmz0eztvm
Tags: upstream-0.11.0~svn1143
ImportĀ upstreamĀ versionĀ 0.11.0~svn1143

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: plugin_w1retap.c
 
2
 *
 
3
 * plugin to perform simple file operations for w1retap
 
4
 *
 
5
 * Copyright (C) 2007 Jonathan Hudson <jh+w1retap@daria.co.uk>
 
6
 * Copyright (C) 2006 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 
7
 *
 
8
 * This file is part of LCD4Linux.
 
9
 *
 
10
 * LCD4Linux is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation; either version 2, or (at your option)
 
13
 * any later version.
 
14
 *
 
15
 * LCD4Linux is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
23
 *
 
24
 */
 
25
 
 
26
/*
 
27
 * This module returns a specific value associated with a key from a
 
28
 * file containing one or more lines formatted as KEY=VALUE.
 
29
 *
 
30
 * It is intended for use with the w1retap weather station logging
 
31
 * software <http://www.daria.co.uk/wx/tech.html>, but is applicable
 
32
 * to any similarly formatted file.
 
33
 */
 
34
 
 
35
/* 
 
36
 * exported functions:
 
37
 *
 
38
 * int plugin_init_file (void)
 
39
 *  adds various functions
 
40
 *
 
41
 */
 
42
 
 
43
 
 
44
#include "config.h"
 
45
 
 
46
#include <stdio.h>
 
47
#include <string.h>
 
48
#include <stdlib.h>
 
49
#include <ctype.h>
 
50
 
 
51
#include "debug.h"
 
52
#include "plugin.h"
 
53
 
 
54
#ifdef WITH_DMALLOC
 
55
#include <dmalloc.h>
 
56
#endif
 
57
 
 
58
/* function 'my_readkey' */
 
59
/* takes two arguments, file name and key */
 
60
/* returns value associated with key, trimmed to suitable dec places */
 
61
 
 
62
static void my_readkey(RESULT * result, RESULT * arg1, RESULT * arg2)
 
63
{
 
64
    char value[80], val2[80];
 
65
    FILE *fp;
 
66
    char *reqkey, *pval;
 
67
    size_t size;
 
68
 
 
69
    *value = 0;
 
70
    pval = value;
 
71
 
 
72
    reqkey = R2S(arg2);
 
73
    fp = fopen(R2S(arg1), "r");
 
74
 
 
75
    if (!fp) {
 
76
        error("w1retap couldn't open file '%s'", R2S(arg1));
 
77
        value[0] = '\0';
 
78
    } else {
 
79
        size = strlen(reqkey);
 
80
        while (!feof(fp) && pval == value) {
 
81
            fgets(val2, sizeof(val2), fp);
 
82
            if (*(val2 + size) == '=') {
 
83
                if (strncmp(val2, reqkey, size) == 0) {
 
84
                    char *p;
 
85
                    p = index(val2, ' ');
 
86
                    if (p == NULL) {
 
87
                        p = index(val2, '\n');
 
88
                    }
 
89
 
 
90
                    if (p) {
 
91
                        *p = 0;
 
92
                        pval = val2 + size + 1;
 
93
                        {
 
94
                            double d;
 
95
                            char *ep;
 
96
                            d = strtod(pval, &ep);
 
97
                            if (ep != pval && (*ep == 0 || isspace(*ep))) {
 
98
                                if (d > 500)
 
99
                                    sprintf(val2, "%.0f", d);
 
100
                                else
 
101
                                    sprintf(val2, "%.1f", d);
 
102
                                pval = val2;
 
103
                            }
 
104
                        }
 
105
                    }
 
106
                }
 
107
            }
 
108
        }
 
109
        fclose(fp);
 
110
    }
 
111
 
 
112
    /* store result */
 
113
    SetResult(&result, R_STRING, pval);
 
114
}
 
115
 
 
116
 
 
117
/* plugin initialization */
 
118
/* MUST NOT be declared 'static'! */
 
119
int plugin_init_w1retap(void)
 
120
{
 
121
 
 
122
    /* register all our cool functions */
 
123
    /* the second parameter is the number of arguments */
 
124
    /* -1 stands for variable argument list */
 
125
    AddFunction("w1retap::readkey", 2, my_readkey);
 
126
 
 
127
    return 0;
 
128
}
 
129
 
 
130
void plugin_exit_w1retap(void)
 
131
{
 
132
    /* free any allocated memory */
 
133
    /* close filedescriptors */
 
134
}
 
135
 
 
136
 
 
137
/*
 
138
 
 
139
#Sample configuration for w1retap
 
140
#================================
 
141
 
 
142
Display Weather {
 
143
   Driver   'Pertelian'
 
144
   Port     '/dev/ttyUSB0'
 
145
   Size     '20x4'
 
146
   Backlight 1
 
147
   Icons     0
 
148
}
 
149
 
 
150
Display XWindow {
 
151
    Driver     'X11'
 
152
    Size       '120x32'
 
153
    Font       '6x8'
 
154
    Pixel      '4+1'
 
155
    Gap        '-1x-1'
 
156
    Border      20
 
157
    Foreground '000000ff'
 
158
    Background '00000033'
 
159
    Basecolor '70c000'
 
160
    Buttons   6
 
161
    Brightness 133
 
162
}
 
163
 
 
164
Widget LCDTimer {
 
165
   class 'Timer'
 
166
   active 1
 
167
   update 10000
 
168
   expression h=strftime('%H', time()); x=file::readline('/tmp/.lcd',1)+0; b=(h > 5 | h < 23) + x ; LCD::backlight(b)
 
169
}
 
170
 
 
171
 
 
172
Widget Temp {
 
173
    class 'Text'
 
174
    expression w1retap::readkey(file, 'OTMP0')
 
175
    prefix 'Extr '
 
176
    width 10
 
177
    align 'L'
 
178
    update tick
 
179
}
 
180
 
 
181
Widget GHouse {
 
182
    class 'Text'
 
183
    expression w1retap::readkey(file, 'GHT')
 
184
    prefix 'GHT '
 
185
    width 10
 
186
    align 'L'
 
187
    update tick
 
188
}
 
189
 
 
190
Widget House {
 
191
    class 'Text'
 
192
    expression w1retap::readkey(file, 'OTMP1')
 
193
    prefix 'Intr '
 
194
    width 10
 
195
    align 'L'
 
196
    update tick
 
197
}
 
198
Widget Garage {
 
199
    class 'Text'
 
200
    expression w1retap::readkey(file, 'OTMP2')
 
201
    prefix 'Gge '
 
202
    width 10
 
203
    align 'L'
 
204
    update tick
 
205
}
 
206
Widget Soil {
 
207
    class 'Text'
 
208
    expression w1retap::readkey(file, 'STMP1')
 
209
    prefix 'Soil '
 
210
    width 10
 
211
    align 'L'
 
212
    update tick
 
213
}
 
214
Widget Press {
 
215
    class 'Text'
 
216
    expression w1retap::readkey(file, 'OPRS')
 
217
    prefix 'Pres '
 
218
    width 10
 
219
    align 'L'
 
220
    update tick
 
221
}
 
222
 
 
223
Widget Rain {
 
224
    class 'Text'
 
225
    expression w1retap::readkey(file1, 'RAIN')
 
226
    prefix 'Rain '
 
227
    width 20
 
228
    align 'L'
 
229
    update tick
 
230
}
 
231
 
 
232
Layout Default {
 
233
    Timer1 'LCDTimer'
 
234
    Row1 {
 
235
        Col1 'Temp'
 
236
        Col11 'GHouse'
 
237
    }    
 
238
 
 
239
    Row2 {
 
240
        Col1 'House'
 
241
        Col11 'Garage'
 
242
    }    
 
243
 
 
244
    Row3 {
 
245
        Col1 'Soil'
 
246
        Col11 'Press'
 
247
    }    
 
248
 
 
249
    Row4 {
 
250
        Col1 'Rain'
 
251
    }
 
252
}
 
253
 
 
254
Variables {
 
255
   tick 120000
 
256
   file '/tmp/.w1retap.dat'
 
257
   file1 '/tmp/.w1rain.txt'
 
258
}
 
259
 
 
260
Layout 'Default'
 
261
 
 
262
Display 'Weather'
 
263
 
 
264
*/