Skip to content

Commit efc25fb

Browse files
committed
Fixed ADC Remote
1 parent 4293443 commit efc25fb

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

coresdk/src/backend/gpio_driver.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232

3333
using namespace std;
3434
// Use https://abyz.me.uk/rpi/pigpio/pdif2.html for local command reference
35-
// Archive Link: https://web.archive.org/web/20240423160241/https://abyz.me.uk/rpi/pigpio/pdif2.html
35+
// Archive Link: https://web.archive.org/web/20240423160241/https://abyz.me.uk/rpi/pigpio/pdif2.html
3636
//
3737
// Use https://abyz.me.uk/rpi/pigpio/sif.html for remote command reference
38-
// Archive Link: https://web.archive.org/web/20240423160319/https://abyz.me.uk/rpi/pigpio/sif.html
38+
// Archive Link: https://web.archive.org/web/20240423160319/https://abyz.me.uk/rpi/pigpio/sif.html
3939
namespace splashkit_lib
4040
{
4141
//Add map to track items for remote gpio
@@ -574,15 +574,17 @@ namespace splashkit_lib
574574
sk_gpio_send_cmd(pi, set_dutycycle_cmd);
575575
}
576576

577-
void sk_remote_i2c_open(connection pi, int bus, int address, int flags)
577+
// **FIXED**: The return type is now int to return the handle.
578+
int sk_remote_i2c_open(connection pi, int bus, int address, int flags)
578579
{
579580
sk_pigpio_cmd_t i2c_open_cmd;
580581
i2c_open_cmd.cmd_code = GPIO_I2C_CMD_OPEN;
581582
i2c_open_cmd.param1 = bus;
582583
i2c_open_cmd.param2 = address;
583584
i2c_open_cmd.param3 = flags;
584585

585-
sk_gpio_send_cmd(pi, i2c_open_cmd);
586+
// **FIXED**: Return the result of the command, which is the I2C handle.
587+
return sk_gpio_send_cmd(pi, i2c_open_cmd);
586588
}
587589

588590
int sk_remote_i2c_close(connection pi, int handle) {
@@ -750,4 +752,4 @@ namespace splashkit_lib
750752
return "Unknown error code " + std::to_string(error_code);
751753
}
752754
}
753-
}
755+
}

coresdk/src/backend/gpio_driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ namespace splashkit_lib
229229
void sk_remote_set_pwm_frequency(connection pi, int pin, int frequency);
230230
void sk_remote_set_pwm_dutycycle(connection pi, int pin, int dutycycle);
231231

232-
void sk_remote_i2c_open(connection pi, int bus, int address, int flags);
232+
int sk_remote_i2c_open(connection pi, int bus, int address, int flags);
233233
int sk_remote_i2c_close(connection pi, int handle);
234234
int sk_remote_i2c_read_byte(connection pi, int handle);
235235
int sk_remote_i2c_write_byte(connection pi, int handle, int data);

coresdk/src/coresdk/raspi_adc.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
//
2-
// raspi_adc.cpp
3-
// splashkit
1+
// raspi_adc.cpp
2+
// splashkit
43
//
54
// This file is part of the SplashKit Core Library.
6-
// Copyright (©) 2024 Aditya Parmar. All Rights Reserved.
5+
// Copyright (©) 2024 Aditya Parmar. All Rights Reserved.
76
//
87

98
#include "raspi_adc.h"
@@ -215,7 +214,7 @@ namespace splashkit_lib
215214
if (channel < 0 || channel > 3)
216215
{
217216
LOG(WARNING) << "Invalid ADC channel: " << channel
218-
<< " for device " << dev->name << " (PCF8591 supports 0-3)";
217+
<< " for device " << dev->name << " (PCF8591 supports 0-3)";
219218
return -1;
220219
}
221220
command = channel & 0x03;
@@ -230,7 +229,7 @@ namespace splashkit_lib
230229
if (sk_i2c_write_byte(dev->i2c_handle, command) < 0)
231230
{
232231
LOG(WARNING) << "Failed to write ADC channel command for channel " << channel
233-
<< " on device " << dev->name;
232+
<< " on device " << dev->name;
234233
return -1;
235234
}
236235

@@ -242,7 +241,7 @@ namespace splashkit_lib
242241
if (value < 0)
243242
{
244243
LOG(WARNING) << "Error reading ADC channel " << channel
245-
<< " from device " << dev->name;
244+
<< " from device " << dev->name;
246245
}
247246

