1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
|
/*
* COPYRIGHT
*
* pcb-rnd, interactive printed circuit board design
* Copyright (C) 2019 Tibor 'Igor2' Palinkas
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Contact:
* Project page: http://repo.hu/projects/pcb-rnd
* lead developer: http://repo.hu/projects/pcb-rnd/contact.html
* mailing list: pcb-rnd (at) list.repo.hu (send "subscribe")
*/
#include "config.h"
#include <librnd/core/hid_dad.h>
#include "event.h"
#define MAX_EXC 16
#define FREQ_MAX ((double)(100.0*1000.0*1000.0*1000.0))
#define AEPREFIX "openems::excitation::"
typedef struct {
int w[8];
} exc_data_t;
typedef struct {
RND_DAD_DECL_NOINIT(dlg)
int active; /* already open - allow only one instance */
int wselector, wtab;
int selected;
exc_data_t exc_data[MAX_EXC];
} exc_ctx_t;
exc_ctx_t exc_ctx;
typedef struct {
const char *name;
void (*dad)(int idx);
char *(*get)(int idx, int fmt_matlab);
void (*ser)(int idx, int save); /* serialization: if save is 1, set attributes, else load attributes */
int type_id;
} exc_t;
#define MAX_EXC_TYPES 5
static const exc_t excitations[MAX_EXC_TYPES];
static void ser_save(const char *data, const char *attrkey)
{
const char *orig = pcb_attribute_get(&PCB->Attributes, attrkey);
if ((orig == NULL) || (strcmp(orig, data) != 0)) {
pcb_attribute_put(&PCB->Attributes, attrkey, data);
pcb_board_set_changed_flag(PCB, rnd_true);
}
}
static const char *ser_load(const char *attrkey)
{
return pcb_attribute_get(&PCB->Attributes, attrkey);
}
#if 0
/* unused at the moment */
static void ser_int(int save, int widx, const char *attrkey)
{
if (save) {
char tmp[128];
sprintf(tmp, "%d", exc_ctx.dlg[widx].val.lng);
ser_save(tmp, attrkey);
}
else {
rnd_hid_attr_val_t hv;
char *end;
const char *orig = ser_load(attrkey);
if (orig != NULL) {
hv.lng = strtol(orig, &end, 10);
if (*end != '\0') {
rnd_message(RND_MSG_ERROR, "Invalid integer value in board attribute '%s': '%s'\n", attrkey, orig);
hv.lng = 0;
}
}
else
hv.lng = 0;
rnd_gui->attr_dlg_set_value(exc_ctx.dlg_hid_ctx, widx, &hv);
}
}
#endif
static void ser_hz(int save, int widx, const char *attrkey)
{
if (save) {
char tmp[128];
sprintf(tmp, "%f Hz", exc_ctx.dlg[widx].val.dbl);
ser_save(tmp, attrkey);
}
else {
rnd_hid_attr_val_t hv;
char *end;
const char *orig = ser_load(attrkey);
if (orig != NULL) {
hv.dbl = strtod(orig, &end);
if (*end != '\0') {
while(isspace(*end)) end++;
if (rnd_strcasecmp(end, "hz") != 0) {
rnd_message(RND_MSG_ERROR, "Invalid real value (Hz) in board attribute '%s': '%s'\n", attrkey, orig);
hv.dbl = 0;
}
}
}
else
hv.dbl = 0;
rnd_gui->attr_dlg_set_value(exc_ctx.dlg_hid_ctx, widx, &hv);
}
}
static void ser_str(int save, int widx, const char *attrkey)
{
if (save) {
ser_save(exc_ctx.dlg[widx].val.str, attrkey);
}
else {
rnd_hid_attr_val_t hv;
hv.str = ser_load(attrkey);
if (hv.str == NULL)
hv.str = "";
rnd_gui->attr_dlg_set_value(exc_ctx.dlg_hid_ctx, widx, &hv);
}
}
static void exc_val_chg_cb(void *hid_ctx, void *caller_data, rnd_hid_attribute_t *attr);
static rnd_bool to_hz(const char *s, double *out)
{
const rnd_unit_t *u;
double d;
if (!rnd_get_value_unit(s, NULL, 0, &d, &u))
return rnd_false;
if (u->family != RND_UNIT_FREQ)
return rnd_false;
*out = d;
return rnd_true;
}
/*** excitation "micro-plugins" ***/
#define I_FC 0
#define I_F0 1
/** gaussian **/
static void exc_gaus_dad(int idx)
{
RND_DAD_BEGIN_TABLE(exc_ctx.dlg, 2);
RND_DAD_LABEL(exc_ctx.dlg, "fc");
RND_DAD_SPIN_FREQ(exc_ctx.dlg);
RND_DAD_MINMAX(exc_ctx.dlg, 0, FREQ_MAX);
RND_DAD_HELP(exc_ctx.dlg, "20db Cutoff Frequency\nbandwidth is 2*fc");
RND_DAD_CHANGE_CB(exc_ctx.dlg, exc_val_chg_cb);
exc_ctx.exc_data[idx].w[I_FC] = RND_DAD_CURRENT(exc_ctx.dlg);
RND_DAD_LABEL(exc_ctx.dlg, "f0");
RND_DAD_REAL(exc_ctx.dlg);
RND_DAD_MINMAX(exc_ctx.dlg, 0, FREQ_MAX);
RND_DAD_HELP(exc_ctx.dlg, "Center Frequency [Hz]");
RND_DAD_CHANGE_CB(exc_ctx.dlg, exc_val_chg_cb);
exc_ctx.exc_data[idx].w[I_F0] = RND_DAD_CURRENT(exc_ctx.dlg);
RND_DAD_END(exc_ctx.dlg);
}
static char *exc_gaus_get(int idx, int fmt_matlab)
{
double f0 = 0, fc = 0;
if (!to_hz(pcb_attribute_get(&PCB->Attributes, AEPREFIX "gaussian::f0"), &f0))
rnd_message(RND_MSG_ERROR, "Gauss excitation: unable to parse frequency gaussian::f0\n");
if (!to_hz(pcb_attribute_get(&PCB->Attributes, AEPREFIX "gaussian::fc"), &fc))
rnd_message(RND_MSG_ERROR, "Gauss excitation: unable to parse frequency gaussian::fc\n");
if (fmt_matlab)
return rnd_strdup_printf("FDTD = SetGaussExcite(FDTD, %f, %f);", fc, f0);
return rnd_strdup_printf("Type='%d' f0='%f' fc='%f'", excitations[idx].type_id, fc, f0);
}
static void exc_gaus_ser(int idx, int save)
{
ser_hz(save, exc_ctx.exc_data[idx].w[I_F0], AEPREFIX "gaussian::f0");
ser_hz(save, exc_ctx.exc_data[idx].w[I_FC], AEPREFIX "gaussian::fc");
}
#undef I_FC
#undef I_F0
/** sinusoidal **/
#define I_F0 0
static void exc_sin_dad(int idx)
{
RND_DAD_BEGIN_TABLE(exc_ctx.dlg, 2);
RND_DAD_LABEL(exc_ctx.dlg, "f0");
RND_DAD_REAL(exc_ctx.dlg);
RND_DAD_MINMAX(exc_ctx.dlg, 0, FREQ_MAX);
RND_DAD_HELP(exc_ctx.dlg, "Center Frequency [Hz]");
RND_DAD_CHANGE_CB(exc_ctx.dlg, exc_val_chg_cb);
exc_ctx.exc_data[idx].w[I_F0] = RND_DAD_CURRENT(exc_ctx.dlg);
RND_DAD_END(exc_ctx.dlg);
}
static char *exc_sin_get(int idx, int fmt_matlab)
{
double f0;
if (!to_hz(pcb_attribute_get(&PCB->Attributes, AEPREFIX "sinusoidal::f0"), &f0))
rnd_message(RND_MSG_ERROR, "Sinus excitation: unable to parse frequency sinusoidal::f0\n");
if (fmt_matlab)
return rnd_strdup_printf("FDTD = SetSinusExcite(FDTD, %f);", f0);
return rnd_strdup_printf("Type='%d' f0='%f'", excitations[idx].type_id, f0);
}
static void exc_sin_ser(int idx, int save)
{
ser_hz(save, exc_ctx.exc_data[idx].w[I_F0], AEPREFIX "sinusoidal::f0");
}
#undef I_F0
/** custom **/
#define I_F0 0
#define I_FUNC 1
static void exc_cust_dad(int idx)
{
RND_DAD_BEGIN_TABLE(exc_ctx.dlg, 2);
RND_DAD_LABEL(exc_ctx.dlg, "f0");
RND_DAD_REAL(exc_ctx.dlg);
RND_DAD_MINMAX(exc_ctx.dlg, 0, FREQ_MAX);
RND_DAD_HELP(exc_ctx.dlg, "Nyquest Rate [Hz]");
RND_DAD_CHANGE_CB(exc_ctx.dlg, exc_val_chg_cb);
exc_ctx.exc_data[idx].w[I_F0] = RND_DAD_CURRENT(exc_ctx.dlg);
RND_DAD_LABEL(exc_ctx.dlg, "function");
RND_DAD_STRING(exc_ctx.dlg);
RND_DAD_HELP(exc_ctx.dlg, "Custom function");
RND_DAD_CHANGE_CB(exc_ctx.dlg, exc_val_chg_cb);
exc_ctx.exc_data[idx].w[I_FUNC] = RND_DAD_CURRENT(exc_ctx.dlg);
RND_DAD_END(exc_ctx.dlg);
}
static char *exc_cust_get(int idx, int fmt_matlab)
{
double f0;
if (!to_hz(pcb_attribute_get(&PCB->Attributes, AEPREFIX "custom::f0"), &f0))
rnd_message(RND_MSG_ERROR, "Custom excitation: unable to parse frequency custom::f0\n");
if (fmt_matlab)
return rnd_strdup_printf(
"FDTD = SetCustomExcite(FDTD, %f, %s)",
f0,
pcb_attribute_get(&PCB->Attributes, AEPREFIX "custom::func")
);
return rnd_strdup_printf("Type='%d' f0='%f' Function='%s'",
excitations[idx].type_id,
f0,
pcb_attribute_get(&PCB->Attributes, AEPREFIX "custom::func")
);
}
static void exc_cust_ser(int idx, int save)
{
ser_hz(save, exc_ctx.exc_data[idx].w[I_F0], AEPREFIX "custom::f0");
ser_str(save, exc_ctx.exc_data[idx].w[I_FUNC], AEPREFIX "custom::func");
}
#undef I_F0
#undef I_FUNC
/** user-specified **/
#define I_SCRIPT 0
static void exc_user_dad(int idx)
{
RND_DAD_BEGIN_VBOX(exc_ctx.dlg);
RND_DAD_COMPFLAG(exc_ctx.dlg, RND_HATF_EXPFILL);
RND_DAD_LABEL(exc_ctx.dlg, "Specify the excitation setup script:");
RND_DAD_TEXT(exc_ctx.dlg, NULL);
RND_DAD_COMPFLAG(exc_ctx.dlg, RND_HATF_EXPFILL | RND_HATF_SCROLL);
RND_DAD_CHANGE_CB(exc_ctx.dlg, exc_val_chg_cb);
exc_ctx.exc_data[idx].w[I_SCRIPT] = RND_DAD_CURRENT(exc_ctx.dlg);
RND_DAD_END(exc_ctx.dlg);
}
static char *exc_user_get(int idx, int fmt_matlab)
{
if (fmt_matlab)
return rnd_strdup(pcb_attribute_get(&PCB->Attributes, AEPREFIX "user-defined::script"));
return NULL;
}
static void exc_user_ser(int idx, int save)
{
int wscript;
rnd_hid_attribute_t *attr;
rnd_hid_text_t *txt;
wscript = exc_ctx.exc_data[idx].w[I_SCRIPT];
attr = &exc_ctx.dlg[wscript];
txt = attr->wdata;
ser_save(txt->hid_get_text(attr, exc_ctx.dlg_hid_ctx), AEPREFIX "user-defined::script");
}
#undef I_SCRIPT
/*** generic code ***/
static const exc_t excitations[MAX_EXC_TYPES] = {
{ "gaussian", exc_gaus_dad, exc_gaus_get, exc_gaus_ser, 0 },
{ "sinusoidal", exc_sin_dad, exc_sin_get, exc_sin_ser, 1 },
{ "custom", exc_cust_dad, exc_cust_get, exc_cust_ser, 10 },
{ "user-defined", exc_user_dad, exc_user_get, exc_user_ser, -1 },
/* pending IDs: dirac=2, step=3 */
{ NULL, NULL}
};
static void exc_close_cb(void *caller_data, rnd_hid_attr_ev_t ev)
{
exc_ctx_t *ctx = caller_data;
RND_DAD_FREE(ctx->dlg);
memset(ctx, 0, sizeof(exc_ctx_t)); /* reset all states to the initial - includes ctx->active = 0; */
}
static void exc_load_all(void)
{
const exc_t *e;
int n;
for(n = 0, e = excitations; e->name != NULL; n++,e++)
e->ser(n, 0);
}
static int load_selector(void)
{
const char *type = pcb_attribute_get(&PCB->Attributes, AEPREFIX "type");
const exc_t *e;
int n;
if (type == NULL) {
exc_ctx.selected = 0;
return 0;
}
for(n = 0, e = excitations; e->name != NULL; n++,e++) {
if (strcmp(e->name, type) == 0) {
exc_ctx.selected = n;
return 0;
}
}
return -1;
}
static void select_update(int setattr)
{
rnd_hid_attr_val_t hv;
hv.lng = exc_ctx.selected;
if ((exc_ctx.selected < 0) || (exc_ctx.selected >= sizeof(excitations)/sizeof(excitations[0]))) {
rnd_message(RND_MSG_ERROR, "Invalid excitation selected\n");
exc_ctx.selected = 0;
}
rnd_gui->attr_dlg_set_value(exc_ctx.dlg_hid_ctx, exc_ctx.wtab, &hv);
rnd_gui->attr_dlg_set_value(exc_ctx.dlg_hid_ctx, exc_ctx.wselector, &hv);
if (setattr) {
const char *orig = pcb_attribute_get(&PCB->Attributes, "openems::excitation::type");
if ((orig == NULL) || (strcmp(orig, excitations[exc_ctx.selected].name) != 0)) {
pcb_attribute_put(&PCB->Attributes, "openems::excitation::type", excitations[exc_ctx.selected].name);
pcb_board_set_changed_flag(PCB, rnd_true);
}
}
}
static void select_cb(void *hid_ctx, void *caller_data, rnd_hid_attribute_t *attr)
{
exc_ctx.selected = attr->val.lng;
select_update(1);
}
static void exc_val_chg_cb(void *hid_ctx, void *caller_data, rnd_hid_attribute_t *attr)
{
excitations[exc_ctx.selected].ser(exc_ctx.selected, 1);
}
static void pcb_dlg_exc(void)
{
static const char *excnames[MAX_EXC+1];
const exc_t *e;
int n;
rnd_hid_dad_buttons_t clbtn[] = {{"Close", 0}, {NULL, 0}};
if (exc_ctx.active)
return; /* do not open another */
if (excnames[0] == NULL) {
for(n = 0, e = excitations; e->name != NULL; n++,e++) {
if (n >= MAX_EXC) {
rnd_message(RND_MSG_ERROR, "internal error: too many excitations");
break;
}
excnames[n] = e->name;
}
excnames[n] = NULL;
}
RND_DAD_BEGIN_VBOX(exc_ctx.dlg);
RND_DAD_COMPFLAG(exc_ctx.dlg, RND_HATF_EXPFILL);
RND_DAD_BEGIN_HBOX(exc_ctx.dlg);
RND_DAD_LABEL(exc_ctx.dlg, "Excitation type:");
RND_DAD_ENUM(exc_ctx.dlg, excnames);
exc_ctx.wselector = RND_DAD_CURRENT(exc_ctx.dlg);
RND_DAD_CHANGE_CB(exc_ctx.dlg, select_cb);
RND_DAD_END(exc_ctx.dlg);
RND_DAD_BEGIN_TABBED(exc_ctx.dlg, excnames);
RND_DAD_COMPFLAG(exc_ctx.dlg, RND_HATF_EXPFILL | RND_HATF_HIDE_TABLAB);
exc_ctx.wtab = RND_DAD_CURRENT(exc_ctx.dlg);
for(n = 0, e = excitations; e->name != NULL; n++,e++) {
if (e->dad != NULL)
e->dad(n);
else
RND_DAD_LABEL(exc_ctx.dlg, "Not yet available.");
}
RND_DAD_END(exc_ctx.dlg);
RND_DAD_BUTTON_CLOSES(exc_ctx.dlg, clbtn);
RND_DAD_END(exc_ctx.dlg);
/* set up the context */
exc_ctx.active = 1;
RND_DAD_NEW("openems_excitation", exc_ctx.dlg, "openems: excitation", &exc_ctx, rnd_false, exc_close_cb);
load_selector();
select_update(1);
exc_load_all();
}
static const char pcb_acts_OpenemsExcitation[] =
"OpenemsExcitation([interactive])\n"
"OpenemsExcitation(select, excitationname)\n"
"OpenemsExcitation(set, [excitationnme], paramname, paramval)\n"
"OpenemsExcitation(get, [excitationnme], paramname)\n"
;
static const char pcb_acth_OpenemsExcitation[] = "Select which openEMS excitation method should be exported and manipulate the associated parameters. When invoked without arguments a dialog box with the same functionality is presented.";
/* DOC: openemsexcication.html */
static fgw_error_t pcb_act_OpenemsExcitation(fgw_arg_t *res, int argc, fgw_arg_t *argv)
{
const char *op = "interactive", *a1 = NULL;
RND_ACT_MAY_CONVARG(1, FGW_STR, OpenemsExcitation, op = argv[1].val.str);
RND_ACT_MAY_CONVARG(2, FGW_STR, OpenemsExcitation, a1 = argv[2].val.str);
RND_ACT_IRES(0);
if (strcmp(op, "interactive") == 0)
pcb_dlg_exc();
else if (strcmp(op, "select") == 0) {
if (a1 == NULL) {
rnd_message(RND_MSG_ERROR, "OpenemsExcitation(select) needs a excitation name");
goto error;
}
pcb_attribute_put(&PCB->Attributes, AEPREFIX "type", a1);
load_selector();
select_update(1);
}
else if (strcmp(op, "set") == 0) {
int start;
const char *key, *val;
char *attrkey;
switch(argc) {
case 4: a1 = excitations[exc_ctx.selected].name; start = 2; break;
case 5: start = 3; break;
default:
rnd_message(RND_MSG_ERROR, "OpenemsExcitation(set) needs exactly 2 or 3 more arguments");
goto error;
}
RND_ACT_CONVARG(start+0, FGW_STR, OpenemsExcitation, key = argv[start+0].val.str);
RND_ACT_CONVARG(start+1, FGW_STR, OpenemsExcitation, val = argv[start+1].val.str);
attrkey = rnd_strdup_printf(AEPREFIX "%s::%s", a1, key);
pcb_attribute_put(&PCB->Attributes, attrkey, val);
free(attrkey);
exc_load_all();
}
else if (strcmp(op, "get") == 0) {
int start;
const char *key;
char *attrkey;
switch(argc) {
case 3: a1 = excitations[exc_ctx.selected].name; start = 2; break;
case 4: start = 3; break;
default:
rnd_message(RND_MSG_ERROR, "OpenemsExcitation(get) needs exactly 1 or 2 more arguments");
goto error;
}
RND_ACT_CONVARG(start+0, FGW_STR, OpenemsExcitation, key = argv[start+0].val.str);
attrkey = rnd_strdup_printf(AEPREFIX "%s::%s", a1, key);
res->type = FGW_STR;
res->val.cstr = pcb_attribute_get(&PCB->Attributes, attrkey);
free(attrkey);
}
return 0;
error:;
RND_ACT_IRES(1);
return 0;
}
static char *pcb_openems_excitation_get(pcb_board_t *pcb, int fmt_matlab)
{
if ((exc_ctx.selected < 0) || (exc_ctx.selected >= sizeof(excitations)/sizeof(excitations[0]))) {
rnd_message(RND_MSG_ERROR, "No excitation selected\n");
if (fmt_matlab)
return rnd_strdup("%% ERROR: no excitation selected\n");
else
return NULL;
}
return excitations[exc_ctx.selected].get(exc_ctx.selected, fmt_matlab);
}
static void exc_ev_board_changed(rnd_hidlib_t *hidlib, void *user_data, int argc, rnd_event_arg_t argv[])
{
load_selector();
if (exc_ctx.active)
exc_load_all();
}
static void pcb_openems_excitation_init(void)
{
rnd_event_bind(RND_EVENT_BOARD_CHANGED, exc_ev_board_changed, NULL, openems_cookie);
}
static void pcb_openems_excitation_uninit(void)
{
rnd_event_unbind_allcookie(openems_cookie);
}
|