Exemple complet qui affiche sur la liaison série les informations reçues des capteurs qui intégre des commandes permettant de gérer l'intégration et l'exclusion des capteurs et de leur envoyer des informations.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <avrio/serial.h>
#include <avrio/term.h>
#include <avrio/util.h>
#include <avrio/led.h>
#include <idwarf/hub.h>
#define DEBUG_BAUDRATE 115200
#define ADC_SCALE 3.3
#define CMD_SIZE 21
#define BEACON_TIME IDWARF_SLEEP_TIME(5) // 5 sec.
uint8_t ucDataLen = 0;
uint16_t usBeaconTime = BEACON_TIME;
enum {
HEX_FLAG = 0x01,
PRINT_FLAG = 0x02
};
volatile uint8_t ucFlags = HEX_FLAG | PRINT_FLAG;
void vSensorPacketReceived (
PACKET_TYPES, uint16_t, uint8_t,
volatile uint8_t *);
void vProcessRxData (void);
void vPrintStatus (void);
void vPrintOk (void);
void vPrintBeaconTime (void);
void vPrintHelp (void);
int
main (void) {
printf_P (PSTR("\r\riDwaRF - Firmware v" __IDWARF_VERSION_STRING__ " Hub version\n\n"));
printf_P(PSTR("Init... "));
vPrintOk();
printf_P(PSTR("Register Call Back functions... "));
vPrintOk();
vPrintStatus();
vPrintBeaconTime();
vPrintHelp();
puts_P(PSTR("\nProcess All ..."));
for (;;) {
vProcessRxData();
}
if (ucDataLen > 0) {
uint8_t * pucData = ucData;
if ((ucFlags & HEX_FLAG) == 0) {
if (ucDataLen >= 1) {
printf_P (PSTR("Button %s"), (*pucData++ != 0 ? "ON " : "off"));
}
if (ucDataLen >= 2) {
uint8_t ucBatt;
ucBatt = *pucData++;
printf_P (PSTR(" Batt <0x%02X> %.2fV"), ucBatt, ADC_SCALE * (float) ucBatt / 256.0);
}
if (ucDataLen >= 4) {
int16_t iTemp;
iTemp = (*pucData++) << 8;
iTemp += *pucData++;
printf_P (PSTR(" Temp (0x%02X) %.1foC"), iTemp, (float)iTemp / 10.0);
}
if (ucDataLen >= 5) {
printf_P (PSTR(" Ldr %d"), *pucData++);
}
}
while (pucData < (ucData + ucDataLen)) {
printf_P (PSTR("%02X "), *pucData++);
}
printf_P (PSTR("> len=%d\n"), ucDataLen);
ucDataLen = 0;
}
}
}
void vPrintOk (void);
void vPrintError (void);
void vEnumerateAllSensors (void);
void vCleanupSensorList (void);
void vDoTimCommand (char * cCmd, uint8_t ucLen);
void vDoCnfCommand (char * cCmd, uint8_t ucLen);
void vDoSndCommand (char * cCmd, uint8_t ucLen);
void vDoDelCommand (char * cCmd, uint8_t ucLen);
void vSendBackData (uint16_t usDeviceId, uint8_t * pucPayload, uint8_t ucPayloadLen);
void vSensorPacketReceived (
PACKET_TYPES xPacketType, uint16_t usDeviceId,
uint8_t ucUserDataCount, volatile uint8_t * pucData) {
static uint8_t ucPacketCount;
if (ucUserDataCount == 5) {
if (pucData[4] != 0) {
}
}
ucPacketCount++;
if (ucFlags & PRINT_FLAG) {
printf_P (PSTR("%03d> S%d-"), ucPacketCount, xPacketType);
switch (xPacketType) {
case BIND_REQUEST_PACKET:
puts_P (PSTR("BIND_REQUEST\n"));
break;
printf_P (PSTR("ID%d <"), usDeviceId);
for (uint8_t n = 0; n < ucUserDataCount; n ++) {
ucData[n] = *pucData++;
}
ucDataLen = ucUserDataCount;
break;
default:
break;
}
}
}
}
void
vProcessRxData (void) {
static char cCmd[CMD_SIZE];
static uint8_t ucPos;
static char cLastChar;
cLastChar = cSerialGetChar();
ucFlags &= ~PRINT_FLAG;
cCmd[ucPos] = cLastChar;
if (ucPos < CMD_SIZE) {
ucPos++;
}
else {
ucPos = 0;
}
}
if (cLastChar == '\r') {
if (strncmp_P (cCmd, PSTR("rst\r"), 4) == 0) {
vPrintOk();
}
else if (strncmp_P (cCmd, PSTR("gps\r"), 4) == 0) {
vPrintOk();
vPrintStatus();
}
else if (strncmp_P (cCmd, PSTR("bon\r"), 4) == 0) {
vPrintOk();
vPrintStatus();
}
else if (strncmp_P (cCmd, PSTR("bof\r"), 4) == 0) {
vPrintOk();
vPrintStatus();
}
else if (strncmp_P (cCmd, PSTR("enu\r"), 4) == 0) {
vPrintOk();
vEnumerateAllSensors();
}
else if (strncmp_P (cCmd, PSTR("cln\r"), 4) == 0) {
vPrintOk();
vCleanupSensorList();
}
else if (strncmp_P (cCmd, PSTR("hex\r"), 4) == 0) {
vPrintOk();
ucFlags ^= HEX_FLAG;
printf_P (PSTR("hex %d\r"), ucFlags & HEX_FLAG);
}
else if (strncmp_P (cCmd, PSTR("hlp\r"), 4) == 0) {
vPrintOk();
vPrintHelp();
}
else if (strncmp_P (cCmd, PSTR("cnf"), 3) == 0) {
vDoCnfCommand (cCmd, ucPos);
}
else if (strncmp_P (cCmd, PSTR("tim"), 3) == 0) {
vDoTimCommand (cCmd, ucPos);
}
else if (strncmp_P (cCmd, PSTR("snd"), 3) == 0) {
vDoSndCommand (cCmd, ucPos);
}
else if (strncmp_P (cCmd, PSTR("del"), 3) == 0) {
vDoDelCommand (cCmd, ucPos);
}
else {
vPrintError();
}
ucPos = 0;
cLastChar = 0;
ucFlags |= PRINT_FLAG;
}
}
void
vPrintHelp (void) {
puts_P(PSTR("\n\nHub commands list:\n"
"rst\t\t\t\t - Reset firmware\n"
"gps\t\t\t\t - Get protocol status\n"
"bon\t\t\t\t - Set Bind On\n"
"bof\t\t\t\t - Set Bind Off\n"
"enu\t\t\t\t - Enumarate all sensors\n"
"cln\t\t\t\t - Cleanup sensor list\n"
"hex\t\t\t\t - Switch output to hex numbers\n"
"hlp\t\t\t\t - Display this help\n"
"cnf PNCODE CHANNEL\t\t - Configure Network parameters\n"
"tim BEACONTIME\t\t\t - Configure beacon time (unit=125ms)\n"
"snd DEVICEID BYTE1 [BYTE2]...\t - Send backchannel data\n"
"\t\t\t\t DEVICEID=x playload send to all devices\n"
"del [DEVICEID]\t\t\t - Remove sensor(s) from the hub\n\n"
));
}
void
vPrintOk (void) {
puts_P (PSTR("Ok"));
}
void
vPrintError (void) {
puts_P (PSTR("Error"));
}
void
vPrintStatus (void) {
uint8_t ucStatus;
printf_P (PSTR("Status 0x%02X\n"), ucStatus);
}
void
vPrintBeaconTime (void) {
printf_P (PSTR("Beacon Time %d s.\n"), usBeaconTime >> 3);
}
void
vEnumerateAllSensors (void) {
uint16_t usDeviceId;
uint16_t usNumOfDevicesFound = 0;
printf_P (PSTR("Maximum Devices = %d\n"), usMaximumDeviceId);
for (usDeviceId = 0; usDeviceId <= usMaximumDeviceId; usDeviceId++) {
printf_P (PSTR(
"deviceID = %d %+08lX\n"), usDeviceId, xMid.
u32Mid);
usNumOfDevicesFound++;
}
}
printf_P (PSTR("nofDevices = %d\n"), usNumOfDevicesFound);
}
void
vCleanupSensorList (void) {
uint16_t usDeviceId1;
for (usDeviceId1 = 0; usDeviceId1 <= usMaximumDeviceId; usDeviceId1++) {
uint16_t usDeviceId2;
for (usDeviceId2 = usDeviceId1 + 1; usDeviceId2 <= usMaximumDeviceId; usDeviceId2++) {
if (memcmp (&xMid1, &xMid2, 4) == 0) {
printf_P (PSTR ("Same entry found! remove the first one %d\n"), usDeviceId1);
break;
}
}
}
}
}
}
void
vSendBackData (uint16_t usDeviceId, uint8_t * pucPayload, uint8_t ucPayloadLen) {
static uint8_t ucPktCount = 0;
uint8_t ucStatus;
uint8_t * pucData = pucPayload;
printf_P (PSTR ("SND-ID%d <%04X %02X"), usDeviceId, usBeaconTime, ucPktCount);
while (pucData < (pucPayload + ucPayloadLen)) {
printf_P (PSTR("%02X "), *pucData++);
}
printf_P (PSTR("> %d\n"), ucPayloadLen + 3);
if (ucStatus == CMD_STATUS_MSG_QUEUED) {
vPrintOk();
}
else {
vPrintError();
}
}
void
vDoSndCommand (char * cCmd, uint8_t ucLen) {
uint16_t usDeviceId = 0;
bool sndToAll = false;
uint8_t * pucPayload = NULL;
uint8_t ucPayloadLen = 0;
uint8_t ucIndex;
if (ucLen > 4) {
if (cCmd[4] == 'x') {
sndToAll = true;
}
else {
usDeviceId = atoi (&cCmd[4]);
}
for (ucIndex = 4; ucIndex < ucLen; ucIndex++) {
if ((cCmd[ucIndex] == ' ') && (ucLen > ucIndex + 1)) {
pucPayload = (uint8_t *) &cCmd[ucIndex + 1];
ucPayloadLen = ucLen - (ucIndex + 2);
break;
}
}
}
if (sndToAll == false) {
vSendBackData (usDeviceId, pucPayload, ucPayloadLen);
}
else {
uint16_t usDeviceId;
for (usDeviceId = 0; usDeviceId <= usMaximumDeviceId; usDeviceId++) {
uint8_t ucStatus;
if (ucStatus) {
vSendBackData (usDeviceId, pucPayload, ucPayloadLen);
}
}
}
}
void
vDoCnfCommand (char * cCmd, uint8_t ucLen) {
uint8_t ucPnCode = 1;
uint8_t ucChannel = 1;
uint8_t ucStatus;
if (ucLen > 4) {
uint8_t ucIndex;
ucPnCode = atoi (&cCmd[4]);
for (ucIndex = 4; ucIndex < ucLen; ucIndex++) {
if (cCmd[ucIndex] == ' ') {
ucChannel = atoi (&cCmd[ucIndex]);
break;
}
}
}
if (ucStatus == 0) {
vPrintOk();
printf_P (PSTR ("Pn=%d - Ch=%d\n"), ucPnCode, ucChannel);
}
else {
vPrintError();
}
}
void
vDoTimCommand (char * cCmd, uint8_t ucLen) {
if (ucLen > 4) {
usBeaconTime = atoi (&cCmd[4]);
}
vPrintBeaconTime();
}
void
vDoDelCommand (char * cCmd, uint8_t ucLen) {
uint16_t usDeviceId = 0;
uint8_t ucStatus;
if (ucLen > 4) {
usDeviceId = atoi (&cCmd[4]);
}
vPrintOk();
printf_P (PSTR("0x%02X\n"), ucStatus);
}