Dawn Framework 1.0
Universal data acquisition framework for embedded systems
rgbled.cxx
1// dawn/src/io/rgbled.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/rgbled.hxx"
7
8#include <cstdio>
9
10using namespace dawn;
11
12int CIORgbLed::configureDesc(const CDescObject &desc)
13{
14 const SObjectCfg::SObjectCfgItem *item = nullptr;
15 size_t offset = 0;
16
17 for (size_t i = 0; i < desc.getSize(); i++)
18 {
19 item = desc.objectCfgItemAtOffset(offset);
20
21 if (item->cfgid.s.cls != CIOCommon::IO_CLASS_ANY &&
23 {
24 DAWNERR("unsupported rgbled cfg 0x08%" PRIx32 "\n", item->cfgid.v);
25 return -EINVAL;
26 }
27
29 {
30 offset += cfgCmnOffset(item);
31 continue;
32 }
33
34 switch (item->cfgid.s.id)
35 {
36 case IO_RGBLED_CFG_INITVAL:
37 if (item->cfgid.s.size > 0)
38 {
39 initVal = *reinterpret_cast<const uint32_t *>(&item->data[0]);
40 }
41 offset += 1 + item->cfgid.s.size;
42 break;
43
44 default:
45 DAWNERR("unsupported rgbled cfg id %u\n", item->cfgid.s.id);
46 return -EINVAL;
47 }
48 }
49
50 return OK;
51}
52
53CIORgbLed::~CIORgbLed()
54{
55 deinit();
56}
57
59{
60 int ret;
61
63 {
64 DAWNERR("RGBLED requires DTYPE_UINT32\n");
65 return -EINVAL;
66 }
67
68 ret = configureDesc(getDesc());
69 if (ret != OK)
70 {
71 return ret;
72 }
73
74 if (getCmnDevno() == -1)
75 {
76 DAWNERR("RGBLED device number not configured\n");
77 return -EINVAL;
78 }
79
80 snprintf(path, sizeof(path), RGBLED_PATH_FMT, getCmnDevno());
81
82 fd = rgbled_open(path);
83 if (fd < 0)
84 {
85 DAWNERR("rgbled_open failed %d\n", fd);
86 return fd;
87 }
88
89 return OK;
90}
91
93{
94 if (fd < 0)
95 {
96 return -EIO;
97 }
98
99 return writeRgb(initVal);
100}
101
103{
104 rgbled_close(fd);
105 fd = -1;
106 return OK;
107}
108
109int CIORgbLed::writeRgb(uint32_t val)
110{
111 char rgb[8];
112 int ret;
113
114 if (fd < 0)
115 {
116 return -EIO;
117 }
118
119 // TODO: add ioctl call in nuttx so we can write RAW bytes, not string
120
121 snprintf(rgb,
122 sizeof(rgb),
123 "#%02X%02X%02X",
124 static_cast<unsigned int>((val >> 16) & 0xffu),
125 static_cast<unsigned int>((val >> 8) & 0xffu),
126 static_cast<unsigned int>(val & 0xffu));
127
128 ret = rgbled_write(fd, rgb, sizeof(rgb));
129 if (ret < 0)
130 {
131 DAWNERR("rgbled_write failed %d\n", ret);
132 return ret;
133 }
134
135 currentVal = val & 0x00ffffffu;
136
137#ifdef CONFIG_DAWN_IO_TIMESTAMP
138 if (isTimestamp())
139 {
140 ts = getTimestamp();
141 }
142#endif
143
144 return OK;
145}
146
147int CIORgbLed::getDataImpl(IODataCmn &data, size_t len)
148{
149 uint32_t *val;
150
151 if (len != 1)
152 {
153 return -ENOTSUP;
154 }
155
156 val = static_cast<uint32_t *>(data.getDataPtr());
157 *val = currentVal;
158
159#ifdef CONFIG_DAWN_IO_TIMESTAMP
160 if (isTimestamp())
161 {
162 data.getTs() = ts;
163 }
164#endif
165
166 return OK;
167}
168
170{
171 uint32_t *val = static_cast<uint32_t *>(data.getDataPtr());
172
173 return writeRgb(*val);
174}
175
177{
178 return sizeof(uint32_t);
179}
180
182{
183 return 1;
184}
Descriptor wrapper for individual object configuration.
size_t getSize() const
Get number of configuration items for this object.
SObjectCfg::SObjectCfgItem * objectCfgItemAtOffset(size_t offset) const
Get configuration item at specified offset.
uint64_t getTimestamp()
Get current timestamp.
Definition common.cxx:194
bool isTimestamp() const
Check if I/O supports timestamp.
Definition common.cxx:189
int getCmnDevno() const
Get device number for this I/O.
Definition common.hxx:798
size_t cfgCmnOffset(const SObjectCfg::SObjectCfgItem *cfg)
Get offset of configuration item in descriptor.
Definition common.cxx:144
@ IO_CLASS_ANY
Any I/O class.
Definition common.hxx:86
@ IO_CLASS_RGBLED
RGB LED output.
Definition common.hxx:168
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition rgbled.cxx:147
size_t getDataDim() const
Get data vector dimension.
Definition rgbled.cxx:181
int setDataImpl(IODataCmn &data)
Set data implementation (override in derived classes).
Definition rgbled.cxx:169
int init()
One-time initialize object after bindings are resolved.
Definition rgbled.cxx:92
int deinit()
De-initialize object.
Definition rgbled.cxx:102
size_t getDataSize() const
Get data size in bytes.
Definition rgbled.cxx:176
int configure()
Configure object from descriptor data.
Definition rgbled.cxx:58
CDescObject & getDesc()
Get descriptor object for this object.
Definition object.cxx:190
uint8_t getDtype() const
Get data type field.
Definition object.cxx:175
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Base interface for I/O data buffers (static and dynamic).
Definition idata.hxx:21
virtual void * getDataPtr(size_t batch=0)=0
Get pointer to data only (skips timestamp if present).
virtual uint64_t & getTs(size_t batch=0)=0
Get timestamp reference for batch.
Single configuration item within object.
ObjectCfgData_t data[]
Configuration data array (flexible, size from cfgid.s.size).
UObjectCfgId cfgid
Configuration ID header (type, class, id, size, rw, dtype).
@ DTYPE_UINT32
Unsigned 32-bit integer (0 to 4294967295).
Definition objectid.hxx:96
ObjectCfgId v
Raw 32-bit ConfigID value (for storage, comparison).
Definition objectcfg.hxx:82
uint32_t cls
Object class (bits 21-29, max 511).
uint32_t id
Configuration identifier (bits 0-4, max 31).
Definition objectcfg.hxx:94
uint32_t size
Configuration data size in 32-bit words (bits 5-14, max 1023).
struct dawn::SObjectCfg::UObjectCfgId::@10 s
Bit-field structure for named member access.