lights: use driver base class

This commit is contained in:
Beat Küng 2020-03-10 13:25:37 +01:00 committed by Daniel Agar
parent 692d262e0e
commit ec2de33547
14 changed files with 238 additions and 375 deletions

View File

@ -68,13 +68,13 @@ unset BOARD_RC_DEFAULTS
# #
# Start system state indicator. # Start system state indicator.
# #
rgbled start rgbled start -X
rgbled_ncp5623c start rgbled_ncp5623c start -X
rgbled_pwm start rgbled_pwm start
if param greater LIGHT_EN_BLINKM 0 if param greater LIGHT_EN_BLINKM 0
then then
if blinkm start if blinkm start -X
then then
blinkm systemstate blinkm systemstate
fi fi

View File

@ -198,13 +198,13 @@ else
# #
# Start system state indicator. # Start system state indicator.
# #
rgbled start rgbled start -X
rgbled_ncp5623c start rgbled_ncp5623c start -X
rgbled_pwm start rgbled_pwm start
if param greater LIGHT_EN_BLINKM 0 if param greater LIGHT_EN_BLINKM 0
then then
if blinkm start if blinkm start -X
then then
blinkm systemstate blinkm systemstate
fi fi

View File

@ -9,7 +9,7 @@ set BOARD_RC /etc/init.d/rc.board
uorb start uorb start
if rgbled start if rgbled start -X
then then
led_control on -c blue led_control on -c blue
fi fi

View File

@ -3,6 +3,8 @@
# PX4 FMUv2 specific board sensors init # PX4 FMUv2 specific board sensors init
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
rgbled start -I
adc start adc start
# External I2C bus # External I2C bus

View File

@ -3,6 +3,8 @@
# PX4 FMUv3 specific board sensors init # PX4 FMUv3 specific board sensors init
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
rgbled start -I
adc start adc start
# External I2C bus # External I2C bus

View File

@ -3,6 +3,8 @@
# PX4 FMUv4pro specific board sensors init # PX4 FMUv4pro specific board sensors init
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
rgbled start -I
adc start adc start
# Internal SPI bus ICM-20608-G # Internal SPI bus ICM-20608-G

View File

@ -15,7 +15,7 @@ fi
if param compare SYS_AUTOSTART 6002 if param compare SYS_AUTOSTART 6002
then then
# GPS LED # GPS LED
rgbled_ncp5623c start -a 0x38 rgbled_ncp5623c start -X -a 0x38
#mpu6000 -s -R 4 -T 20608 start #mpu6000 -s -R 4 -T 20608 start
mpu9250 -R 4 start mpu9250 -R 4 start
@ -34,7 +34,7 @@ fi
if param compare SYS_AUTOSTART 4071 if param compare SYS_AUTOSTART 4071
then then
# IFO GPS LED # IFO GPS LED
rgbled_ncp5623c start -a 0x38 rgbled_ncp5623c start -X -a 0x38
# IFO rgb LED # IFO rgb LED
pca9685 start pca9685 start

View File

@ -55,7 +55,7 @@ mpu9250 start
gps start -d /dev/ttyS2 -s -p ubx gps start -d /dev/ttyS2 -s -p ubx
#rgbled start -b 1 #rgbled start -I -b 1
adc start adc start
battery_status start battery_status start

View File

@ -55,7 +55,7 @@ mpu9250 start
gps start -d /dev/ttyS2 -s -p ubx gps start -d /dev/ttyS2 -s -p ubx
#rgbled start -b 1 #rgbled start -I -b 1
adc start adc start
battery_status start battery_status start

View File

