2121#include < FastLED.h>
2222#include < simpleRPC.h>
2323
24+ // Error codes for negative return values
25+ enum ErrorCode {
26+ ERR_DATA_LEN_TOO_LARGE = -1 ,
27+ ERR_SEND_BUSY_CHECK_FIRST = -2 ,
28+ ERR_SEND_BUSY_CHECK_SECOND = -3 ,
29+ ERR_SEND_BUSY_CHECK_THIRD = -4 ,
30+ ERR_RECV_BUSY_CHECK_FIRST = -5 ,
31+ ERR_RECV_BUSY_CHECK_SECOND = -6 ,
32+ ERR_RECV_BUSY_CHECK_THIRD = -7 ,
33+ ERR_TOO_MANY_ELEMENTS = -8 ,
34+ ERR_TOO_MANY_ADDRESSES = -9 ,
35+ ERR_EEPROM_DATA_TOO_LARGE = -10 ,
36+ ERR_LEN_TOO_LARGE = -11 ,
37+ ERR_TOO_MANY_PARAMS = -12 ,
38+ ERR_SELECT_COMMAND_TOO_LARGE = -13 ,
39+ ERR_TX_DATA_TOO_LARGE = -14 ,
40+ ERR_SEND_DATA_TOO_LARGE = -15
41+ };
42+
2443// SPI commands for PN5180:
2544static const uint8_t PN5180_WRITE_REGISTER = 0x00 ;
2645static const uint8_t PN5180_WRITE_REGISTER_OR_MASK = 0x01 ;
@@ -92,13 +111,13 @@ static int send_spi_data(const uint8_t* data, size_t data_len) {
92111 uint8_t buffer[256 ];
93112 if (data_len > sizeof (buffer)) {
94113 log (" data_len too large" );
95- return - 1 ;
114+ return ERR_DATA_LEN_TOO_LARGE ;
96115 }
97116 memcpy (buffer, data, data_len);
98117
99118 if (!wait_busy_is (LOW)) {
100119 log (" First busy check failed" );
101- return - 2 ;
120+ return ERR_SEND_BUSY_CHECK_FIRST ;
102121 }
103122
104123 digitalWrite (PN5180_NSS, LOW);
@@ -110,15 +129,15 @@ static int send_spi_data(const uint8_t* data, size_t data_len) {
110129 int retval = 0 ;
111130 if (!wait_busy_is (HIGH)) {
112131 log (" Second busy check failed" );
113- retval = - 3 ;
132+ retval = ERR_SEND_BUSY_CHECK_SECOND ;
114133 }
115134
116135 PN_SPI.endTransaction ();
117136 digitalWrite (PN5180_NSS, HIGH);
118137
119138 if (!retval && !wait_busy_is (LOW)) {
120139 log (" Third busy check failed" );
121- retval = - 4 ;
140+ retval = ERR_SEND_BUSY_CHECK_THIRD ;
122141 }
123142
124143 return retval;
@@ -129,7 +148,7 @@ static int recv_spi_data(uint8_t* buffer, size_t buffer_len) {
129148
130149 if (!wait_busy_is (LOW)) {
131150 log (" First busy check failed" );
132- return - 1 ;
151+ return ERR_RECV_BUSY_CHECK_FIRST ;
133152 }
134153
135154 digitalWrite (PN5180_NSS, LOW);
@@ -141,15 +160,15 @@ static int recv_spi_data(uint8_t* buffer, size_t buffer_len) {
141160 int retval = 0 ;
142161 if (!wait_busy_is (HIGH)) {
143162 log (" Second busy check failed" );
144- retval = - 2 ;
163+ retval = ERR_RECV_BUSY_CHECK_SECOND ;
145164 }
146165
147166 PN_SPI.endTransaction ();
148167 digitalWrite (PN5180_NSS, HIGH);
149168
150169 if (!retval && !wait_busy_is (LOW)) {
151170 log (" Third busy check failed" );
152- retval = - 3 ;
171+ retval = ERR_RECV_BUSY_CHECK_THIRD ;
153172 }
154173
155174 return retval;
@@ -229,7 +248,7 @@ static int write_register_and_mask(uint8_t addr, uint32_t value) {
229248static int write_register_multiple (Vector<Object<uint8_t , uint8_t , uint32_t >>& elements) {
230249 if (elements.size > 42 ) {
231250 log (" Too many elements" );
232- return - 1 ;
251+ return ERR_TOO_MANY_ELEMENTS ;
233252 }
234253
235254 uint8_t buffer[211 ];
@@ -290,7 +309,7 @@ static Object<int, Vector<uint32_t>> read_register_multiple(Vector<uint8_t>& add
290309
291310 if (addrs.size > 18 ) {
292311 log (" Too many addresses" );
293- get<0 >(result) = - 1 ;
312+ get<0 >(result) = ERR_TOO_MANY_ADDRESSES ;
294313 return result;
295314 }
296315
@@ -330,7 +349,7 @@ static int16_t write_eeprom(uint8_t addr, Vector<uint8_t>& values) {
330349 uint8_t buffer[256 ];
331350 if (values.size > 255 ) {
332351 log (" Too much data to write" );
333- return - 1 ;
352+ return ERR_EEPROM_DATA_TOO_LARGE ;
334353 }
335354
336355 buffer[0 ] = PN5180_WRITE_EEPROM;
@@ -385,7 +404,7 @@ static int16_t write_tx_data(Vector<uint8_t>& values) {
385404 uint8_t buffer[261 ];
386405 if (values.size > 260 ) {
387406 log (" Too much data to write" );
388- return - 1 ;
407+ return ERR_TX_DATA_TOO_LARGE ;
389408 }
390409
391410 buffer[0 ] = PN5180_WRITE_TX_DATA;
@@ -413,7 +432,7 @@ static int16_t send_data(uint8_t bits, Vector<uint8_t>& values) {
413432 uint8_t buffer[262 ];
414433 if (values.size > 260 ) {
415434 log (" Too much data to write" );
416- return - 1 ;
435+ return ERR_SEND_DATA_TOO_LARGE ;
417436 }
418437
419438 buffer[0 ] = PN5180_SEND_DATA;
@@ -442,7 +461,7 @@ static Object<int, Vector<uint8_t>> read_data(uint16_t len) {
442461
443462 if (len > sizeof (response)) {
444463 log (" len too large" );
445- get<0 >(result) = - 1 ;
464+ get<0 >(result) = ERR_LEN_TOO_LARGE ;
446465 return result;
447466 }
448467
@@ -486,7 +505,7 @@ static int switch_mode(uint8_t mode, Vector<uint8_t>& params) {
486505 uint8_t buffer[1 + 1 + 4 ];
487506 if (params.size > 4 ) {
488507 log (" Too many params" );
489- return - 1 ;
508+ return ERR_TOO_MANY_PARAMS ;
490509 }
491510 buffer[0 ] = PN5180_SWITCH_MODE;
492511 buffer[1 ] = mode;
@@ -553,7 +572,7 @@ static int16_t epc_inventory(Vector<uint8_t>& select_command, uint8_t select_com
553572 uint8_t begin_round[3 ], uint8_t timeslot_behavior) {
554573 if (select_command.size > 39 ) {
555574 log (" Too large select_command" );
556- return - 1 ;
575+ return ERR_SELECT_COMMAND_TOO_LARGE ;
557576 }
558577
559578 uint8_t buffer[47 ];
0 commit comments