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