| 1 | /* $Id$ |
|---|
| 2 | * $URL$ |
|---|
| 3 | * |
|---|
| 4 | * generic driver helper for GPO's |
|---|
| 5 | * |
|---|
| 6 | * Copyright (C) 2005 Michael Reinelt <michael@reinelt.co.at> |
|---|
| 7 | * Copyright (C) 2005 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net> |
|---|
| 8 | * |
|---|
| 9 | * This file is part of LCD4Linux. |
|---|
| 10 | * |
|---|
| 11 | * LCD4Linux is free software; you can redistribute it and/or modify |
|---|
| 12 | * it under the terms of the GNU General Public License as published by |
|---|
| 13 | * the Free Software Foundation; either version 2, or (at your option) |
|---|
| 14 | * any later version. |
|---|
| 15 | * |
|---|
| 16 | * LCD4Linux is distributed in the hope that it will be useful, |
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 19 | * GNU General Public License for more details. |
|---|
| 20 | * |
|---|
| 21 | * You should have received a copy of the GNU General Public License |
|---|
| 22 | * along with this program; if not, write to the Free Software |
|---|
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 24 | * |
|---|
| 25 | */ |
|---|
| 26 | |
|---|
| 27 | /* |
|---|
| 28 | * |
|---|
| 29 | * exported variables: |
|---|
| 30 | * |
|---|
| 31 | * extern int GPIS, GPOS; number of Inputs and Outputs |
|---|
| 32 | * |
|---|
| 33 | * |
|---|
| 34 | * these functions must be implemented by the real driver: |
|---|
| 35 | * |
|---|
| 36 | * int (*drv_generic_gpio_real_set) (const int num, const int val); |
|---|
| 37 | * sets GPO num to val, returns the actal value |
|---|
| 38 | * |
|---|
| 39 | * int (*drv_generic_gpio_real_get) (const int num); |
|---|
| 40 | * reads GPI num |
|---|
| 41 | * |
|---|
| 42 | * |
|---|
| 43 | * exported fuctions: |
|---|
| 44 | * |
|---|
| 45 | * int drv_generic_gpio_init(const char *section, const char *driver) |
|---|
| 46 | * initializes the generic GPIO driver |
|---|
| 47 | * |
|---|
| 48 | * int drv_generic_gpio_clear(void); |
|---|
| 49 | * resets all GPO's |
|---|
| 50 | * |
|---|
| 51 | * int drv_generic_gpio_get (const int num) |
|---|
| 52 | * returns value og GPI #num |
|---|
| 53 | * |
|---|
| 54 | * int drv_generic_gpio_draw(WIDGET * W) |
|---|
| 55 | * 'draws' GPO widget |
|---|
| 56 | * calls drv_generic_gpio_real_set() |
|---|
| 57 | * |
|---|
| 58 | * int drv_generic_gpio_quit(void) |
|---|
| 59 | * closes the generic GPIO driver |
|---|
| 60 | * |
|---|
| 61 | */ |
|---|
| 62 | |
|---|
| 63 | #include "config.h" |
|---|
| 64 | |
|---|
| 65 | #include <stdio.h> |
|---|
| 66 | |
|---|
| 67 | #include "debug.h" |
|---|
| 68 | #include "plugin.h" |
|---|
| 69 | #include "widget.h" |
|---|
| 70 | #include "widget_gpo.h" |
|---|
| 71 | |
|---|
| 72 | #include "drv_generic_gpio.h" |
|---|
| 73 | |
|---|
| 74 | #ifdef WITH_DMALLOC |
|---|
| 75 | #include <dmalloc.h> |
|---|
| 76 | #endif |
|---|
| 77 | |
|---|
| 78 | #define MAX_GPIS 32 |
|---|
| 79 | #define MAX_GPOS 32 |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | static char *Section = NULL; |
|---|
| 83 | static char *Driver = NULL; |
|---|
| 84 | |
|---|
| 85 | static int GPI[MAX_GPIS]; |
|---|
| 86 | static int GPO[MAX_GPOS]; |
|---|
| 87 | |
|---|
| 88 | int GPOS = 0; |
|---|
| 89 | int GPIS = 0; |
|---|
| 90 | |
|---|
| 91 | int (*drv_generic_gpio_real_set) () = NULL; |
|---|
| 92 | int (*drv_generic_gpio_real_get) () = NULL; |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | static void drv_generic_gpio_plugin_gpi(RESULT * result, RESULT * arg1) |
|---|
| 96 | { |
|---|
| 97 | int num; |
|---|
| 98 | double val; |
|---|
| 99 | |
|---|
| 100 | num = R2N(arg1); |
|---|
| 101 | |
|---|
| 102 | if (num <= 0 || num > GPIS) { |
|---|
| 103 | error("%s::GPI(%d): GPI out of range (1..%d)", Driver, num, GPIS); |
|---|
| 104 | SetResult(&result, R_STRING, ""); |
|---|
| 105 | return; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | val = drv_generic_gpio_get(num - 1); |
|---|
| 109 | SetResult(&result, R_NUMBER, &val); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | static void drv_generic_gpio_plugin_gpo(RESULT * result, const int argc, RESULT * argv[]) |
|---|
| 114 | { |
|---|
| 115 | int num, val; |
|---|
| 116 | double gpo; |
|---|
| 117 | |
|---|
| 118 | switch (argc) { |
|---|
| 119 | case 1: |
|---|
| 120 | num = R2N(argv[0]); |
|---|
| 121 | if (num <= 0 || num > GPOS) { |
|---|
| 122 | error("%s::GPO(%d): GPO out of range (1..%d)", Driver, num, GPOS); |
|---|
| 123 | SetResult(&result, R_STRING, ""); |
|---|
| 124 | return; |
|---|
| 125 | } |
|---|
| 126 | gpo = GPO[num - 1]; |
|---|
| 127 | SetResult(&result, R_NUMBER, &gpo); |
|---|
| 128 | break; |
|---|
| 129 | case 2: |
|---|
| 130 | num = R2N(argv[0]); |
|---|
| 131 | val = R2N(argv[1]); |
|---|
| 132 | if (num <= 0 || num > GPOS) { |
|---|
| 133 | error("%s::GPO(%d): GPO out of range (1..%d)", Driver, num, GPOS); |
|---|
| 134 | SetResult(&result, R_STRING, ""); |
|---|
| 135 | return; |
|---|
| 136 | } |
|---|
| 137 | if (GPO[num - 1] != val) { |
|---|
| 138 | if (drv_generic_gpio_real_set) |
|---|
| 139 | GPO[num - 1] = drv_generic_gpio_real_set(num - 1, val); |
|---|
| 140 | } |
|---|
| 141 | gpo = GPO[num - 1]; |
|---|
| 142 | SetResult(&result, R_NUMBER, &gpo); |
|---|
| 143 | break; |
|---|
| 144 | default: |
|---|
| 145 | error("%s::GPO(): wrong number of parameters", Driver); |
|---|
| 146 | SetResult(&result, R_STRING, ""); |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | int drv_generic_gpio_init(const char *section, const char *driver) |
|---|
| 152 | { |
|---|
| 153 | WIDGET_CLASS wc; |
|---|
| 154 | |
|---|
| 155 | Section = (char *) section; |
|---|
| 156 | Driver = (char *) driver; |
|---|
| 157 | |
|---|
| 158 | info("%s: using %d GPI's and %d GPO's", Driver, GPIS, GPOS); |
|---|
| 159 | |
|---|
| 160 | /* reset all GPO's */ |
|---|
| 161 | drv_generic_gpio_clear(); |
|---|
| 162 | |
|---|
| 163 | /* register gpo widget */ |
|---|
| 164 | wc = Widget_GPO; |
|---|
| 165 | wc.draw = drv_generic_gpio_draw; |
|---|
| 166 | widget_register(&wc); |
|---|
| 167 | |
|---|
| 168 | /* register plugins */ |
|---|
| 169 | AddFunction("LCD::GPI", 1, drv_generic_gpio_plugin_gpi); |
|---|
| 170 | AddFunction("LCD::GPO", -1, drv_generic_gpio_plugin_gpo); |
|---|
| 171 | |
|---|
| 172 | return 0; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | int drv_generic_gpio_clear(void) |
|---|
| 177 | { |
|---|
| 178 | int i; |
|---|
| 179 | |
|---|
| 180 | /* clear GPI buffer */ |
|---|
| 181 | for (i = 0; i < MAX_GPIS; i++) { |
|---|
| 182 | GPI[i] = 0; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | /* clear GPO buffer */ |
|---|
| 186 | for (i = 0; i < MAX_GPOS; i++) { |
|---|
| 187 | GPO[i] = 0; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | /* really clear GPO's */ |
|---|
| 191 | for (i = 0; i < GPOS; i++) { |
|---|
| 192 | if (drv_generic_gpio_real_set) |
|---|
| 193 | GPO[i] = drv_generic_gpio_real_set(i, 0); |
|---|
| 194 | |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | return 0; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | int drv_generic_gpio_get(const int num) |
|---|
| 202 | { |
|---|
| 203 | int val = 0; |
|---|
| 204 | |
|---|
| 205 | if (num < 0 || num >= GPIS) { |
|---|
| 206 | error("%s: gpio_get(%d): GPI out of range (0..%d)", Driver, num + 1, GPIS); |
|---|
| 207 | return -1; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | if (drv_generic_gpio_real_get) |
|---|
| 211 | val = drv_generic_gpio_real_get(num); |
|---|
| 212 | |
|---|
| 213 | GPI[num] = val; |
|---|
| 214 | |
|---|
| 215 | return val; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | int drv_generic_gpio_draw(WIDGET * W) |
|---|
| 220 | { |
|---|
| 221 | WIDGET_GPO *gpo = W->data; |
|---|
| 222 | int num, val; |
|---|
| 223 | |
|---|
| 224 | num = W->row; |
|---|
| 225 | val = P2N(&gpo->expression); |
|---|
| 226 | |
|---|
| 227 | if (num < 0 || num >= GPOS) { |
|---|
| 228 | error("%s: gpio_draw(%d): GPO out of range (0..%d)", Driver, num + 1, GPOS); |
|---|
| 229 | return -1; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | if (GPO[num] != val) { |
|---|
| 233 | if (drv_generic_gpio_real_set) |
|---|
| 234 | GPO[num] = drv_generic_gpio_real_set(num, val); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | return 0; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | int drv_generic_gpio_quit(void) |
|---|
| 242 | { |
|---|
| 243 | info("%s: shutting down GPIO driver.", Driver); |
|---|
| 244 | drv_generic_gpio_clear(); |
|---|
| 245 | return 0; |
|---|
| 246 | } |
|---|