2017-07-28 15:14:58 +02:00
|
|
|
/****************************************************************************
|
2022-03-07 15:45:34 +01:00
|
|
|
* apps/examples/powermonitor/powermonitor_main.c
|
2017-07-28 15:14:58 +02:00
|
|
|
*
|
2022-03-07 15:22:16 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2017-07-28 15:14:58 +02:00
|
|
|
*
|
2022-03-07 15:22:16 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-07-28 15:14:58 +02:00
|
|
|
*
|
2022-03-07 15:22:16 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2017-07-28 15:14:58 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-03-07 15:45:34 +01:00
|
|
|
/****************************************************************************
|
2017-07-28 17:14:47 +02:00
|
|
|
* Included Files
|
2022-03-07 15:45:34 +01:00
|
|
|
****************************************************************************/
|
2017-07-28 17:14:47 +02:00
|
|
|
|
2017-07-28 15:14:58 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fixedmath.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <nuttx/sensors/ltc4151.h>
|
|
|
|
|
2022-03-07 15:45:34 +01:00
|
|
|
/****************************************************************************
|
2017-07-28 17:14:47 +02:00
|
|
|
* Pre-processor Definitions
|
2022-03-07 15:45:34 +01:00
|
|
|
****************************************************************************/
|
2017-07-28 15:14:58 +02:00
|
|
|
|
|
|
|
#define DECIMAL_PLACES3(x) abs(((int)(((x)-((int)x))*1000)))
|
|
|
|
|
2022-03-07 15:45:34 +01:00
|
|
|
/****************************************************************************
|
2017-07-28 17:14:47 +02:00
|
|
|
* Public Functions
|
2022-03-07 15:45:34 +01:00
|
|
|
****************************************************************************/
|
2017-07-28 17:14:47 +02:00
|
|
|
|
2017-07-28 15:14:58 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2017-07-28 17:14:47 +02:00
|
|
|
static FAR const char *pwrmntr_dev = "/dev/pwrmntr0";
|
|
|
|
float float_current;
|
|
|
|
float float_voltage;
|
2017-07-28 15:14:58 +02:00
|
|
|
ltc4151_t ltc;
|
2017-07-28 17:14:47 +02:00
|
|
|
int pwrmntr_fd;
|
|
|
|
int samples;
|
2017-07-28 15:14:58 +02:00
|
|
|
int sample;
|
2017-07-28 17:14:47 +02:00
|
|
|
|
|
|
|
samples = 1;
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
samples = atoi(argv[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
pwrmntr_fd = open(pwrmntr_dev, O_RDONLY);
|
|
|
|
if (pwrmntr_fd < 0)
|
|
|
|
{
|
|
|
|
printf("Failed to open %s: %d\n", pwrmntr_dev, errno);
|
2017-07-28 15:14:58 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2017-07-28 17:14:47 +02:00
|
|
|
|
|
|
|
for (sample = 0; sample < samples; ++sample)
|
|
|
|
{
|
|
|
|
if (read(pwrmntr_fd, <c, sizeof(ltc)) < 0)
|
|
|
|
{
|
|
|
|
printf("Failed to read from %s: %d\n", pwrmntr_dev, errno);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
float_current = b16tof(ltc.current);
|
|
|
|
float_voltage = b16tof(ltc.voltage);
|
|
|
|
|
|
|
|
printf("Current: %d.%03dmA - Voltage: %d.%03dV\n",
|
|
|
|
(int)float_current, DECIMAL_PLACES3(float_current),
|
|
|
|
(int)float_voltage, DECIMAL_PLACES3(float_voltage));
|
|
|
|
}
|
2017-07-28 15:14:58 +02:00
|
|
|
|
|
|
|
close(pwrmntr_fd);
|
|
|
|
return 0;
|
|
|
|
}
|