|
@@ -0,0 +1,400 @@
|
|
|
|
|
+#include <stdio.h>
|
|
|
|
|
+#include <unistd.h>
|
|
|
|
|
+// ssize_t read(int fd, void *buf, size_t count); // from <unistd.h>
|
|
|
|
|
+#include <stdlib.h>
|
|
|
|
|
+// note: include the header <stdlib.h> or explicitly provide a declaration for 'abs'
|
|
|
|
|
+#include <math.h>
|
|
|
|
|
+// note: include the header <math.h> or explicitly provide a declaration for 'round'
|
|
|
|
|
+
|
|
|
|
|
+// Task Object - return int value based on attrs
|
|
|
|
|
+// - example: PWM task return VALUE to send using analogWrite(PIN, VALUE)
|
|
|
|
|
+// - obj has attrs - TODO: config for calculations like XPATH or in x3d
|
|
|
|
|
+// Task Object attrs - like xsd element attrs of type (int value, other object)
|
|
|
|
|
+
|
|
|
|
|
+// TODO: CALC_ enum?
|
|
|
|
|
+const int CALC_DEFINE = 0; // attr[i] = { value } // regVal[i] = value
|
|
|
|
|
+const int CALC_GET_DATA = 1; // attr[i] = { data_idx }; // regVal[i] = data[data_idx]
|
|
|
|
|
+const int CALC_SET_DATA = 2; // attr[i] = { data_idx, reg_idx } // data[data_idx] = regVal[reg_idx]
|
|
|
|
|
+const int CALC_COMP_EQ = 3; // attr[i] = { a, b, false_idx } // if (reg[a] == reg[b]) then go to + 1, else go to false_idx
|
|
|
|
|
+const int CALC_COMP_GT = 4; // attr[i] = { a, b, false_idx } // if (reg[a] > reg[b]) then go to + 1, else go to false_idx
|
|
|
|
|
+const int CALC_COMP_LT = 5; // attr[i] = { a, b, false_idx } // if (reg[a] < reg[b]) then go to + 1, else go to false_idx
|
|
|
|
|
+const int CALC_FUNC_ABS = 6; // attr[i] = { idx } // regVal[i] = abs(reg[idx])
|
|
|
|
|
+const int CALC_FUNC_DIV = 7; // attr[i] = { a, b } // regVal[i] = regVal[a] / regVal[b]
|
|
|
|
|
+const int CALC_FUNC_MULTI = 8; // attr[i] = { a, b } // regVal[i] = regVal[a] * regVal[b]
|
|
|
|
|
+const int CALC_FUNC_ADD = 9; // attr[i] = { a, b } // regVal[i] = regVal[a] + regVal[b]
|
|
|
|
|
+const int CALC_FUNC_SUB = 10; // attr[i] = { a, b } // regVal[i] = regVal[a] - regVal[b]
|
|
|
|
|
+const int CALC_FUNC_OPPOSITE = 11; // attr[i] = { a, b } // regVal[i] = -1 * regVal[a]
|
|
|
|
|
+const int CALC_FINISH = 12;
|
|
|
|
|
+
|
|
|
|
|
+const int DB_OBJ_MAX = 10; // Object list -> Conf -> Attr
|
|
|
|
|
+int DB_OBJ_IDX = 0;
|
|
|
|
|
+int DB_OBJ[DB_OBJ_MAX][4]; // idObj => { firstConf, lastConf, firstAttr, lastAttr }
|
|
|
|
|
+const int DB_CONF_MAX = 50; // Conf list
|
|
|
|
|
+int DB_CONF_IDX = 0;
|
|
|
|
|
+int DB_CONF[DB_CONF_MAX]; // idType
|
|
|
|
|
+const int DB_ATTR_MAX = 100; // Attr list
|
|
|
|
|
+int DB_ATTR_IDX = 0;
|
|
|
|
|
+int DB_ATTR[DB_ATTR_MAX][3]; // { relIdxConf, value, idObj }
|
|
|
|
|
+
|
|
|
|
|
+void calc__db_init() {
|
|
|
|
|
+ int i;
|
|
|
|
|
+ for (i = 0; i < DB_OBJ_MAX; i++) {
|
|
|
|
|
+ DB_OBJ[i][0] = 0;
|
|
|
|
|
+ DB_OBJ[i][1] = 0;
|
|
|
|
|
+ DB_OBJ[i][2] = 0;
|
|
|
|
|
+ DB_OBJ[i][3] = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (i = 0; i < DB_CONF_MAX; i++) {
|
|
|
|
|
+ DB_CONF[i] = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (i = 0; i < DB_ATTR_MAX; i++) {
|
|
|
|
|
+ DB_ATTR[i][0] = 0;
|
|
|
|
|
+ DB_ATTR[i][1] = 0;
|
|
|
|
|
+ DB_ATTR[i][2] = -1;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+int calc__db_create_obj() {
|
|
|
|
|
+ int idObj = DB_OBJ_IDX;
|
|
|
|
|
+ DB_OBJ[DB_OBJ_IDX][0] = DB_CONF_IDX;
|
|
|
|
|
+ DB_OBJ[DB_OBJ_IDX][1] = 0; // last conf idx
|
|
|
|
|
+ DB_OBJ[DB_OBJ_IDX][2] = DB_ATTR_IDX; // first conf idx
|
|
|
|
|
+ DB_OBJ[DB_OBJ_IDX][3] = 0; // last attr idx
|
|
|
|
|
+ DB_OBJ_IDX++;
|
|
|
|
|
+ return idObj;
|
|
|
|
|
+}
|
|
|
|
|
+void calc__db_attr_add(int idObj, int val) {
|
|
|
|
|
+ DB_ATTR[DB_ATTR_IDX][0] = DB_CONF_IDX - DB_OBJ[idObj][0];
|
|
|
|
|
+ DB_ATTR[DB_ATTR_IDX][1] = val;
|
|
|
|
|
+ DB_ATTR[DB_ATTR_IDX][2] = idObj;
|
|
|
|
|
+ DB_OBJ[idObj][3] = DB_ATTR_IDX; // last attr idx
|
|
|
|
|
+ DB_ATTR_IDX++;
|
|
|
|
|
+}
|
|
|
|
|
+void calc__db_conf_add0(int idObj, int idType) {
|
|
|
|
|
+ if (DB_CONF_IDX >= DB_CONF_MAX) {
|
|
|
|
|
+ printf("BUG:DB_CONF_MAX reached!");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DB_CONF[DB_CONF_IDX] = idType;
|
|
|
|
|
+
|
|
|
|
|
+ DB_OBJ[idObj][1] = DB_CONF_IDX;
|
|
|
|
|
+ DB_CONF_IDX++;
|
|
|
|
|
+}
|
|
|
|
|
+void calc__db_conf_add1(int idObj, int idType, int a) {
|
|
|
|
|
+ if (DB_CONF_IDX >= DB_CONF_MAX) {
|
|
|
|
|
+ printf("BUG:DB_CONF_MAX reached!");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DB_CONF[DB_CONF_IDX] = idType;
|
|
|
|
|
+ calc__db_attr_add(idObj, a);
|
|
|
|
|
+
|
|
|
|
|
+ DB_OBJ[idObj][1] = DB_CONF_IDX;
|
|
|
|
|
+ DB_CONF_IDX++;
|
|
|
|
|
+}
|
|
|
|
|
+void calc__db_conf_add2(int idObj, int idType, int a, int b) {
|
|
|
|
|
+ if (DB_CONF_IDX >= DB_CONF_MAX) {
|
|
|
|
|
+ printf("BUG:DB_CONF_MAX reached!");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DB_CONF[DB_CONF_IDX] = idType;
|
|
|
|
|
+ calc__db_attr_add(idObj, a);
|
|
|
|
|
+ calc__db_attr_add(idObj, b);
|
|
|
|
|
+
|
|
|
|
|
+ DB_OBJ[idObj][1] = DB_CONF_IDX;
|
|
|
|
|
+ DB_CONF_IDX++;
|
|
|
|
|
+}
|
|
|
|
|
+void calc__db_conf_add3(int idObj, int idType, int a, int b, int c) {
|
|
|
|
|
+ if (DB_CONF_IDX >= DB_CONF_MAX) {
|
|
|
|
|
+ printf("BUG:DB_CONF_MAX reached!");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DB_CONF[DB_CONF_IDX] = idType;
|
|
|
|
|
+ calc__db_attr_add(idObj, a);
|
|
|
|
|
+ calc__db_attr_add(idObj, b);
|
|
|
|
|
+ calc__db_attr_add(idObj, c);
|
|
|
|
|
+
|
|
|
|
|
+ DB_OBJ[idObj][1] = DB_CONF_IDX;
|
|
|
|
|
+ DB_CONF_IDX++;
|
|
|
|
|
+}
|
|
|
|
|
+void calc__db_conf_add4(int idObj, int idType, int a, int b, int c, int d) {
|
|
|
|
|
+ if (DB_CONF_IDX >= DB_CONF_MAX) {
|
|
|
|
|
+ printf("BUG:DB_CONF_MAX reached!");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DB_CONF[DB_CONF_IDX] = idType;
|
|
|
|
|
+ calc__db_attr_add(idObj, a);
|
|
|
|
|
+ calc__db_attr_add(idObj, b);
|
|
|
|
|
+ calc__db_attr_add(idObj, c);
|
|
|
|
|
+ calc__db_attr_add(idObj, d);
|
|
|
|
|
+
|
|
|
|
|
+ DB_OBJ[idObj][1] = DB_CONF_IDX;
|
|
|
|
|
+ DB_CONF_IDX++;
|
|
|
|
|
+}
|
|
|
|
|
+int calc__get_firstConf( int idObj ) { return DB_OBJ[idObj][0]; }
|
|
|
|
|
+int calc__get_lastConf( int idObj ) { return DB_OBJ[idObj][1]; }
|
|
|
|
|
+int calc__get_firstAttr( int idObj ) { return DB_OBJ[idObj][2]; }
|
|
|
|
|
+int calc__get_lastAttr( int idObj ) { return DB_OBJ[idObj][3]; }
|
|
|
|
|
+int calc__get_totalConf( int idObj ) { return DB_OBJ[idObj][1] - DB_OBJ[idObj][0] + 1; }
|
|
|
|
|
+int calc__get_totalAttr( int idObj ) { return DB_OBJ[idObj][3] - DB_OBJ[idObj][2] + 1; }
|
|
|
|
|
+int calc__get_conf( int idObj, int relIdx ) { return DB_CONF[ calc__get_firstConf(idObj) + relIdx ]; }
|
|
|
|
|
+
|
|
|
|
|
+int * calc__attr(int idObj, int relIdx, int subIdx) {
|
|
|
|
|
+ int si = 0;
|
|
|
|
|
+ for (int i = calc__get_firstAttr(idObj); i <= calc__get_lastAttr(idObj); i++) {
|
|
|
|
|
+ if (DB_ATTR[i][0] == relIdx) {
|
|
|
|
|
+ if (si == subIdx) {
|
|
|
|
|
+ return &DB_ATTR[i][1];
|
|
|
|
|
+ }
|
|
|
|
|
+ si++;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+}
|
|
|
|
|
+void calc__DBG_OBJ(int idObj) {
|
|
|
|
|
+ printf("DBG:obj[%d] = { %d, %d, %d, %d }\n", idObj, DB_OBJ[idObj][0], DB_OBJ[idObj][1], DB_OBJ[idObj][2], DB_OBJ[idObj][3]); // TODO: DBG
|
|
|
|
|
+ printf("DBG:conf(obj:%d,total:%d): { ", idObj, calc__get_totalConf(idObj)); for (int i = DB_OBJ[idObj][0]; i < DB_OBJ[idObj][1]; i++) { printf("%d:%d, ", i, DB_CONF[i]); }; printf("}\n");
|
|
|
|
|
+ printf("DBG:attr(obj:%d,total:%d): { ", idObj, calc__get_totalAttr(idObj));
|
|
|
|
|
+ for (int i = calc__get_firstAttr(idObj); i <= calc__get_lastAttr(idObj); i++) {
|
|
|
|
|
+ if (DB_ATTR[i][2] == idObj) {
|
|
|
|
|
+ // printf("%d:{%d,%d,%d}, ", i, DB_ATTR[i][0], DB_ATTR[i][1], DB_ATTR[i][2]);
|
|
|
|
|
+ printf("%d:{%d,%d}, ", i, DB_ATTR[i][0], DB_ATTR[i][1]);
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ printf("}\n");
|
|
|
|
|
+}
|
|
|
|
|
+void calc__run(int idObj, int regData[]) {
|
|
|
|
|
+ int i = 0;
|
|
|
|
|
+ int confTotal = calc__get_totalConf(idObj);
|
|
|
|
|
+ int attrTotal = calc__get_totalAttr(idObj);
|
|
|
|
|
+ printf("DBG:calc__run:confTotal = %d, attrTotal = %d\n", confTotal, attrTotal); // TODO: DBG
|
|
|
|
|
+ int regVal[confTotal];
|
|
|
|
|
+ for (i = 0; i < confTotal; i++) regVal[i] = 0;
|
|
|
|
|
+
|
|
|
|
|
+ // int test[3][2] = { { 1,4 }, { 2,5 }, { 2,8 } };
|
|
|
|
|
+ // for ( int ( *p )[2] = test ; p != test + 3; ++p ) {
|
|
|
|
|
+ // for ( int *q = *p; q != *p + 2; ++q ) printf( "%d ", *q );
|
|
|
|
|
+ // puts( "" );
|
|
|
|
|
+ // }
|
|
|
|
|
+
|
|
|
|
|
+ // int DB_ATTR[DB_ATTR_MAX][3]; // { idxConf, value, idObj }
|
|
|
|
|
+ int regAttr[1][2];
|
|
|
|
|
+
|
|
|
|
|
+ // DB_CONF[ DB_OBJ[idObj][0] + relIdx ];
|
|
|
|
|
+ int * p = NULL;
|
|
|
|
|
+ for (i = 0; i < confTotal; i++) {
|
|
|
|
|
+ if (calc__get_conf(idObj, i) == CALC_GET_DATA) {
|
|
|
|
|
+ p = calc__attr(idObj, i, 0);
|
|
|
|
|
+ if (p != NULL) {
|
|
|
|
|
+ regVal[i] = regData[*p];
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // BUG: cant find reg idx in attr
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ printf("DBG:calc__run:regVal: { "); for (int i = 0; i < confTotal; i++) { printf("%d:%d, ", i, regVal[i]); }; printf("}\n"); // TODO: DBG
|
|
|
|
|
+ printf("DBG:calc__run:regData: { "); for (int i = 0; i < 7; i++) { printf("%d:%d, ", i, regData[i]); }; printf("}\n"); // TODO: DBG
|
|
|
|
|
+
|
|
|
|
|
+ char finish = 0;
|
|
|
|
|
+ int regIdx = 0; // relative reg idx
|
|
|
|
|
+ int nextRegIdx = 0; // relative reg idx
|
|
|
|
|
+ // int lastAttr = calc__get_lastAttr(idObj); // TODO: security: (regIdx >= firstAttr && regIdx <= lastAttr)
|
|
|
|
|
+ int calc_loop_limit = 100; // TODO: ...
|
|
|
|
|
+ for (i = 0; i < calc_loop_limit && !finish; i++) {
|
|
|
|
|
+ // printf("DBG:loop:%d: Start\n", i);
|
|
|
|
|
+
|
|
|
|
|
+ // printf("DBG:loop:%d:reg(%d:[%d,%d,%d])\n", i, regIdx, reg[regIdx][0], reg[regIdx][1], reg[regIdx][2]);
|
|
|
|
|
+ int type = calc__get_conf(idObj, regIdx);
|
|
|
|
|
+ int * pa = calc__attr(idObj, regIdx, 0);
|
|
|
|
|
+ int * pb = calc__attr(idObj, regIdx, 1);
|
|
|
|
|
+ printf("DBG:calc__run:loop:%d:reg[%d]C(%d) => a(%d):%d, b(%d):%d\n", i, regIdx, type, (NULL == pa) ? -1 : *pa, (NULL == pa) ? -1 : regVal[*pa], (NULL == pb) ? -1 : *pb, (NULL == pb) ? -1 : regVal[*pb]);
|
|
|
|
|
+ // calc__DBG_OBJ(0); // TODO: DBG
|
|
|
|
|
+ int a = (pa != NULL) ? *pa : 0;
|
|
|
|
|
+ int b = (pb != NULL) ? *pb : 0;
|
|
|
|
|
+ // printf("DBG:calc__run:loop:%d:reg[%d] => a(%d):%d, b(%d):%d\n", i, regIdx, a, regVal[a], b, regVal[b]);
|
|
|
|
|
+ switch (type) {
|
|
|
|
|
+ case CALC_DEFINE: { // 0
|
|
|
|
|
+ regVal[regIdx] = *pa; // TODO: check NULL
|
|
|
|
|
+ nextRegIdx = regIdx + 1;
|
|
|
|
|
+ } break;
|
|
|
|
|
+ case CALC_GET_DATA: { // 1
|
|
|
|
|
+ regVal[regIdx] = regData[*pa]; // TODO: read from data register
|
|
|
|
|
+ nextRegIdx = regIdx + 1;
|
|
|
|
|
+ } break;
|
|
|
|
|
+ case CALC_SET_DATA: { // 2
|
|
|
|
|
+ regVal[regIdx] = regVal[*pb];
|
|
|
|
|
+ printf("DBG:calc__run:loop:stop:%d:reg[%d] => %d from %d | data[%d] = %d\n", i, regIdx, regVal[regIdx], *pb, *pa, regVal[regIdx]);
|
|
|
|
|
+ regData[*pa] = regVal[regIdx];
|
|
|
|
|
+ // regAttr[0][1] = regVal[regIdx]; // TODO: set this too ???
|
|
|
|
|
+ nextRegIdx = regIdx + 1;
|
|
|
|
|
+ } break;
|
|
|
|
|
+ case CALC_COMP_EQ: { // 3
|
|
|
|
|
+ regVal[regIdx] = (regVal[*pa] == regVal[*pb]) ? 1 : 0;
|
|
|
|
|
+ nextRegIdx = (regVal[regIdx]) ? regIdx + 1 : *calc__attr(idObj, regIdx, 2);
|
|
|
|
|
+ } break;
|
|
|
|
|
+ case CALC_COMP_GT: { // 4
|
|
|
|
|
+ regVal[regIdx] = (regVal[*pa] > regVal[*pb]) ? 1 : 0;
|
|
|
|
|
+ nextRegIdx = (regVal[regIdx]) ? regIdx + 1 : *calc__attr(idObj, regIdx, 2);
|
|
|
|
|
+ } break;
|
|
|
|
|
+ case CALC_COMP_LT: { // 5
|
|
|
|
|
+ regVal[regIdx] = (regVal[*pa] < regVal[*pb]) ? 1 : 0;
|
|
|
|
|
+ nextRegIdx = (regVal[regIdx]) ? regIdx + 1 : *calc__attr(idObj, regIdx, 2);
|
|
|
|
|
+ } break;
|
|
|
|
|
+ case CALC_FUNC_ABS: { // 6
|
|
|
|
|
+ printf("TODO:calc__run:Not Implemented:CALC_FUNC_ABS"); // abs(reg[a]);
|
|
|
|
|
+ regVal[regIdx] = 0;
|
|
|
|
|
+ nextRegIdx = regIdx + 1;
|
|
|
|
|
+ } break;
|
|
|
|
|
+ case CALC_FUNC_DIV: { // 7
|
|
|
|
|
+ printf("TODO:calc__run:Not Implemented:CALC_FUNC_DIV"); // reg[a] / reg[b];
|
|
|
|
|
+ regVal[regIdx] = 0;
|
|
|
|
|
+ nextRegIdx = regIdx + 1;
|
|
|
|
|
+ } break;
|
|
|
|
|
+ case CALC_FUNC_MULTI: { // 8
|
|
|
|
|
+ printf("TODO:calc__run:Not Implemented:CALC_FUNC_MULTI"); // reg[a] * reg[b];
|
|
|
|
|
+ regVal[regIdx] = 0;
|
|
|
|
|
+ nextRegIdx = regIdx + 1;
|
|
|
|
|
+ } break;
|
|
|
|
|
+ case CALC_FUNC_ADD: { // 9
|
|
|
|
|
+ regVal[regIdx] = regVal[*pa] + regVal[*pb];
|
|
|
|
|
+ nextRegIdx = regIdx + 1;
|
|
|
|
|
+ } break;
|
|
|
|
|
+ case CALC_FUNC_SUB: { // 10
|
|
|
|
|
+ regVal[regIdx] = regVal[*pa] - regVal[*pb];
|
|
|
|
|
+ nextRegIdx = regIdx + 1;
|
|
|
|
|
+ } break;
|
|
|
|
|
+ case CALC_FUNC_OPPOSITE: { // 11
|
|
|
|
|
+ regVal[regIdx] = regVal[*pa] * -1; // -1 * nr;
|
|
|
|
|
+ nextRegIdx = regIdx + 1;
|
|
|
|
|
+ } break;
|
|
|
|
|
+ case CALC_FINISH: { // 12
|
|
|
|
|
+ finish = 1; // TODO: break loop
|
|
|
|
|
+ } break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ (finish)
|
|
|
|
|
+ ? printf("DBG:calc__run:loop:%d:reg[%d] => %d; finish!\n", i, regIdx, regVal[regIdx])
|
|
|
|
|
+ : printf("DBG:calc__run:loop:%d:reg[%d] => %d; goto %d\n", i, regIdx, regVal[regIdx], nextRegIdx)
|
|
|
|
|
+ ;
|
|
|
|
|
+
|
|
|
|
|
+ regIdx = nextRegIdx;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ printf("DBG:calc__run:end:regVal: { "); for (int i = 0; i < confTotal; i++) { printf("%d:%d, ", i, regVal[i]); }; printf("}\n"); // TODO: DBG
|
|
|
|
|
+ printf("DBG:calc__run:end:regData: { "); for (int i = 0; i < 7; i++) { printf("%d:%d, ", i, regData[i]); }; printf("}\n"); // TODO: DBG
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+int main() {
|
|
|
|
|
+ printf("Test calc db: Start\n");
|
|
|
|
|
+
|
|
|
|
|
+ // p5__config__individual__init(
|
|
|
|
|
+ // P5__TASK__CONFIG_A,
|
|
|
|
|
+ // P5__TASK__CONFIG__INDEX__CURRENT_A,
|
|
|
|
|
+ // P5__TASK__CONFIG__INDEX__CURRENT__EXEC_A,
|
|
|
|
|
+ // P5__TASK__ATTR_A,
|
|
|
|
|
+ // P5__TASK__ATTR__INDEX__CURRENT_A,
|
|
|
|
|
+ // P5__TASK__ATTR__INDEX__CURRENT__EXEC_A
|
|
|
|
|
+ // );
|
|
|
|
|
+
|
|
|
|
|
+ // char inputBuf[1];
|
|
|
|
|
+ // char inputChar;
|
|
|
|
|
+
|
|
|
|
|
+ int TOTAL_ATTRS = 7;
|
|
|
|
|
+ int obj1[TOTAL_ATTRS];
|
|
|
|
|
+ int obj2[TOTAL_ATTRS];
|
|
|
|
|
+ int PIN_NR = 0;
|
|
|
|
|
+ int LVL_MIN = 1;
|
|
|
|
|
+ int LVL_MAX = 2;
|
|
|
|
|
+ int LVL_VAL = 3;
|
|
|
|
|
+ int DELTA = 4;
|
|
|
|
|
+ int MAX_DELTA = 5;
|
|
|
|
|
+ int SWING = 6;
|
|
|
|
|
+ obj1[PIN_NR] = 9; // const
|
|
|
|
|
+ obj1[LVL_MIN] = 150; // const
|
|
|
|
|
+ obj1[LVL_MAX] = 200; // const
|
|
|
|
|
+ obj1[LVL_VAL] = 150; // var, default = 150
|
|
|
|
|
+ obj1[DELTA] = 0; // var, default 0
|
|
|
|
|
+ obj1[MAX_DELTA] = 10; // var, default = 10
|
|
|
|
|
+ obj1[SWING] = 8; // var, default = 8
|
|
|
|
|
+
|
|
|
|
|
+ obj2[PIN_NR] = 10; // const
|
|
|
|
|
+ obj2[LVL_MIN] = 150; // const
|
|
|
|
|
+ obj2[LVL_MAX] = 200; // const
|
|
|
|
|
+ obj2[LVL_VAL] = 150; // var, default = 150
|
|
|
|
|
+ obj2[DELTA] = 0; // var, default 0
|
|
|
|
|
+ obj2[MAX_DELTA] = 10; // var, default = 10
|
|
|
|
|
+ obj2[SWING] = 30; // var, default = 8
|
|
|
|
|
+
|
|
|
|
|
+ calc__db_init();
|
|
|
|
|
+ int id1 = calc__db_create_obj(); // LVL_VAL = (LVL_VAL + DELTA > LVL_MAX) ? LVL_VAL - DELTA : LVL_VAL + DELTA;
|
|
|
|
|
+ calc__db_conf_add1(id1, CALC_GET_DATA, LVL_VAL); // reg: 0
|
|
|
|
|
+ calc__db_conf_add1(id1, CALC_DEFINE, 35); // reg: 1
|
|
|
|
|
+ calc__db_conf_add1(id1, CALC_GET_DATA, LVL_MAX); // reg: 2
|
|
|
|
|
+ calc__db_conf_add2(id1, CALC_FUNC_ADD, 0, 1); // reg: 3 : ... LVL_VAL + DELTA ...
|
|
|
|
|
+ calc__db_conf_add3(id1, CALC_COMP_GT, 3, 2, 8); // reg: 4 : ... (LVL_VAL + DELTA > LVL_MAX) ? ...
|
|
|
|
|
+ calc__db_conf_add2(id1, CALC_FUNC_SUB, 0, 1); // reg: 5 : ... ? LVL_VAL - DELTA ...
|
|
|
|
|
+ calc__db_conf_add2(id1, CALC_SET_DATA, LVL_VAL, 5); // reg: 6 : LVL_VAL = ...
|
|
|
|
|
+ calc__db_conf_add0(id1, CALC_FINISH); // reg: 7
|
|
|
|
|
+ calc__db_conf_add2(id1, CALC_SET_DATA, LVL_VAL, 3); // reg: 8 : LVL_VAL = ( ... ) ? ... : LVL_VAL + DELTA
|
|
|
|
|
+ calc__db_conf_add0(id1, CALC_FINISH); // reg: 9
|
|
|
|
|
+
|
|
|
|
|
+ int id2 = calc__db_create_obj();
|
|
|
|
|
+ // int NEW_VAL = LVL_VAL + SWING;
|
|
|
|
|
+ // if (NEW_VAL > LVL_MAX) {
|
|
|
|
|
+ // LVL_VAL = LVL_MAX - (NEW_VAL - LVL_MAX);
|
|
|
|
|
+ // SWING = -1 * SWING;
|
|
|
|
|
+ // } else {
|
|
|
|
|
+ // if (NEW_VAL < LVL_MIN) {
|
|
|
|
|
+ // LVL_VAL = LVL_MIN + (LVL_MIN - NEW_VAL);
|
|
|
|
|
+ // SWING = -1 * SWING;
|
|
|
|
|
+ // } else {
|
|
|
|
|
+ // LVL_VAL = NEW_VAL;
|
|
|
|
|
+ // }
|
|
|
|
|
+ // }
|
|
|
|
|
+ calc__db_conf_add1(id2, CALC_GET_DATA, LVL_VAL); // reg: 0 : LVL_VAL
|
|
|
|
|
+ calc__db_conf_add1(id2, CALC_GET_DATA, SWING); // reg: 1 : SWING
|
|
|
|
|
+ calc__db_conf_add1(id2, CALC_DEFINE, 150); // reg: 2 : LVL_MIN
|
|
|
|
|
+ calc__db_conf_add1(id2, CALC_DEFINE, 200); // reg: 3 : LVL_MAX
|
|
|
|
|
+ calc__db_conf_add2(id2, CALC_FUNC_ADD, 0, 1); // reg: 4 : NEW_VAL = LVL_VAL + SWING;
|
|
|
|
|
+ calc__db_conf_add3(id2, CALC_COMP_GT, 4, 3, 12); // reg: 5 : if (NEW_VAL > LVL_MAX) {
|
|
|
|
|
+ calc__db_conf_add2(id2, CALC_FUNC_SUB, 4, 3); // reg: 6 : ... (NEW_VAL - LVL_MAX)
|
|
|
|
|
+ calc__db_conf_add2(id2, CALC_FUNC_SUB, 3, 6); // reg: 7 : LVL_MAX - (NEW_VAL - LVL_MAX);
|
|
|
|
|
+ calc__db_conf_add2(id2, CALC_SET_DATA, LVL_VAL, 7); // reg: 8 : LVL_VAL = LVL_MAX - (NEW_VAL - LVL_MAX);
|
|
|
|
|
+ calc__db_conf_add1(id2, CALC_FUNC_OPPOSITE, 1); // reg: 9 : -1 * SWING
|
|
|
|
|
+ calc__db_conf_add2(id2, CALC_SET_DATA, SWING, 9); // reg: 10 : SWING = -1 * SWING;
|
|
|
|
|
+ calc__db_conf_add0(id2, CALC_FINISH); // reg: 11
|
|
|
|
|
+ calc__db_conf_add3(id2, CALC_COMP_LT, 4, 2, 19); // reg: 12 : if (NEW_VAL < LVL_MIN) {
|
|
|
|
|
+ calc__db_conf_add2(id2, CALC_FUNC_SUB, 2, 4); // reg: 13 : ... (LVL_MIN - NEW_VAL);
|
|
|
|
|
+ calc__db_conf_add2(id2, CALC_FUNC_ADD, 2, 13); // reg: 14 : LVL_MIN + (LVL_MIN - NEW_VAL);
|
|
|
|
|
+ calc__db_conf_add2(id2, CALC_SET_DATA, LVL_VAL, 14); // reg : 15 : LVL_VAL = LVL_MIN + (LVL_MIN - NEW_VAL);
|
|
|
|
|
+ calc__db_conf_add1(id2, CALC_FUNC_OPPOSITE, 1); // reg: 16 : -1 * SWING
|
|
|
|
|
+ calc__db_conf_add2(id2, CALC_SET_DATA, SWING, 9); // reg: 17 : SWING = -1 * SWING;
|
|
|
|
|
+ calc__db_conf_add0(id2, CALC_FINISH); // reg: 18
|
|
|
|
|
+ calc__db_conf_add2(id2, CALC_SET_DATA, LVL_VAL, 4); // reg: 19 : LVL_VAL = NEW_VAL;
|
|
|
|
|
+ calc__db_conf_add0(id2, CALC_FINISH); // reg: 20
|
|
|
|
|
+
|
|
|
|
|
+ calc__DBG_OBJ(id1);
|
|
|
|
|
+ calc__DBG_OBJ(id2);
|
|
|
|
|
+
|
|
|
|
|
+ int j = 0;
|
|
|
|
|
+ for (j = 0; j < 3; j++) { // like Arduino loop
|
|
|
|
|
+
|
|
|
|
|
+ printf("DBG:loop:%d:obj1:\n", j);
|
|
|
|
|
+ calc__run(id1, obj1);
|
|
|
|
|
+ printf("DBG:loop:%d:obj1:END; obj1[LVL_VAL:%d] = %d\n", j, LVL_VAL, obj1[LVL_VAL]);
|
|
|
|
|
+
|
|
|
|
|
+ printf("DBG:loop:%d:obj2:\n", j);
|
|
|
|
|
+ calc__run(id2, obj2);
|
|
|
|
|
+ printf("DBG:loop:%d:obj2:END; obj2[LVL_VAL:%d] = %d\n", j, LVL_VAL, obj2[LVL_VAL]);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ printf("Test calc db: End\n");
|
|
|
|
|
+ return(0);
|
|
|
|
|
+}
|