Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions sensors/mag/lis2mdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,45 @@ static int lis2mdl_hwSetup(lis2mdl_ctx_t *ctx)
}


static void lis2mdl_threadPublish(void *data)
int lis2mdl_read(const sensor_info_t *info, const sensor_event_t **evt)
{
int err;
sensor_info_t *info = (sensor_info_t *)data;
lis2mdl_ctx_t *ctx = info->ctx;
uint8_t ibuf[SENSOR_OUTPUT_SIZE] = { 0 };
const uint8_t obuf = REG_DATA_OUT | SPI_READ_BIT;
int err;

err = sensorsspi_xfer(&ctx->spiCtx, &ctx->spiSS, &obuf, sizeof(obuf), ibuf, sizeof(ibuf), sizeof(obuf));

if (err < 0) {
fprintf(stderr, "lis2mdl read: %d\n", err);
return -1;
}

gettime(&(ctx->evt.timestamp), NULL);
ctx->evt.mag.magX = translateMag(ibuf[1], ibuf[0]);
ctx->evt.mag.magY = -translateMag(ibuf[3], ibuf[2]); /* minus accounts for non right-handness of measurement */
ctx->evt.mag.magZ = translateMag(ibuf[5], ibuf[4]);

if (evt != NULL) {
*evt = &ctx->evt;
}

return 1;
}


static void lis2mdl_threadPublish(void *data)
{
sensor_info_t *info = (sensor_info_t *)data;

while (1) {
usleep(100 * 1000);

err = sensorsspi_xfer(&ctx->spiCtx, &ctx->spiSS, &obuf, sizeof(obuf), ibuf, sizeof(ibuf), sizeof(obuf));

if (err >= 0) {
gettime(&(ctx->evt.timestamp), NULL);
ctx->evt.mag.magX = translateMag(ibuf[1], ibuf[0]);
ctx->evt.mag.magY = -translateMag(ibuf[3], ibuf[2]); /* minus accounts for non right-handness of measurement */
ctx->evt.mag.magZ = translateMag(ibuf[5], ibuf[4]);
sensors_publish(info->id, &ctx->evt);
if (lis2mdl_read(info, NULL) < 0) {
continue;
}

sensors_publish(info->id, &ctx->evt);
}
}

Expand All @@ -169,6 +188,14 @@ static int lis2mdl_start(sensor_info_t *info)
}


int lis2mdl_dealloc(sensor_info_t *info)
{
free((lis2mdl_ctx_t *)info->ctx);

return 0;
}


static int lis2mdl_alloc(sensor_info_t *info, const char *args)
{
lis2mdl_ctx_t *ctx;
Expand Down Expand Up @@ -220,7 +247,9 @@ void __attribute__((constructor)) lis2mdl_mag_register(void)
static sensor_drv_t sensor = {
.name = "lis2mdl",
.alloc = lis2mdl_alloc,
.start = lis2mdl_start
.dealloc = lis2mdl_dealloc,
.start = lis2mdl_start,
.read = lis2mdl_read
};

sensors_register(&sensor);
Expand Down
Loading