diff --git a/project/Giomba/Makefile b/project/Giomba/Makefile index 7af95bd48..a0f4d2c20 100644 --- a/project/Giomba/Makefile +++ b/project/Giomba/Makefile @@ -1,5 +1,5 @@ CONTIKI_PROJECT = cart -PROJECT_SOURCEFILES += sendrecv.c status.c +PROJECT_SOURCEFILES += sendrecv.c status.c batmon.c leds.cart.c PLATFORMS_ONLY = cc26x0-cc13x0 all: $(CONTIKI_PROJECT) diff --git a/project/Giomba/batmon.c b/project/Giomba/batmon.c new file mode 100644 index 000000000..92e606b3e --- /dev/null +++ b/project/Giomba/batmon.c @@ -0,0 +1,34 @@ +#include "batmon.h" + +void batmon(void) { + + /* read battery monitor sensor -- hey where's the battery? */ + /* actually this is a simulation, isn't it? =) */ + + switch (status) { + /* charge */ + case NOT_ASSOCIATED: + case ASSOCIATED: + battery_charge += 5; + if (battery_charge > 100) battery_charge = 100; + led_heartbeat(NULL); + break; + + /* discharge */ + default: + battery_charge -= 1; + if (battery_charge < 0) battery_charge = 0; + break; + } + + /* turn on red led if battery charge is low */ + if (battery_charge < 48) { /* TODO */ + leds_on(LEDS_RED); + } else { + leds_off(LEDS_RED); + } + + etimer_reset(&battery_timer); + + printf("[I] updated battery status: %d %%\n", battery_charge); +} diff --git a/project/Giomba/batmon.h b/project/Giomba/batmon.h new file mode 100644 index 000000000..ab4ea93cb --- /dev/null +++ b/project/Giomba/batmon.h @@ -0,0 +1,9 @@ +#ifndef BATMON_H +#define BATMON_H + +#include "leds.cart.h" +#include "status.h" + +void batmon(void); + +#endif diff --git a/project/Giomba/cart.c b/project/Giomba/cart.c index 3bb1bf108..6b6513009 100644 --- a/project/Giomba/cart.c +++ b/project/Giomba/cart.c @@ -1,29 +1,26 @@ #include #include -#include "os/dev/leds.h" -#include "batmon-sensor.h" - #include "../common/supermarket_net.h" +#include "batmon.h" #include "event.h" #include "log.h" #include "sendrecv.h" #include "status.h" -//static linkaddr_t destination_address = {{ 0x00, 0x12, 0x4b, 0x00, 0x0f, 0x8f, 0x18, 0x11 }}; - PROCESS(cart_main_process, "Cart Process"); AUTOSTART_PROCESSES(&cart_main_process); PROCESS_THREAD(cart_main_process, ev, data) { PROCESS_BEGIN(); - // SENSORS_ACTIVATE(batmon_sensor); /*** Variables initialization ***/ - // status = NOT_ASSOCIATED; // TODO DEBUG - status = SHOPPING; + status = NOT_ASSOCIATED; // TODO DEBUG + //status = SHOPPING; - etimer_set(&broadcast_timer, 5 * CLOCK_SECOND); + /*** Timer initialization ***/ + etimer_set(&assigner_timer, 5 * CLOCK_SECOND); + etimer_set(&battery_timer, 10 * CLOCK_SECOND); /*** Subsystem initialization ***/ net_init(); @@ -34,6 +31,8 @@ PROCESS_THREAD(cart_main_process, ev, data) { while (true) { PROCESS_WAIT_EVENT(); + if (ev == PROCESS_EVENT_TIMER && etimer_expired(&battery_timer)) batmon(); + switch(status) { case NOT_ASSOCIATED: s_not_associated(ev, data); break; case ASSOCIATED: s_associated(ev, data); break; diff --git a/project/Giomba/leds.cart.c b/project/Giomba/leds.cart.c new file mode 100644 index 000000000..43e2f3a17 --- /dev/null +++ b/project/Giomba/leds.cart.c @@ -0,0 +1,29 @@ +#include "leds.cart.h" + +void led_blink(void* ptr) { + printf("[I] led blink\n"); + if (! (leds_get() & LEDS_GREEN)) { + leds_on(LEDS_GREEN); + ctimer_set(&led_timer, CLOCK_SECOND, led_blink, NULL); + } else { + leds_off(LEDS_GREEN); + } + return; +} + + +void led_heartbeat(void* ptr) { + printf("[I] led heartbeat\n"); + +/* + static uint8_t times = 4; + + if (times == 4) leds_off(LEDS_GREEN); + if (--times != 0) { + leds_toggle(LEDS_GREEN); + ctimer_set(&led_timer, 20, led_heartbeat, NULL); + } else { + times = 4; + } +*/ +} diff --git a/project/Giomba/leds.cart.h b/project/Giomba/leds.cart.h new file mode 100644 index 000000000..f2db7353b --- /dev/null +++ b/project/Giomba/leds.cart.h @@ -0,0 +1,10 @@ +#ifndef LEDS_CART_H +#define LEDS_CART_H + +#include "status.h" +#include "os/dev/leds.h" + +void led_blink(void*); +void led_heartbeat(void*); + +#endif diff --git a/project/Giomba/sendrecv.c b/project/Giomba/sendrecv.c index 32a633804..a296d43ed 100644 --- a/project/Giomba/sendrecv.c +++ b/project/Giomba/sendrecv.c @@ -58,6 +58,7 @@ void net_recv(const void* data, uint16_t len, const linkaddr_t* src, const linka } void net_send(const void* data, uint16_t len, const linkaddr_t* dst) { + printf("[I] sending msg type %d\n", ((msg*)data)->msg_type); memcpy(net_buffer, data, len); nullnet_len = len; NETSTACK_NETWORK.output(dst); diff --git a/project/Giomba/status.c b/project/Giomba/status.c index 5eb3bc09e..7b793d967 100644 --- a/project/Giomba/status.c +++ b/project/Giomba/status.c @@ -3,7 +3,10 @@ #define MAX_PRODUCT 20 enum CartStatus status; -struct etimer broadcast_timer; +struct etimer assigner_timer; /* timer to periodically make a report to the assigner */ +struct etimer battery_timer; /* timer to periodically read battery status */ +struct ctimer led_timer; /* timer to shortly blink leds */ +int8_t battery_charge = 50; linkaddr_t assigner_address; linkaddr_t cash_address; uint32_t customer_id = 1234; @@ -16,20 +19,24 @@ product_t list[MAX_PRODUCT]; void s_not_associated(process_event_t ev, process_data_t data) { if (ev == PROCESS_EVENT_TIMER) { /* at time expiration, send broadcast message to request association with assigner */ - if (etimer_expired(&broadcast_timer)) { + if (etimer_expired(&assigner_timer)) { printf("[I] Sending association request msg\n"); msg m; m.msg_type = ASSOCIATION_REQUEST_MSG; net_send(&m, sizeof(m), NULL); - etimer_reset(&broadcast_timer); + etimer_reset(&assigner_timer); } } else /* if a msg is received from network and represents an association event, then associate */ if (ev == PROCESS_EVENT_MSG && event == CART_EVENT_ASSOCIATED) { printf("[I] Associated with Assigner\n"); + assigner_address = pkt.src; nprod = 0; status = ASSOCIATED; + + /* blink led */ + led_blink(NULL); } } @@ -39,14 +46,15 @@ void s_associated(process_event_t ev, process_data_t data) { printf("[I] Sending battery level\n"); struct battery_msg m; m.msg_type = BATTERY_STATUS_MSG; - m.battery_percentage = 77; + m.battery_percentage = battery_charge; net_send(&m, sizeof(m), &assigner_address); - etimer_reset(&broadcast_timer); + etimer_reset(&assigner_timer); } if (ev == PROCESS_EVENT_MSG && event == CART_EVENT_ASSIGNED) { /* cart has been assigned to a new customer */ customer_id = ((assign_msg*)pkt.data)->customer_id; printf("[I] Assigned to customer id %d\n", (int)customer_id); + leds_on(LEDS_GREEN); status = SHOPPING; } } @@ -84,6 +92,7 @@ void s_shopping(process_event_t ev, process_data_t data) { } if (ev == button_hal_release_event) { /* any button will do */ /* ask product to send items to me */ + printf("[I] Dear product Launchpad, please send items to me\n"); msg m; m.msg_type = START_SHOPPING_MSG; net_send(&m, sizeof(m), NULL); @@ -135,9 +144,10 @@ void s_cash_out_send_list(process_event_t ev, process_data_t data) { memset(&cash_address, 0, sizeof(cash_address)); printf("[I] List END. Go back to ASSOCIATED status\n"); - etimer_restart(&broadcast_timer); - // status = ASSOCIATED; // TODO DEBUG - status = SHOPPING; + etimer_restart(&assigner_timer); + leds_off(LEDS_GREEN); + status = ASSOCIATED; + // status = SHOPPING; // TODO DEBUG } } diff --git a/project/Giomba/status.h b/project/Giomba/status.h index 27915b340..b7d10f862 100644 --- a/project/Giomba/status.h +++ b/project/Giomba/status.h @@ -6,9 +6,11 @@ #include "sys/process.h" #include "os/dev/button-hal.h" +#include "batmon-sensor.h" #include "../common/supermarket_net.h" #include "event.h" +#include "leds.cart.h" #include "sendrecv.h" enum CartStatus { @@ -20,7 +22,11 @@ enum CartStatus { }; extern enum CartStatus status; -extern struct etimer broadcast_timer; +extern struct etimer assigner_timer; +extern struct etimer battery_timer; +extern struct ctimer led_timer; + +extern int8_t battery_charge; void s_not_associated(process_event_t ev, process_data_t data); void s_associated(process_event_t ev, process_data_t data); diff --git a/project/product/product.c b/project/product/product.c index c441697f7..820edad84 100644 --- a/project/product/product.c +++ b/project/product/product.c @@ -17,7 +17,7 @@ #define LOG_LEVEL LOG_LEVEL_INFO /* MAC Address for cart -- This emulates the RFID tag */ -static linkaddr_t dest_addr = {{0x00, 0x12, 0x4b, 0x00, 0x0f, 0x82, 0x00, 0x04}}; +static linkaddr_t dest_addr = {{0xca, 0xfe, 0xba, 0xbe, 0xde, 0xad, 0xbe, 0xef}}; product_t product_list[] = { { 1, "21/12/19", 105 },