[cart] marvellous leds and battery monitor on the cart

- plus some minor fixes here and there, as usual
This commit is contained in:
giomba 2019-05-08 22:05:24 +02:00
parent 10f4cac7ff
commit 3b142cd991
10 changed files with 118 additions and 20 deletions

View File

@ -1,5 +1,5 @@
CONTIKI_PROJECT = cart CONTIKI_PROJECT = cart
PROJECT_SOURCEFILES += sendrecv.c status.c PROJECT_SOURCEFILES += sendrecv.c status.c batmon.c leds.cart.c
PLATFORMS_ONLY = cc26x0-cc13x0 PLATFORMS_ONLY = cc26x0-cc13x0
all: $(CONTIKI_PROJECT) all: $(CONTIKI_PROJECT)

34
project/Giomba/batmon.c Normal file
View File

@ -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);
}

9
project/Giomba/batmon.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef BATMON_H
#define BATMON_H
#include "leds.cart.h"
#include "status.h"
void batmon(void);
#endif

View File

@ -1,29 +1,26 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "os/dev/leds.h"
#include "batmon-sensor.h"
#include "../common/supermarket_net.h" #include "../common/supermarket_net.h"
#include "batmon.h"
#include "event.h" #include "event.h"
#include "log.h" #include "log.h"
#include "sendrecv.h" #include "sendrecv.h"
#include "status.h" #include "status.h"
//static linkaddr_t destination_address = {{ 0x00, 0x12, 0x4b, 0x00, 0x0f, 0x8f, 0x18, 0x11 }};
PROCESS(cart_main_process, "Cart Process"); PROCESS(cart_main_process, "Cart Process");
AUTOSTART_PROCESSES(&cart_main_process); AUTOSTART_PROCESSES(&cart_main_process);
PROCESS_THREAD(cart_main_process, ev, data) { PROCESS_THREAD(cart_main_process, ev, data) {
PROCESS_BEGIN(); PROCESS_BEGIN();
// SENSORS_ACTIVATE(batmon_sensor);
/*** Variables initialization ***/ /*** Variables initialization ***/
// status = NOT_ASSOCIATED; // TODO DEBUG status = NOT_ASSOCIATED; // TODO DEBUG
status = SHOPPING; //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 ***/ /*** Subsystem initialization ***/
net_init(); net_init();
@ -34,6 +31,8 @@ PROCESS_THREAD(cart_main_process, ev, data) {
while (true) { while (true) {
PROCESS_WAIT_EVENT(); PROCESS_WAIT_EVENT();
if (ev == PROCESS_EVENT_TIMER && etimer_expired(&battery_timer)) batmon();
switch(status) { switch(status) {
case NOT_ASSOCIATED: s_not_associated(ev, data); break; case NOT_ASSOCIATED: s_not_associated(ev, data); break;
case ASSOCIATED: s_associated(ev, data); break; case ASSOCIATED: s_associated(ev, data); break;

View File

@ -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;
}
*/
}

View File

@ -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

View File

@ -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) { 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); memcpy(net_buffer, data, len);
nullnet_len = len; nullnet_len = len;
NETSTACK_NETWORK.output(dst); NETSTACK_NETWORK.output(dst);

View File

@ -3,7 +3,10 @@
#define MAX_PRODUCT 20 #define MAX_PRODUCT 20
enum CartStatus status; 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 assigner_address;
linkaddr_t cash_address; linkaddr_t cash_address;
uint32_t customer_id = 1234; 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) { void s_not_associated(process_event_t ev, process_data_t data) {
if (ev == PROCESS_EVENT_TIMER) { if (ev == PROCESS_EVENT_TIMER) {
/* at time expiration, send broadcast message to request association with assigner */ /* 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"); printf("[I] Sending association request msg\n");
msg m; msg m;
m.msg_type = ASSOCIATION_REQUEST_MSG; m.msg_type = ASSOCIATION_REQUEST_MSG;
net_send(&m, sizeof(m), NULL); 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 */ else /* if a msg is received from network and represents an association event, then associate */
if (ev == PROCESS_EVENT_MSG && event == CART_EVENT_ASSOCIATED) { if (ev == PROCESS_EVENT_MSG && event == CART_EVENT_ASSOCIATED) {
printf("[I] Associated with Assigner\n"); printf("[I] Associated with Assigner\n");
assigner_address = pkt.src; assigner_address = pkt.src;
nprod = 0; nprod = 0;
status = ASSOCIATED; 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"); printf("[I] Sending battery level\n");
struct battery_msg m; struct battery_msg m;
m.msg_type = BATTERY_STATUS_MSG; m.msg_type = BATTERY_STATUS_MSG;
m.battery_percentage = 77; m.battery_percentage = battery_charge;
net_send(&m, sizeof(m), &assigner_address); net_send(&m, sizeof(m), &assigner_address);
etimer_reset(&broadcast_timer); etimer_reset(&assigner_timer);
} }
if (ev == PROCESS_EVENT_MSG && event == CART_EVENT_ASSIGNED) { if (ev == PROCESS_EVENT_MSG && event == CART_EVENT_ASSIGNED) {
/* cart has been assigned to a new customer */ /* cart has been assigned to a new customer */
customer_id = ((assign_msg*)pkt.data)->customer_id; customer_id = ((assign_msg*)pkt.data)->customer_id;
printf("[I] Assigned to customer id %d\n", (int)customer_id); printf("[I] Assigned to customer id %d\n", (int)customer_id);
leds_on(LEDS_GREEN);
status = SHOPPING; 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 */ if (ev == button_hal_release_event) { /* any button will do */
/* ask product to send items to me */ /* ask product to send items to me */
printf("[I] Dear product Launchpad, please send items to me\n");
msg m; msg m;
m.msg_type = START_SHOPPING_MSG; m.msg_type = START_SHOPPING_MSG;
net_send(&m, sizeof(m), NULL); 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)); memset(&cash_address, 0, sizeof(cash_address));
printf("[I] List END. Go back to ASSOCIATED status\n"); printf("[I] List END. Go back to ASSOCIATED status\n");
etimer_restart(&broadcast_timer); etimer_restart(&assigner_timer);
// status = ASSOCIATED; // TODO DEBUG leds_off(LEDS_GREEN);
status = SHOPPING; status = ASSOCIATED;
// status = SHOPPING; // TODO DEBUG
} }
} }

View File

@ -6,9 +6,11 @@
#include "sys/process.h" #include "sys/process.h"
#include "os/dev/button-hal.h" #include "os/dev/button-hal.h"
#include "batmon-sensor.h"
#include "../common/supermarket_net.h" #include "../common/supermarket_net.h"
#include "event.h" #include "event.h"
#include "leds.cart.h"
#include "sendrecv.h" #include "sendrecv.h"
enum CartStatus { enum CartStatus {
@ -20,7 +22,11 @@ enum CartStatus {
}; };
extern enum CartStatus status; 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_not_associated(process_event_t ev, process_data_t data);
void s_associated(process_event_t ev, process_data_t data); void s_associated(process_event_t ev, process_data_t data);

View File

@ -17,7 +17,7 @@
#define LOG_LEVEL LOG_LEVEL_INFO #define LOG_LEVEL LOG_LEVEL_INFO
/* MAC Address for cart -- This emulates the RFID tag */ /* 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[] = { product_t product_list[] = {
{ 1, "21/12/19", 105 }, { 1, "21/12/19", 105 },