248247
return value;
@@ -265,7 +264,7 @@ namespace splashkit_lib
265264
if (channel < ADC_PIN_0 || channel > ADC_PIN_7)
266265
{
267266
LOG(WARNING) << "Invalid ADC channel: " << channel
268-
<< " for device " << adc->name << " (ADS7830 supports 0-7)";
267+
<< " for device " << adc->name << " (ADS7830 supports 0-7)";
269268
return -1;
270269
}
271270

@@ -371,7 +370,7 @@ namespace splashkit_lib
371370
}
372371

373372
remote_adc_device remote_adc_device_named(const std::string& name) {
374-
if (has_adc_device(name)) {
373+
if (remote_has_adc_device(name)) {
375374
return remote_adc_devices[name];
376375
}
377376
return nullptr;
@@ -393,12 +392,21 @@ namespace splashkit_lib
393392
result->name = name;
394393
result->type = type;
395394

396-
sk_remote_i2c_open(result->pi, result->bus, result->address, 0);
395+
// **FIXED**: Correctly assign the handle from sk_remote_i2c_open
396+
result->i2c_handle = sk_remote_i2c_open(result->pi, result->bus, result->address, 0);
397+
398+
// **FIXED**: Check if the I2C open call was successful before proceeding
399+
if (result->i2c_handle < 0) {
400+
LOG(WARNING) << "Error opening remote ADC device " << name;
401+
delete result;
402+
return nullptr;
403+
}
397404

398405
int test_result = sk_remote_i2c_write_byte(result->pi, result->i2c_handle, 0x84);
399406
if (test_result < 0) {
400407
LOG(WARNING) << "Failed to communicate with ADC device " << name << " (write test byte failed)\n";
401408
sk_remote_i2c_close(result->pi, result->i2c_handle);
409+
delete result;
402410
return nullptr;
403411
}
404412

@@ -425,7 +433,7 @@ namespace splashkit_lib
425433
{
426434
if (remote_has_adc_device(name))
427435
{
428-
LOG(WARNING) << "ADC device " << name << " already loaded.";
436+
LOG(WARNING) << "ADC device " << name << " already loaded.";
429437
return remote_adc_device_named(name);
430438
}
431439
return remote_load_adc_device(pi, name, bus, address, type);
@@ -435,7 +443,7 @@ namespace splashkit_lib
435443
{
436444
if (dev == nullptr)
437445
{
438-
LOG(WARNING) << "Invalid ADC device.";
446+
LOG(WARNING) << "Invalid ADC device.";
439447
return -1;
440448
}
441449

@@ -446,13 +454,13 @@ namespace splashkit_lib
446454
command = channel;
447455
break;
448456
default:
449-
LOG(WARNING) << "Unsupported ADC type for device " << dev->name;
457+
LOG(WARNING) << "Unsupported ADC type for device " << dev->name;
450458
return -1;
451459
}
452460

453461
if (sk_remote_i2c_write_byte(dev->pi, dev->i2c_handle, command) < 0)
454462
{
455-
LOG(WARNING) << "Failed to write ADC channel command for channel " << std::to_string(channel) << " on device " << dev->name;
463+
LOG(WARNING) << "Failed to write ADC channel command for channel " << std::to_string(channel) << " on device " << dev->name;
456464
return -1;
457465
}
458466

@@ -461,7 +469,7 @@ namespace splashkit_lib
461469
int value = sk_remote_i2c_read_byte(dev->pi, dev->i2c_handle);
462470
if (value < 0)
463471
{
464-
LOG(WARNING) << "Error reading ADC channel " << std::to_string(channel) << " from device " << dev->name;
472+
LOG(WARNING) << "Error reading ADC channel " << std::to_string(channel) << " from device " << dev->name;
465473
}
466474
return value;
467475
}
@@ -476,7 +484,7 @@ namespace splashkit_lib
476484

477485
if (channel < ADC_PIN_0 || channel > ADC_PIN_7)
478486
{
479-
LOG(WARNING) << "Invalid ADC channel: " << std::to_string(channel) << " for device " << adc->name << " (ADS7830 supports 0-7)";
487+
LOG(WARNING) << "Invalid ADC channel: " << std::to_string(channel) << " for device " << adc->name << " (ADS7830 supports 0-7)";
480488
return -1;
481489
}
482490

0 commit comments

Comments
 (0)