Commit c83166e8 authored by levi's avatar levi
Browse files

removed leftover sources

parent 1cc1415b
#ifndef __MM_DEVICES_H_
#define __MM_DEVICES_H_
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include "mm_controller/mm_api.h"
#include "mm_controller/mm_utils.h"
typedef struct
{
uint32_t distance_mm;
} MarvelmindDevDistance;
typedef struct
{
MarvelmindDeviceInfo info;
MarvelmindDeviceVersion version;
bool devConnected;
MMDeviceType deviceType;
#if MM_LOCATIONS_VERSION == 1
MarvelmindDeviceLocation pos;
#endif // MM_LOCATIONS_VERSION
#if MM_LOCATIONS_VERSION == 2
MarvelmindDeviceLocation2 pos;
#endif // MM_LOCATIONS_VERSION
MarvelmindDevDistance distances[MM_MAX_DEVICES_COUNT];
} MarvelmindDevice;
// Read Marvelmind devices list from modem
void marvelmindDevicesReadIfNeeded(); //Move to node
// Get device data structure
MarvelmindDevice *getMarvelmindDevice(uint8_t address);
#if MM_LOCATIONS_VERSION == 1
// Update device location
MarvelmindDevice *marvelmindUpdateLocation(uint8_t address, MarvelmindDeviceLocation *ppos);
#endif
#if MM_LOCATIONS_VERSION == 2
// Update device location
MarvelmindDevice *marvelmindUpdateLocation(uint8_t address, MarvelmindDeviceLocation2 *ppos);
#endif
// Update distance to device
MarvelmindDevice *marvelmindUpdateDistance(uint8_t addressRx, uint8_t addressTx, uint32_t d);
// Initialize structure of Marvelmind devices
void initMarvelmindDevicesList(); //move to node
#endif // __MARVELMIND_DEVICES_H_
#ifndef __MM_UTILS_H_
#define __MM_UTILS_H_
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include "mm_controller/mm_api.h"
typedef enum
{
modem,
beacon,
hedgehog,
unknown
} MMDeviceType;
void printBoolEnabled(char *prefix, bool v);
int boolAsInt(bool v);
void sleep_ms(int ms);
void trim(char *const a);
MMDeviceType getMMDeviceType(uint8_t deviceType);
void printMMDeviceVersionAndId(MarvelmindDeviceVersion *dv);
void printMMDeviceType(MMDeviceType *dt);
#ifndef WIN32
double getPassedTime(struct timespec *t1, struct timespec *t2);
#endif
#endif // __MM_UTILS_H_
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "mm_controller/mm_devices.h"
typedef struct
{
uint8_t numDevices;
MarvelmindDevice devices[MM_MAX_DEVICES_COUNT];
} MarvelmindDevicesListExt;
static MarvelmindDevicesListExt mmDevList;
#ifdef WIN32
static clock_t prevReadTime;
#else
static struct timespec prevReadTime;
#endif
// Returns true if devices info is same
static bool sameDevice(MarvelmindDeviceInfo *dev1, MarvelmindDeviceInfo *dev2)
{
if (dev1->address != dev2->address)
return false;
if (dev1->isDuplicatedAddress != dev2->isDuplicatedAddress)
return false;
if (dev1->isSleeping != dev2->isSleeping)
return false;
if (dev1->fwVerMajor != dev2->fwVerMajor)
return false;
if (dev1->fwVerMinor != dev2->fwVerMinor)
return false;
if (dev1->fwVerMinor2 != dev2->fwVerMinor2)
return false;
if (dev1->fwVerDeviceType != dev2->fwVerDeviceType)
return false;
if (dev1->fwOptions != dev2->fwOptions)
return false;
if (dev1->flags != dev2->flags)
return false;
return true;
}
// Update device in list
static void updateDevice(uint8_t index, MarvelmindDeviceInfo *info)
{
bool connected = ((info->flags & 0x01) != 0);
mmDevList.devices[index].devConnected = connected;
printf("Device %d updated\r\n", (int)info->address);
if (connected)
{
MarvelmindDeviceVersion version;
if (!mmGetVersionAndId(info->address, &version))
{
printf("Failed read version of device: %d\r\n", (int)info->address);
return;
}
mmDevList.devices[index].version = version;
printMMDeviceVersionAndId(&version);
mmDevList.devices[index].deviceType = getMMDeviceType(version.fwVerDeviceType);
printMMDeviceType(&mmDevList.devices[index].deviceType);
}
else
{
if (info->isSleeping)
{
printf("Device %d is sleeping\r\n", (int)info->address);
}
else
{
printf("Device %d connecting...\r\n", (int)info->address);
}
}
mmDevList.devices[index].info = *info;
}
// Remove device from list
static void removeDevice(uint8_t index)
{
if (mmDevList.numDevices == 0)
return;
printf("Device updated: %d\r\n", (int)mmDevList.devices[index].info.address);
uint8_t i;
mmDevList.numDevices--;
if (mmDevList.numDevices > 0)
{
for (i = index; i < mmDevList.numDevices; i++)
{
mmDevList.devices[i] = mmDevList.devices[i + 1];
}
}
}
// Add device to list
static void addDevice(MarvelmindDeviceInfo *info)
{
if (mmDevList.numDevices >= MM_MAX_DEVICES_COUNT)
return;
updateDevice(mmDevList.numDevices, info);
printf("Device added: %d\r\n", (int)info->address);
#if MM_LOCATIONS_VERSION == 1
MarvelmindDeviceLocation *ppos = &mmDevList.devices[mmDevList.numDevices].pos;
ppos->x_mm = 0;
ppos->y_mm = 0;
ppos->z_mm = 0;
#endif
#if MM_LOCATIONS_VERSION == 2
MarvelmindDeviceLocation2 *ppos = &mmDevList.devices[mmDevList.numDevices].pos;
ppos->x_mm = 0;
ppos->y_mm = 0;
ppos->z_mm = 0;
#endif
mmDevList.numDevices++;
}
// Remove devices not present in new list
static void removeAbsentDevices(MarvelmindDevicesList *pNewList)
{
bool cont;
uint8_t i, j;
for (i = 0; i < mmDevList.numDevices; i++)
{
uint8_t address = mmDevList.devices[i].info.address;
MarvelmindDeviceInfo *info_i = &mmDevList.devices[i].info;
cont = false;
for (j = 0; j < pNewList->numDevices; j++)
{
if (sameDevice(info_i, &pNewList->devices[j]))
{
cont = true;
break;
}
if (address == pNewList->devices[j].address)
{
updateDevice(i, &pNewList->devices[j]);
cont = true;
break;
}
} //for j
if (cont)
continue;
// device not found in new list
removeDevice(i);
} //for i
}
// Add new devices from new list
static void addNewDevices(MarvelmindDevicesList *pNewList)
{
bool cont;
uint8_t i, j;
for (i = 0; i < pNewList->numDevices; i++)
{
uint8_t address = pNewList->devices[i].address;
MarvelmindDeviceInfo *info_i = &pNewList->devices[i];
cont = false;
for (j = 0; j < mmDevList.numDevices; j++)
{
if (sameDevice(info_i, &mmDevList.devices[j].info))
{
cont = true;
break;
}
if (address == mmDevList.devices[j].info.address)
{
updateDevice(j, info_i);
cont = true;
break;
}
} //for j
if (cont)
continue;
// device not found in current list
addDevice(info_i);
} //for i
}
// check lists identity
static bool checkDevicesList(MarvelmindDevicesList *pNewList)
{
uint8_t n = pNewList->numDevices;
if (n == 0)
{
return true;
}
uint8_t i;
for (i = 0; i < n; i++)
{
if (!sameDevice(&pNewList->devices[i], &mmDevList.devices[i].info))
{
if (pNewList->devices[i].address == mmDevList.devices[i].info.address)
{
updateDevice(i, &pNewList->devices[i]);
}
else
{
return false;
}
}
} //for i
return true;
}
// Read Marvelmind devices list from modem
void marvelmindDevicesReadIfNeeded()
{
#ifdef WIN32
clock_t curTime = clock();
double passedSec = ((double)(curTime - prevReadTime)) / CLOCKS_PER_SEC;
#else
struct timespec curTime;
clock_gettime(CLOCK_REALTIME, &curTime);
double passedSec = getPassedTime(&prevReadTime, &curTime);
#endif
if (passedSec < 1.0)
{
return;
}
prevReadTime = curTime;
MarvelmindDevicesList newList;
if (!mmGetDevicesList(&newList))
{
return; // failed read
}
if (newList.numDevices == mmDevList.numDevices)
{
// check lists identity
if (checkDevicesList(&newList))
return;
}
removeAbsentDevices(&newList);
addNewDevices(&newList);
}
// Get device data structure
MarvelmindDevice *getMarvelmindDevice(uint8_t address)
{
uint8_t i;
if (mmDevList.numDevices == 0)
return NULL;
for (i = 0; i < mmDevList.numDevices; i++)
{
if (mmDevList.devices[i].info.address == address)
{
return &mmDevList.devices[i];
}
}
return NULL;
}
#if MM_LOCATIONS_VERSION == 1
// Update device location
MarvelmindDevice *marvelmindUpdateLocation(uint8_t address, MarvelmindDeviceLocation *ppos)
{
MarvelmindDevice *mmDevice;
mmDevice = getMarvelmindDevice(address);
if (mmDevice == NULL)
return NULL;
if (mmDevice->pos.x_mm == ppos->x_mm)
if (mmDevice->pos.y_mm == ppos->y_mm)
if (mmDevice->pos.z_mm == ppos->z_mm)
{
return NULL;
}
mmDevice->pos = *ppos;
return mmDevice;
}
#endif // #if MM_LOCATIONS_VERSION==1
#if MM_LOCATIONS_VERSION == 2
// Update device location
MarvelmindDevice *marvelmindUpdateLocation(uint8_t address, MarvelmindDeviceLocation2 *ppos)
{
MarvelmindDevice *mmDevice;
mmDevice = getMarvelmindDevice(address);
if (mmDevice == NULL)
return NULL;
if (mmDevice->pos.x_mm == ppos->x_mm)
if (mmDevice->pos.y_mm == ppos->y_mm)
if (mmDevice->pos.z_mm == ppos->z_mm)
{
return NULL;
}
mmDevice->pos = *ppos;
return mmDevice;
}
#endif // #if MM_LOCATIONS_VERSION==2
// Update distance to device
MarvelmindDevice *marvelmindUpdateDistance(uint8_t addressRx, uint8_t addressTx, uint32_t d)
{
MarvelmindDevice *mmDeviceRx;
mmDeviceRx = getMarvelmindDevice(addressRx);
if (mmDeviceRx == NULL)
return NULL;
uint32_t dPrev = mmDeviceRx->distances[addressTx].distance_mm;
if (d == dPrev)
{
return NULL;
}
mmDeviceRx->distances[addressTx].distance_mm = d;
return mmDeviceRx;
}
// Initialize structure of Marvelmind devices
void initMarvelmindDevicesList()
{
mmDevList.numDevices = 0;
#ifdef WIN32
prevReadTime = clock();
#else
clock_gettime(CLOCK_REALTIME, &prevReadTime);
#endif
uint8_t i, j;
for (i = 0; i < MM_MAX_DEVICES_COUNT; i++)
{
for (j = 0; j < MM_MAX_DEVICES_COUNT; j++)
{
mmDevList.devices[i].distances[j].distance_mm = 0;
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "mm_controller/mm_utils.h"
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <time.h>
#endif
void printBoolEnabled(char *prefix, bool v)
{
if (v)
{
printf("%s: enabled\r\n", prefix);
}
else
{
printf("%s: disabled\r\n", prefix);
}
}
int boolAsInt(bool v)
{
if (v)
return 1;
else
return 0;
}
// Cross platform sleep function
void sleep_ms(int ms)
{
#ifdef WIN32
Sleep(ms);
#else
usleep(ms * 1000);
#endif // WIN32
}
// Trim unprintable characters from the string
void trim(char *const a)
{
char *p = a, *q = a;
while (isspace(*q))
++q;
while (*q)
*p++ = *q++;
*p = '\0';
while (p > a && isspace(*--p))
*p = '\0';
}
// Returns device type by hardware type id
MMDeviceType getMMDeviceType(uint8_t deviceType)
{
if (mmDeviceIsModem(deviceType))
{
return modem;
}
if (mmDeviceIsBeacon(deviceType))
{
return beacon;
}
if (mmDeviceIsHedgehog(deviceType))
{
return hedgehog;
}
return unknown;
}
// Prints version and ID of the device
void printMMDeviceVersionAndId(MarvelmindDeviceVersion *dv)
{
printf("Version: %d.%02d", (int)dv->fwVerMajor, (int)dv->fwVerMinor);
printf("%01d", (int)dv->fwVerMinor2);
//if (dv->fwVerMinor2 != 0) {
// printf("%c",(char) (dv->fwVerMinor2+'a' - 1));
//}
printf(".%d CPU ID=%06x", (int)dv->fwVerDeviceType, dv->cpuId);
printf("\r\n");
}
// Prints device type
void printMMDeviceType(MMDeviceType *dt)
{
switch (*dt)
{
case modem:
{
printf("Device is modem \r\n");
break;
}
case beacon:
{
printf("Device is beacon \r\n");
break;
}
case hedgehog:
{
printf("Device is hedgehog \r\n");
break;
}
default:
{
printf("Unknown device type \r\n");
break;
}
}
}
#ifndef WIN32
double getPassedTime(struct timespec *t1, struct timespec *t2)
{
double t1_fs = t1->tv_nsec / 1000000000.0;
double t2_fs = t2->tv_nsec / 1000000000.0;
double dt_sec = t2->tv_sec - t1->tv_sec;
return (dt_sec) + (t2_fs - t1_fs);
}
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment