/* ss-core/core.c * * Logic! * * Copyright (C) 2003, 2004 * Andy Goth * * This code is available under the GNU General Public License; see COPYING. */ #include #include #include "evlist/evlist.h" #include "ss-core/core.h" #define DB_NAME "ss-core/shelf.db" static int core_display(void* arg); int core_init(core_t* core, device_t* sshw, device_t* ssgui, evlist_t* evlist) { int i; char* err; /* Open database. */ core->db = sqlite_open(DB_NAME, 0, &err); if (core->db == NULL) { fprintf(stderr, "sqlite_open: %s\n", err); free(err); return -1; } core->sshw = sshw ; core->ssgui = ssgui ; core->evlist = evlist; core->display_id = 0 ; for (i = 0; i < 2; ++i) core->door [i] = 0; for (i = 0; i < 4; ++i) core->pressure[i] = 0; core_display(core); return 0; } int core_cleanup(core_t* core) { sqlite_close(core->db); evlist_sched_del(core->evlist, core->display_id); core->sshw = NULL; core->ssgui = NULL; core->evlist = NULL; core->db = NULL; return 0; } /* Called by main() after the devices are all set up and ready to go. */ int core_start(core_t* core) { /* Use these functions to... */ sshw_send_rf_field (core->sshw, 1 ); /* Enable or disable the field. */ sshw_send_inv_delay(core->sshw, 1000); /* Change the inventory delay. */ return 0; } /* Called with door update information. */ int core_doors(core_t* core, int a, int b) { #if 0 if (a && b && (!core->door[0] || !core->door[1])) { /* Deactivate field. */ sshw_send_rf_field(core->sshw, 0); } else if ((!a || !b) && (core->door[0] && core->door[1])) { /* Activate field. */ sshw_send_rf_field(core->sshw, 1); } #endif core->door[0] = a; core->door[1] = b; return 0; } /* Called with new pressure data. */ int core_pressure(core_t* core, int a, int b, int c, int d) { core->pressure[0] = a; core->pressure[1] = b; core->pressure[2] = c; core->pressure[3] = d; return 0; /* return ssgui_send_pressure(core->ssgui, a, b, c, d); */ } /* Called when a new tag is found. */ int core_tag_add(core_t* core, uint64_t uid, uint32_t type) { char* err; char buf[256]; snprintf(buf, 256, "insert into inventory values('%016llx', '%08x');", uid, type); if (sqlite_exec(core->db, buf, NULL, NULL, &err) != SQLITE_OK) { fprintf(stderr, "sqlite_exec: %s\n", err); free(err); } return 0; /* return ssgui_send_tag_add(core->ssgui, uid, type); */ } /* Called when a tag is removed. */ int core_tag_del(core_t* core, uint64_t uid) { char* err; char buf[256]; snprintf(buf, 256, "delete from inventory where uid='%016llx';", uid); if (sqlite_exec(core->db, buf, NULL, NULL, &err) != SQLITE_OK) { fprintf(stderr, "sqlite_exec: %s\n", err); free(err); } return 0; /* return ssgui_send_tag_del(core->ssgui, uid); */ } /* Demonstrates how to use evlist_sched_add() to arrange for a function to be * executed periodically. */ static int core_display(void* arg) { core_t* core = arg; int i, j; sqlite_vm* vm; char* err; const char* tail; int num_cols; const char** col_data; const char** col_names; char* msg[] = { "Both doors closed.", "Right door opened.", "Left door opened." , "Both doors opened." }; struct timeval timeval; struct tm tm; gettimeofday(&timeval, NULL); localtime_r(&timeval.tv_sec, &tm); /* Clear screen; move cursor to home. */ printf("\e[2J\e[1;1H"); /* Print status row. */ printf("\e[1;35m%02d\e[0;35m:\e[1;35m%02d\e[0;35m:\e[1;35m%02d\e[0m" " \e[1;30m-\e[0m-\e[1;30m- " "\e[1;32mPressure values\e[0;32m: \e[1;32m%3d %3d %3d %3d" " \e[1;30m-\e[0m-\e[1;30m- " "\e[1;33m%s\e[0m\n", tm.tm_hour, tm.tm_min, tm.tm_sec, core->pressure[0], core->pressure[1], core->pressure[2], core->pressure[3], msg[core->door[1] * 2 + core->door[0]]); printf("\e[1;30m-\e[0m-----------------------------------" "--------------------------------\e[1;30m-\e[0m\n"); /* Print item list. */ if (sqlite_compile(core->db, " select count(*), d.name, i.type " " from dictionary as d, inventory as i" " where d.type = i.type " "group by d.type " "order by d.name; ", &tail, &vm, &err) != SQLITE_OK) { fprintf(stderr, "sqlite_compile: %s\n", err); free(err); return 0; } while (sqlite_step(vm, &num_cols, &col_data, &col_names) != SQLITE_DONE) { printf("\e[1;31m%3s\e[0;31mx \e[1;37m%-50s\e[0m " "\e[0;34m(\e[1m%8s\e[0;34m)\e[0m\n", col_data[0], col_data[1], col_data[2]); } if (sqlite_finalize(vm, &err) != SQLITE_OK) { fprintf(stderr, "sqlite_finalize: %s\n", err); free(err); } /* Display snazzy pressure bars. */ for (i = 0; i < 24; ++i) { printf("\e[%d;74H", i + 1); for (j = 0; j < 4; ++j) { if (core->pressure[j] >= 255 - i * 11) { printf("\e[1;4%dm \e[0m ", j + 1); } else { printf("\e[0;3%dm_\e[0m ", j + 1); } } } printf("\e[23;1H"); /* Do it. */ fflush(stdout); /* Reschedule. */ evlist_sched_add(core->evlist, 1000, core_display, core, &core->display_id); return 0; } /* vim: set ts=4 sts=4 sw=4 tw=80 et: */