@ -55,7 +55,3 @@ static_assert(led_control_s::ORB_QUEUE_LENGTH >= BOARD_MAX_LEDS, "led_control_s:
#error "BOARD_MAX_LEDS too large. You need to change the led_mask type in the led_control uorb topic (and where it's used)" #error "BOARD_MAX_LEDS too large. You need to change the led_mask type in the led_control uorb topic (and where it's used)"
#endif #endif
// Legacy paths - 2 are need to allow both pwm and i2c drviers to co-exist
#define RGBLED0_DEVICE_PATH "/dev/rgbled0" // Primary RGB LED on i2c
#define RGBLED1_DEVICE_PATH "/dev/rgbled1" // Primary RGB LED(NCP5623C) on i2c
#define RGBLED_PWM0_DEVICE_PATH "/dev/rgbled_pwm0" // Secondary RGB LED on PWM

View File

@ -138,6 +138,9 @@
#define DRV_DIST_DEVTYPE_VL53LXX 0x76 #define DRV_DIST_DEVTYPE_VL53LXX 0x76
#define DRV_POWER_DEVTYPE_INA226 0x77 #define DRV_POWER_DEVTYPE_INA226 0x77
#define DRV_POWER_DEVTYPE_VOXLPM 0x78 #define DRV_POWER_DEVTYPE_VOXLPM 0x78
#define DRV_LED_DEVTYPE_BLINKM 0x79
#define DRV_LED_DEVTYPE_RGBLED 0x7a
#define DRV_LED_DEVTYPE_RGBLED_NCP5623C 0x7b
#define DRV_DEVTYPE_UNUSED 0xff #define DRV_DEVTYPE_UNUSED 0xff

View File

@ -99,7 +99,8 @@
#include <drivers/device/i2c.h> #include <drivers/device/i2c.h>
#include <drivers/drv_blinkm.h> #include <drivers/drv_blinkm.h>
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp> #include <px4_platform_common/i2c_spi_buses.h>
#include <px4_platform_common/module.h>
#include <uORB/Subscription.hpp> #include <uORB/Subscription.hpp>
#include <uORB/topics/actuator_armed.h> #include <uORB/topics/actuator_armed.h>
#include <uORB/topics/battery_status.h> #include <uORB/topics/battery_status.h>
@ -113,12 +114,17 @@ static const int LED_OFFTIME = 120;
static const int LED_BLINK = 1; static const int LED_BLINK = 1;
static const int LED_NOBLINK = 0; static const int LED_NOBLINK = 0;
class BlinkM : public device::I2C, public px4::ScheduledWorkItem class BlinkM : public device::I2C, public I2CSPIDriver<BlinkM>
{ {
public: public:
BlinkM(int bus, int blinkm); BlinkM(I2CSPIBusOption bus_option, const int bus, int bus_frequency, const int address);
virtual ~BlinkM() = default; virtual ~BlinkM() = default;
static I2CSPIDriverBase *instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
int runtime_instance);
static void print_usage();
void custom_method(const BusCLIArguments &cli) override;
virtual int init(); virtual int init();
virtual int probe(); virtual int probe();
@ -127,6 +133,7 @@ public:
static const char *const script_names[]; static const char *const script_names[];
void RunImpl();
private: private:
enum ScriptID { enum ScriptID {
USER = 0, USER = 0,
@ -196,8 +203,6 @@ private:
void setLEDColor(int ledcolor); void setLEDColor(int ledcolor);
void Run() override;
int set_rgb(uint8_t r, uint8_t g, uint8_t b); int set_rgb(uint8_t r, uint8_t g, uint8_t b);
int fade_rgb(uint8_t r, uint8_t g, uint8_t b); int fade_rgb(uint8_t r, uint8_t g, uint8_t b);
@ -221,12 +226,6 @@ private:
int get_firmware_version(uint8_t version[2]); int get_firmware_version(uint8_t version[2]);
}; };
/* for now, we only support one BlinkM */
namespace
{
BlinkM *g_blinkm;
}
/* list of script names, must match script ID numbers */ /* list of script names, must match script ID numbers */
const char *const BlinkM::script_names[] = { const char *const BlinkM::script_names[] = {
"USER", "USER",
@ -254,9 +253,9 @@ const char *const BlinkM::script_names[] = {
extern "C" __EXPORT int blinkm_main(int argc, char *argv[]); extern "C" __EXPORT int blinkm_main(int argc, char *argv[]);
BlinkM::BlinkM(int bus, int blinkm) : BlinkM::BlinkM(I2CSPIBusOption bus_option, const int bus, int bus_frequency, const int address) :
I2C("blinkm", BLINKM0_DEVICE_PATH, bus, blinkm, 100000), I2C("blinkm", BLINKM0_DEVICE_PATH, bus, address, bus_frequency),
ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(get_device_id())), I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(get_device_id()), bus_option, bus),
led_color_1(LED_OFF), led_color_1(LED_OFF),
led_color_2(LED_OFF), led_color_2(LED_OFF),
led_color_3(LED_OFF), led_color_3(LED_OFF),
@ -382,7 +381,7 @@ BlinkM::ioctl(device::file_t *filp, int cmd, unsigned long arg)
void void
BlinkM::Run() BlinkM::RunImpl()
{ {
if (led_thread_ready == true) { if (led_thread_ready == true) {
if (!detected_cells_blinked) { if (!detected_cells_blinked) {
@ -924,111 +923,117 @@ BlinkM::get_firmware_version(uint8_t version[2])
return transfer(&msg, sizeof(msg), version, 2); return transfer(&msg, sizeof(msg), version, 2);
} }
void blinkm_usage(); void
BlinkM::print_usage()
void blinkm_usage()
{ {
warnx("missing command: try 'start', 'systemstate', 'ledoff', 'list' or a script name {options}"); PRINT_MODULE_USAGE_NAME("blinkm", "driver");
warnx("options:"); PRINT_MODULE_USAGE_COMMAND("start");
warnx("\t-b --bus i2cbus (3)"); PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, false);
warnx("\t-a --blinkmaddr blinkmaddr (9)"); PRINT_MODULE_USAGE_PARAMS_I2C_ADDRESS(9);
PRINT_MODULE_USAGE_COMMAND("systemstate");
PRINT_MODULE_USAGE_COMMAND("ledoff");
PRINT_MODULE_USAGE_COMMAND("list");
PRINT_MODULE_USAGE_COMMAND("script");
PRINT_MODULE_USAGE_PARAM_STRING('n', nullptr, "<file>", "Script file name", false);
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
} }
int I2CSPIDriverBase *BlinkM::instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
blinkm_main(int argc, char *argv[]) int runtime_instance)
{ {
BlinkM *instance = new BlinkM(iterator.configuredBusOption(), iterator.bus(), cli.bus_frequency, cli.i2c_address);
int i2cdevice = PX4_I2C_BUS_EXPANSION; if (instance == nullptr) {
int blinkmadr = 9; PX4_ERR("alloc failed");
return nullptr;
int x;
if (argc < 2) {
blinkm_usage();
return 1;
} }
for (x = 1; x < argc; x++) { if (instance->init() != PX4_OK) {
if (strcmp(argv[x], "-b") == 0 || strcmp(argv[x], "--bus") == 0) { delete instance;
if (argc > x + 1) { return nullptr;
i2cdevice = atoi(argv[x + 1]); }
}
return instance;
}
void
BlinkM::custom_method(const BusCLIArguments &cli)
{
setMode(cli.custom1);
}
extern "C" __EXPORT int blinkm_main(int argc, char *argv[])
{
int ch;
using ThisDriver = BlinkM;
BusCLIArguments cli{true, false};
cli.default_i2c_frequency = 100000;
cli.i2c_address = 9;
const char *script = nullptr;
while ((ch = cli.getopt(argc, argv, "n:")) != EOF) {
switch (ch) {
case 'n':
script = cli.optarg();
break;
} }
if (strcmp(argv[x], "-a") == 0 || strcmp(argv[x], "--blinkmaddr") == 0) {
if (argc > x + 1) {
blinkmadr = atoi(argv[x + 1]);
}
}
} }
if (!strcmp(argv[1], "start")) { const char *verb = cli.optarg();
if (g_blinkm != nullptr) {
warnx("already started");
return 1;
}
g_blinkm = new BlinkM(i2cdevice, blinkmadr); if (!verb) {
ThisDriver::print_usage();
if (g_blinkm == nullptr) { return -1;
warnx("new failed");
return 1;
}
if (OK != g_blinkm->init()) {
delete g_blinkm;
g_blinkm = nullptr;
warnx("init failed");
return 1;
}
return 0;
} }
BusInstanceIterator iterator(MODULE_NAME, cli, DRV_LED_DEVTYPE_BLINKM);
if (g_blinkm == nullptr) { if (!strcmp(verb, "start")) {
PX4_ERR("not started"); return ThisDriver::module_start(cli, iterator);
blinkm_usage();
return 0;
} }
if (!strcmp(argv[1], "systemstate")) { if (!strcmp(verb, "stop")) {
g_blinkm->setMode(1); return ThisDriver::module_stop(iterator);
return 0;
} }
if (!strcmp(argv[1], "ledoff")) { if (!strcmp(verb, "status")) {
g_blinkm->setMode(0); return ThisDriver::module_status(iterator);
return 0;
} }
if (!strcmp(verb, "systemstate")) {
cli.custom1 = 1;
return ThisDriver::module_custom_method(cli, iterator);
}
if (!strcmp(argv[1], "list")) { if (!strcmp(verb, "ledoff")) {
cli.custom1 = 0;
return ThisDriver::module_custom_method(cli, iterator);
}
if (!strcmp(verb, "list")) {
for (unsigned i = 0; BlinkM::script_names[i] != nullptr; i++) { for (unsigned i = 0; BlinkM::script_names[i] != nullptr; i++) {
PX4_ERR(" %s", BlinkM::script_names[i]); PX4_INFO(" %s", BlinkM::script_names[i]);
} }
PX4_ERR(" <html color number>"); PX4_INFO(" <html color number>");
return 0; return 0;
} }
/* things that require access to the device */ if (!strcmp(verb, "script") && script) {
int fd = px4_open(BLINKM0_DEVICE_PATH, 0); /* things that require access to the device */
int fd = px4_open(BLINKM0_DEVICE_PATH, 0);
if (fd < 0) { if (fd < 0) {
warn("can't open BlinkM device"); PX4_ERR("can't open BlinkM device");
return 1; return 1;
} }
g_blinkm->setMode(0); px4_ioctl(fd, BLINKM_PLAY_SCRIPT_NAMED, (unsigned long)script);
if (px4_ioctl(fd, BLINKM_PLAY_SCRIPT_NAMED, (unsigned long)argv[1]) == OK) { px4_close(fd);
return 0; return 0;
} }
px4_close(fd); ThisDriver::print_usage();
return -1;
blinkm_usage();
return 0;
} }

View File

@ -46,7 +46,8 @@
#include <lib/led/led.h> #include <lib/led/led.h>
#include <lib/parameters/param.h> #include <lib/parameters/param.h>
#include <px4_platform_common/getopt.h> #include <px4_platform_common/getopt.h>
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp> #include <px4_platform_common/i2c_spi_buses.h>
#include <px4_platform_common/module.h>
#include <uORB/Subscription.hpp> #include <uORB/Subscription.hpp>
#include <uORB/topics/parameter_update.h> #include <uORB/topics/parameter_update.h>
@ -60,16 +61,23 @@
#define SETTING_NOT_POWERSAVE 0x01 /**< power-save mode not off */ #define SETTING_NOT_POWERSAVE 0x01 /**< power-save mode not off */
#define SETTING_ENABLE 0x02 /**< on */ #define SETTING_ENABLE 0x02 /**< on */
class RGBLED : public device::I2C, public px4::ScheduledWorkItem class RGBLED : public device::I2C, public I2CSPIDriver<RGBLED>
{ {
public: public:
RGBLED(int bus, int rgbled); RGBLED(I2CSPIBusOption bus_option, const int bus, int bus_frequency, const int address);
virtual ~RGBLED(); virtual ~RGBLED() = default;
virtual int init(); static I2CSPIDriverBase *instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
virtual int probe(); int runtime_instance);
int status(); static void print_usage();
int init() override;
int probe() override;
void RunImpl();
protected:
void print_status() override;
private: private:
float _brightness{1.0f}; float _brightness{1.0f};
@ -78,15 +86,12 @@ private:
uint8_t _r{0}; uint8_t _r{0};
uint8_t _g{0}; uint8_t _g{0};
uint8_t _b{0}; uint8_t _b{0};
volatile bool _running{false};
volatile bool _should_run{true};
bool _leds_enabled{true}; bool _leds_enabled{true};
uORB::Subscription _parameter_update_sub{ORB_ID(parameter_update)}; uORB::Subscription _parameter_update_sub{ORB_ID(parameter_update)};
LedController _led_controller; LedController _led_controller;
void Run() override;
int send_led_enable(bool enable); int send_led_enable(bool enable);
int send_led_rgb(); int send_led_rgb();
@ -94,30 +99,10 @@ private:
void update_params(); void update_params();
}; };
/* for now, we only support one RGBLED */ RGBLED::RGBLED(I2CSPIBusOption bus_option, const int bus, int bus_frequency, const int address) :
namespace I2C("rgbled", nullptr, bus, address, bus_frequency),
I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(get_device_id()), bus_option, bus)
{ {
RGBLED *g_rgbled = nullptr;
}
void rgbled_usage();
extern "C" __EXPORT int rgbled_main(int argc, char *argv[]);
RGBLED::RGBLED(int bus, int rgbled) :
I2C("rgbled", RGBLED0_DEVICE_PATH, bus, rgbled, 100000),
ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(get_device_id()))
{
}
RGBLED::~RGBLED()
{
_should_run = false;
int counter = 0;
while (_running && ++counter < 10) {
px4_usleep(100000);
}
} }
int int
@ -135,8 +120,6 @@ RGBLED::init()
update_params(); update_params();
_running = true;
// kick off work queue // kick off work queue
ScheduleNow(); ScheduleNow();
@ -173,8 +156,8 @@ RGBLED::probe()
return ret; return ret;
} }
int void
RGBLED::status() RGBLED::print_status()
{ {
bool on, powersave; bool on, powersave;
uint8_t r, g, b; uint8_t r, g, b;
@ -183,27 +166,17 @@ RGBLED::status()
if (ret == OK) { if (ret == OK) {
/* we don't care about power-save mode */ /* we don't care about power-save mode */
DEVICE_LOG("state: %s", on ? "ON" : "OFF"); PX4_INFO("state: %s", on ? "ON" : "OFF");
DEVICE_LOG("red: %u, green: %u, blue: %u", (unsigned)r, (unsigned)g, (unsigned)b); PX4_INFO("red: %u, green: %u, blue: %u", (unsigned)r, (unsigned)g, (unsigned)b);
} else { } else {
PX4_WARN("failed to read led"); PX4_WARN("failed to read led");
} }
return ret;
} }
/**
* Main loop function
*/
void void
RGBLED::Run() RGBLED::RunImpl()
{ {
if (!_should_run) {
_running = false;
return;
}
// check for parameter updates // check for parameter updates
if (_parameter_update_sub.updated()) { if (_parameter_update_sub.updated()) {
// clear update // clear update
@ -349,113 +322,61 @@ RGBLED::update_params()
} }
void void
rgbled_usage() RGBLED::print_usage()
{ {
PX4_INFO("missing command: try 'start', 'status', 'stop'"); PRINT_MODULE_USAGE_NAME("rgbled", "driver");
PX4_INFO("options:"); PRINT_MODULE_USAGE_COMMAND("start");
PX4_INFO(" -b i2cbus (%d)", PX4_I2C_BUS_LED); PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, false);
PX4_INFO(" -a addr (0x%x)", ADDR); PRINT_MODULE_USAGE_PARAMS_I2C_ADDRESS(0x55);
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
} }
int I2CSPIDriverBase *RGBLED::instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
rgbled_main(int argc, char *argv[]) int runtime_instance)
{ {
int i2cdevice = -1; RGBLED *instance = new RGBLED(iterator.configuredBusOption(), iterator.bus(), cli.bus_frequency, cli.i2c_address);
int rgbledadr = ADDR; /* 7bit */
int ch; if (instance == nullptr) {
PX4_ERR("alloc failed");
/* jump over start/off/etc and look at options first */ return nullptr;
int myoptind = 1;
const char *myoptarg = nullptr;
while ((ch = px4_getopt(argc, argv, "a:b:", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
case 'a':
rgbledadr = strtol(myoptarg, nullptr, 0);
break;
case 'b':
i2cdevice = strtol(myoptarg, nullptr, 0);
break;
default:
rgbled_usage();
return 1;
}
} }
if (myoptind >= argc) { if (instance->init() != PX4_OK) {
rgbled_usage(); delete instance;
return 1; return nullptr;
} }
const char *verb = argv[myoptind]; return instance;
}
extern "C" __EXPORT int rgbled_main(int argc, char *argv[])
{
using ThisDriver = RGBLED;
BusCLIArguments cli{true, false};
cli.default_i2c_frequency = 100000;
cli.i2c_address = ADDR;
const char *verb = cli.parseDefaultArguments(argc, argv);
if (!verb) {
ThisDriver::print_usage();
return -1;
}
BusInstanceIterator iterator(MODULE_NAME, cli, DRV_LED_DEVTYPE_RGBLED);
if (!strcmp(verb, "start")) { if (!strcmp(verb, "start")) {
if (g_rgbled != nullptr) { return ThisDriver::module_start(cli, iterator);
PX4_WARN("already started");
return 1;
}
if (i2cdevice == -1) {
// try the external bus first
i2cdevice = PX4_I2C_BUS_EXPANSION;
g_rgbled = new RGBLED(PX4_I2C_BUS_EXPANSION, rgbledadr);
if (g_rgbled != nullptr && OK != g_rgbled->init()) {
delete g_rgbled;
g_rgbled = nullptr;
}
if (g_rgbled == nullptr) {
// fall back to default bus
if (PX4_I2C_BUS_LED == PX4_I2C_BUS_EXPANSION) {
PX4_WARN("no RGB led on bus #%d", i2cdevice);
return 1;
}
i2cdevice = PX4_I2C_BUS_LED;
}
}
if (g_rgbled == nullptr) {
g_rgbled = new RGBLED(i2cdevice, rgbledadr);
if (g_rgbled == nullptr) {
PX4_WARN("alloc failed");
return 1;
}
if (OK != g_rgbled->init()) {
delete g_rgbled;
g_rgbled = nullptr;
PX4_WARN("no RGB led on bus #%d", i2cdevice);
return 1;
}
}
return 0;
}
/* need the driver past this point */
if (g_rgbled == nullptr) {
PX4_WARN("not started");
rgbled_usage();
return 1;
}
if (!strcmp(verb, "status")) {
g_rgbled->status();
return 0;
} }
if (!strcmp(verb, "stop")) { if (!strcmp(verb, "stop")) {
delete g_rgbled; return ThisDriver::module_stop(iterator);
g_rgbled = nullptr;
return 0;
} }
rgbled_usage(); if (!strcmp(verb, "status")) {
return 1; return ThisDriver::module_status(iterator);
}
ThisDriver::print_usage();
return -1;
} }

View File

@ -45,7 +45,8 @@
#include <lib/led/led.h> #include <lib/led/led.h>
#include <lib/parameters/param.h> #include <lib/parameters/param.h>
#include <px4_platform_common/getopt.h> #include <px4_platform_common/getopt.h>
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp> #include <px4_platform_common/i2c_spi_buses.h>
#include <px4_platform_common/module.h>
#include <uORB/Subscription.hpp> #include <uORB/Subscription.hpp>
#include <uORB/topics/parameter_update.h> #include <uORB/topics/parameter_update.h>
@ -61,14 +62,20 @@
#define NCP5623_LED_OFF 0x00 /**< off */ #define NCP5623_LED_OFF 0x00 /**< off */
class RGBLED_NPC5623C : public device::I2C, public px4::ScheduledWorkItem class RGBLED_NPC5623C : public device::I2C, public I2CSPIDriver<RGBLED_NPC5623C>
{ {
public: public:
RGBLED_NPC5623C(int bus, int rgbled); RGBLED_NPC5623C(I2CSPIBusOption bus_option, const int bus, int bus_frequency, const int address);
virtual ~RGBLED_NPC5623C(); virtual ~RGBLED_NPC5623C() = default;
virtual int init(); static I2CSPIDriverBase *instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
virtual int probe(); int runtime_instance);
static void print_usage();
int init() override;
int probe() override;
void RunImpl();
private: private:
@ -86,38 +93,16 @@ private:
LedController _led_controller; LedController _led_controller;
void Run() override;
int send_led_rgb(); int send_led_rgb();
void update_params(); void update_params();
int write(uint8_t reg, uint8_t data); int write(uint8_t reg, uint8_t data);
}; };
/* for now, we only support one RGBLED */ RGBLED_NPC5623C::RGBLED_NPC5623C(I2CSPIBusOption bus_option, const int bus, int bus_frequency, const int address) :
namespace I2C("rgbled1", nullptr, bus, address, bus_frequency),
I2CSPIDriver(MODULE_NAME, px4::device_bus_to_wq(get_device_id()), bus_option, bus)
{ {
RGBLED_NPC5623C *g_rgbled = nullptr;
}
void rgbled_ncp5623c_usage();
extern "C" __EXPORT int rgbled_ncp5623c_main(int argc, char *argv[]);
RGBLED_NPC5623C::RGBLED_NPC5623C(int bus, int rgbled) :
I2C("rgbled1", RGBLED1_DEVICE_PATH, bus, rgbled, 100000),
ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(get_device_id()))
{
}
RGBLED_NPC5623C::~RGBLED_NPC5623C()
{
_should_run = false;
int counter = 0;
while (_running && ++counter < 10) {
px4_usleep(100000);
}
} }
int int
@ -157,17 +142,9 @@ RGBLED_NPC5623C::probe()
return write(NCP5623_LED_CURRENT, 0x00); return write(NCP5623_LED_CURRENT, 0x00);
} }
/**
* Main loop function
*/
void void
RGBLED_NPC5623C::Run() RGBLED_NPC5623C::RunImpl()
{ {
if (!_should_run) {
_running = false;
return;
}
// check for parameter updates // check for parameter updates
if (_parameter_update_sub.updated()) { if (_parameter_update_sub.updated()) {
// clear update // clear update
@ -262,108 +239,63 @@ RGBLED_NPC5623C::update_params()
} }
void void
rgbled_ncp5623c_usage() RGBLED_NPC5623C::print_usage()
{ {
PX4_INFO("missing command: try 'start', 'stop'"); PRINT_MODULE_USAGE_NAME("rgbled", "driver");
PX4_INFO("options:"); PRINT_MODULE_USAGE_COMMAND("start");
PX4_INFO(" -b i2cbus (%d)", PX4_I2C_BUS_LED); PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, false);
PX4_INFO(" -a addr (0x%x)", ADDR); PRINT_MODULE_USAGE_PARAMS_I2C_ADDRESS(0x39);
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
} }
int I2CSPIDriverBase *RGBLED_NPC5623C::instantiate(const BusCLIArguments &cli, const BusInstanceIterator &iterator,
rgbled_ncp5623c_main(int argc, char *argv[]) int runtime_instance)
{ {
int i2cdevice = -1; RGBLED_NPC5623C *instance = new RGBLED_NPC5623C(iterator.configuredBusOption(), iterator.bus(), cli.bus_frequency,
int rgbledadr = ADDR; /* 7bit */ cli.i2c_address);
int ch; if (instance == nullptr) {
PX4_ERR("alloc failed");
/* jump over start/off/etc and look at options first */ return nullptr;
int myoptind = 1;
const char *myoptarg = nullptr;
while ((ch = px4_getopt(argc, argv, "a:b:", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
case 'a':
rgbledadr = strtol(myoptarg, nullptr, 0);
break;
case 'b':
i2cdevice = strtol(myoptarg, nullptr, 0);
break;
default:
rgbled_ncp5623c_usage();
return 1;
}
} }
if (myoptind >= argc) { if (instance->init() != PX4_OK) {
rgbled_ncp5623c_usage(); delete instance;
return 1; return nullptr;
} }
const char *verb = argv[myoptind]; return instance;
}
extern "C" __EXPORT int rgbled_ncp5623c_main(int argc, char *argv[])
{
using ThisDriver = RGBLED_NPC5623C;
BusCLIArguments cli{true, false};
cli.default_i2c_frequency = 100000;
cli.i2c_address = ADDR;
const char *verb = cli.parseDefaultArguments(argc, argv);
if (!verb) {
ThisDriver::print_usage();
return -1;
}
BusInstanceIterator iterator(MODULE_NAME, cli,
DRV_LED_DEVTYPE_RGBLED_NCP5623C);
if (!strcmp(verb, "start")) { if (!strcmp(verb, "start")) {
if (g_rgbled != nullptr) { return ThisDriver::module_start(cli, iterator);
PX4_WARN("already started");
return 1;
}
if (i2cdevice == -1) {
// try the external bus first
i2cdevice = PX4_I2C_BUS_EXPANSION;
g_rgbled = new RGBLED_NPC5623C(PX4_I2C_BUS_EXPANSION, rgbledadr);
if (g_rgbled != nullptr && OK != g_rgbled->init()) {
delete g_rgbled;
g_rgbled = nullptr;
}
if (g_rgbled == nullptr) {
// fall back to default bus
if (PX4_I2C_BUS_LED == PX4_I2C_BUS_EXPANSION) {
PX4_WARN("no RGB led on bus #%d", i2cdevice);
return 1;
}
i2cdevice = PX4_I2C_BUS_LED;
}
}
if (g_rgbled == nullptr) {
g_rgbled = new RGBLED_NPC5623C(i2cdevice, rgbledadr);
if (g_rgbled == nullptr) {
PX4_WARN("alloc failed");
return 1;
}
if (OK != g_rgbled->init()) {
delete g_rgbled;
g_rgbled = nullptr;
PX4_WARN("no RGB led on bus #%d", i2cdevice);
return 1;
}
}
return 0;
}
/* need the driver past this point */
if (g_rgbled == nullptr) {
PX4_WARN("not started");
rgbled_ncp5623c_usage();
return 1;
} }
if (!strcmp(verb, "stop")) { if (!strcmp(verb, "stop")) {
delete g_rgbled; return ThisDriver::module_stop(iterator);
g_rgbled = nullptr;
return 0;
} }
rgbled_ncp5623c_usage(); if (!strcmp(verb, "status")) {
return 1; return ThisDriver::module_status(iterator);
}
ThisDriver::print_usage();
return -1;
} }