From a2bf57180753547724dc92b0b44224428af8e100 Mon Sep 17 00:00:00 2001 From: JPF2209 Date: Wed, 30 Apr 2025 21:42:27 +1000 Subject: [PATCH 1/4] Adc fixed --- coresdk/src/coresdk/raspi_adc.cpp | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/coresdk/src/coresdk/raspi_adc.cpp b/coresdk/src/coresdk/raspi_adc.cpp index 926152c7..41431873 100644 --- a/coresdk/src/coresdk/raspi_adc.cpp +++ b/coresdk/src/coresdk/raspi_adc.cpp @@ -103,16 +103,8 @@ namespace splashkit_lib // Try to write a test byte to the device to check if it's responding. // This is a simple way to check if the device is connected. // For ADS7830, we can use a command like 0x84 (CH0) to test. - int test = sk_i2c_write_byte(result->i2c_handle, 0x84); - if (test < 0) - { - // ask the user to check the device connection - LOG(WARNING) << "Error communicating with ADC device, check your ADC connection" << name - << " on bus " << bus << " at address " << address; - sk_i2c_close(result->i2c_handle); - delete result; - return nullptr; - } + sk_i2c_write_byte(result->i2c_handle, 0x84); + _adc_devices[name] = result; return result; @@ -234,7 +226,7 @@ namespace splashkit_lib if (channel < ADC_PIN_0 || channel > ADC_PIN_7) { LOG(WARNING) << "Invalid ADC channel: " << channel - << " for device " << dev->name << " (ADS7830 supports 0-7)"; + << " for device " << adc->name << " (ADS7830 supports 0-7)"; return -1; } } From dad3f2c3b23646a9652db8ef11f53f70066e4943 Mon Sep 17 00:00:00 2001 From: JPF2209 Date: Thu, 4 Sep 2025 16:37:32 +1000 Subject: [PATCH 2/4] Final ADC Remote Code --- coresdk/src/backend/gpio_driver.cpp | 74 +++++++++++ coresdk/src/backend/gpio_driver.h | 12 ++ coresdk/src/coresdk/raspi_adc.cpp | 184 +++++++++++++++++++++++++++- coresdk/src/coresdk/raspi_adc.h | 66 ++++++++++ coresdk/src/test/test_main.cpp | 1 + coresdk/src/test/test_main.h | 1 + coresdk/src/test/test_raspi_adc.cpp | 109 ++++++++++++++++ 7 files changed, 446 insertions(+), 1 deletion(-) diff --git a/coresdk/src/backend/gpio_driver.cpp b/coresdk/src/backend/gpio_driver.cpp index ef99839a..b2413dd7 100644 --- a/coresdk/src/backend/gpio_driver.cpp +++ b/coresdk/src/backend/gpio_driver.cpp @@ -448,6 +448,80 @@ namespace splashkit_lib sk_gpio_send_cmd(pi, set_dutycycle_cmd); } + int sk_remote_i2c_open(connection pi, int bus, int address, int flags) + { + sk_pigpio_cmd_t i2c_open_cmd; + i2c_open_cmd.cmd_code = GPIO_I2C_CMD_OPEN; + i2c_open_cmd.param1 = bus; + i2c_open_cmd.param2 = address; + i2c_open_cmd.param3 = flags; + + return sk_gpio_send_cmd(pi, i2c_open_cmd); + } + + int sk_remote_i2c_close(connection pi, int handle) { + sk_pigpio_cmd_t set_cmd; + set_cmd.cmd_code = GPIO_I2C_CMD_CLOSE; + set_cmd.param1 = handle; + + return sk_gpio_send_cmd(pi, set_cmd); + } + + int sk_remote_i2c_read_byte(connection pi, int handle) { + sk_pigpio_cmd_t set_cmd; + set_cmd.cmd_code = GPIO_I2C_CMD_SLAVE_READ; + set_cmd.param1 = handle; + + return sk_gpio_send_cmd(pi, set_cmd); + } + + int sk_remote_i2c_write_byte(connection pi, int handle, int data) { + sk_pigpio_cmd_t set_cmd; + set_cmd.cmd_code = GPIO_I2C_CMD_SLAVE_WRITE; + set_cmd.param1 = handle; + set_cmd.param2 = data; + + return sk_gpio_send_cmd(pi, set_cmd); + } + + int sk_remote_i2c_read_byte_data(connection pi, int handle, int reg) { + sk_pigpio_cmd_t set_cmd; + set_cmd.cmd_code = GPIO_I2C_CMD_READ_BYTE_DATA; + set_cmd.param1 = handle; + set_cmd.param2 = reg; + + return sk_gpio_send_cmd(pi, set_cmd); + } + + void sk_remote_i2c_write_byte_data(connection pi, int handle, int reg, int data) { + sk_pigpio_cmd_t set_cmd; + set_cmd.cmd_code = GPIO_I2C_CMD_WRITE_BYTE_DATA; + set_cmd.param1 = handle; + set_cmd.param2 = reg; + set_cmd.param3 = data; + + sk_gpio_send_cmd(pi, set_cmd); + } + + int sk_remote_i2c_read_word_data(connection pi, int handle, int reg) { + sk_pigpio_cmd_t set_cmd; + set_cmd.cmd_code = GPIO_I2C_CMD_READ_WORD_DATA; + set_cmd.param1 = handle; + set_cmd.param2 = reg; + + return sk_gpio_send_cmd(pi, set_cmd); + } + + void sk_remote_i2c_write_word_data(connection pi, int handle, int reg, int data) { + sk_pigpio_cmd_t set_cmd; + set_cmd.cmd_code = GPIO_I2C_CMD_WRITE_WORD_DATA; + set_cmd.param1 = handle; + set_cmd.param2 = reg; + set_cmd.param3 = data; + + sk_gpio_send_cmd(pi, set_cmd); + } + void sk_remote_clear_bank_1(connection pi) { sk_pigpio_cmd_t clear_bank_cmd; diff --git a/coresdk/src/backend/gpio_driver.h b/coresdk/src/backend/gpio_driver.h index c370b360..d8af23b8 100644 --- a/coresdk/src/backend/gpio_driver.h +++ b/coresdk/src/backend/gpio_driver.h @@ -6,6 +6,7 @@ #define SPLASHKIT_GPIO_H #include "backend_types.h" +#include "types.h" #include // Include the appropriate header file for stdint.h // Relevant error codes from pigpio library @@ -229,6 +230,17 @@ namespace splashkit_lib void sk_remote_set_pwm_range(connection pi, int pin, int range); void sk_remote_set_pwm_frequency(connection pi, int pin, int frequency); void sk_remote_set_pwm_dutycycle(connection pi, int pin, int dutycycle); + + // Remote I2C + int sk_remote_i2c_open(connection pi, int bus, int address, int flags); + int sk_remote_i2c_close(connection pi, int handle); + int sk_remote_i2c_read_byte(connection pi, int handle); + int sk_remote_i2c_write_byte(connection pi, int handle, int data); + int sk_remote_i2c_read_byte_data(connection pi, int handle, int reg); + void sk_remote_i2c_write_byte_data(connection pi, int handle, int reg, int data); + int sk_remote_i2c_read_word_data(connection pi, int handle, int reg); + void sk_remote_i2c_write_word_data(connection pi, int handle, int reg, int data); + void sk_remote_clear_bank_1(connection pi); bool sk_remote_gpio_cleanup(connection pi); diff --git a/coresdk/src/coresdk/raspi_adc.cpp b/coresdk/src/coresdk/raspi_adc.cpp index 2e497af1..93ee7cfd 100644 --- a/coresdk/src/coresdk/raspi_adc.cpp +++ b/coresdk/src/coresdk/raspi_adc.cpp @@ -28,9 +28,20 @@ namespace splashkit_lib string name; // Device name }; + struct _remote_adc_data + { + pointer_identifier id; // Should be ADC_PTR + int i2c_handle; // I2C handle (obtained from i2c_open) + int bus; // I2C bus number + int address; // I2C address for the ADC device + adc_type type; // ADC type (e.g., ADS7830, PCF8591, etc.) + string name; // Device name + connection pi; + }; + // Static map to manage loaded ADC devices (keyed by name) static map _adc_devices; - + static map remote_adc_devices; // a function to return address based on pin number of ads7830 int _get_ads7830_pin_address(adc_pin pin) { @@ -321,4 +332,175 @@ namespace splashkit_lib LOG(ERROR) << "ADC not supported on this platform"; #endif } + + // Start of remote functions + remote_adc_device remote_adc_device_named(const std::string& name) { + if (remote_has_adc_device(name)) { + return remote_adc_devices[name]; + } + return nullptr; + } + + remote_adc_device remote_load_adc_device(connection pi, const string &name, int bus, int address, adc_type type) + { + if (remote_has_adc_device(name)) { + return remote_adc_device_named(name); + } + + // Use std::make_unique for safe memory allocation and ownership transfer + remote_adc_device result = new _remote_adc_data(); + static int next_adc_id = 0; + result->id = static_cast(next_adc_id++); + result->pi = pi; + result->bus = bus; + result->address = address; + result->name = name; + result->type = type; + + result->i2c_handle = sk_remote_i2c_open(result->pi, result->bus, result->address, 0); + + if (result->i2c_handle < 0) { + LOG(WARNING) << "Error opening remote ADC device " << name; + delete result; + return nullptr; + } + + int test_result = sk_remote_i2c_write_byte(result->pi, result->i2c_handle, 0x84); + if (test_result < 0) { + LOG(WARNING) << "Failed to communicate with ADC device " << name << " (write test byte failed)\n"; + sk_remote_i2c_close(result->pi, result->i2c_handle); + delete result; + return nullptr; + } + + LOG(INFO) << "ADC device " << name << " loaded on bus " << bus << " at address " << address << "\n"; + + // Transfer ownership to the map + remote_adc_devices[name] = result; + return remote_adc_devices[name]; + } + + remote_adc_device remote_open_adc(connection pi, string name, adc_type type) + { + if (type != ADS7830) + { + LOG(ERROR) << "Unsupported ADC type for " << name; + return nullptr; + } + const int default_bus = 1; + const int default_address = 0x48; // Default I2C address for ADS7830 + return remote_load_adc_device(pi, name, default_bus, default_address, type); + } + + remote_adc_device remote_open_adc(connection pi, string name, int bus, int address, adc_type type) + { + if (remote_has_adc_device(name)) + { + LOG(WARNING) << "ADC device " << name << " already loaded."; + return remote_adc_device_named(name); + } + return remote_load_adc_device(pi, name, bus, address, type); + } + + int remote_read_adc_channel(remote_adc_device dev, int channel) + { + if (dev == nullptr) + { + LOG(WARNING) << "Invalid ADC device."; + return -1; + } + + int command = 0; + switch (dev->type) + { + case ADS7830: + command = channel; + break; + default: + LOG(WARNING) << "Unsupported ADC type for device " << dev->name; + return -1; + } + + if (sk_remote_i2c_write_byte(dev->pi, dev->i2c_handle, command) < 0) + { + LOG(WARNING) << "Failed to write ADC channel command for channel " << std::to_string(channel) << " on device " << dev->name; + return -1; + } + + delay(10); + + int value = sk_remote_i2c_read_byte(dev->pi, dev->i2c_handle); + if (value < 0) + { + LOG(WARNING) << "Error reading ADC channel " << std::to_string(channel) << " from device " << dev->name; + } + return value; + } + + int remote_read_adc(remote_adc_device adc, adc_pin channel) + { + if (adc == nullptr) + { + LOG(ERROR) << "ADC device not initialized."; + return -1; + } + + if (channel < ADC_PIN_0 || channel > ADC_PIN_7) + { + LOG(WARNING) << "Invalid ADC channel: " << std::to_string(channel) << " for device " << adc->name << " (ADS7830 supports 0-7)"; + return -1; + } + + int channel_num = _get_ads7830_pin_address(channel); + if (channel_num == -1) + { + LOG(ERROR) << "Invalid ADC pin: " << std::to_string(channel); + return -1; + } + return remote_read_adc_channel(adc, channel_num); + } + + int remote_read_adc(const std::string& name, adc_pin channel) { + _remote_adc_data* dev = remote_adc_device_named(name); + if (dev == nullptr) { + LOG(ERROR) << "ADC device " << name << " not found.\n"; + return -1; + } + int channel_num = _get_ads7830_pin_address(channel); + if (channel_num == -1) { + LOG(ERROR) << "Invalid ADC pin: " << channel << "\n"; + return -1; + } + return remote_read_adc_channel(remote_adc_devices.at(name), channel_num); + } + + void remote_close_adc(remote_adc_device adc) + { + remote_close_adc_device(adc); + } + + void remote_close_adc(const string &name) + { + // Find the device in the local map of remotely opened devices + auto it = remote_adc_devices.find(name); + if (it != remote_adc_devices.end()) { + remote_adc_device dev = it->second; + // Call the remote closing function + remote_close_adc_device(dev); + } else { + LOG(WARNING) << "Attempted to close unknown ADC device: " << name; + } + } + + void remote_close_adc_device(remote_adc_device dev) + { + auto it = remote_adc_devices.find(dev->name); + if (it != remote_adc_devices.end()) { + sk_remote_i2c_close(it->second->pi, it->second->i2c_handle); + LOG(INFO) << "Closed ADC device: " << dev->name << "\n"; + remote_adc_devices.erase(it); + } else { + LOG(WARNING) << "Attempted to close unknown ADC device: " << dev->name << "\n"; + } + } } diff --git a/coresdk/src/coresdk/raspi_adc.h b/coresdk/src/coresdk/raspi_adc.h index c6af36d7..0b06be4b 100644 --- a/coresdk/src/coresdk/raspi_adc.h +++ b/coresdk/src/coresdk/raspi_adc.h @@ -125,5 +125,71 @@ namespace splashkit_lib * Closes all ADC devices that have been opened. */ void close_all_adc(); + /** + * @brief Initialises a remote connection to a Raspberry Pi. + * + * This function initialises a connection to a remote Raspberry Pi using the specified name, host, and port. + * + * @param name The name of the connection. + * @param host The host address of the Raspberry Pi. + * @param port The port to use for the connection. + * @returns The connection object used to communicate with the remote Raspberry Pi. + */ + connection remote_raspi_init(const string &name, const string &host, unsigned short int port); + + /** + * @brief Sets the mode of the specified pin on a remote Raspberry Pi. + * + * This function sets the mode of a specific pin on a remote Raspberry Pi. + * + * @param pi The connection object to the remote Raspberry Pi. + * @param pin The pin to set the mode for. + * @param mode The mode to set for the pin. + */ + void remote_raspi_set_mode(connection pi, gpio_pin pin, gpio_pin_mode mode); + + /** + * @brief Gets the mode of the specified pin on a remote Raspberry Pi. + * + * This function retrieves the mode of a specific pin on a remote Raspberry Pi. + * + * @param pi The connection object to the remote Raspberry Pi. + * @param pin The pin to get the mode for. + * @returns The mode of the pin. + */ + gpio_pin_mode remote_raspi_get_mode(connection pi, gpio_pin pin); + + /** + * @brief Sets the pull up/down mode for the specified pin on a remote Raspberry Pi. + * + * This function sets the pull-up/down mode of a specific pin on a remote Raspberry Pi. + * + * @param pi The connection object to the remote Raspberry Pi. + * @param pin The pin to set the pull up/down mode for. + * @param pud The pull up/down mode to set for the pin. + */ + void remote_raspi_set_pull_up_down(connection pi, gpio_pin pin, pull_up_down pud); + + /** + * @brief Writes a value to the specified pin on a remote Raspberry Pi. + * + * This function writes a specified value to a specific pin on a remote Raspberry Pi. + * + * @param pi The connection object to the remote Raspberry Pi. + * @param pin The pin to write the value to. + * @param value The value to write to the pin. + */ + void remote_raspi_write(connection pi, gpio_pin pin, gpio_pin_value value); + + /** + * @brief Reads the value from the specified pin on a remote Raspberry Pi. + * + * This function reads the value from a specific pin on a remote Raspberry Pi. + * + * @param pi The connection object to the remote Raspberry Pi. + * @param pin The pin to read the value from. + * @returns The value read from the pin. + */ + gpio_pin_value remote_raspi_read(connection pi, gpio_pin pin); } #endif /* raspi_adc_hpp */ diff --git a/coresdk/src/test/test_main.cpp b/coresdk/src/test/test_main.cpp index bd02b9af..69a77b5b 100644 --- a/coresdk/src/test/test_main.cpp +++ b/coresdk/src/test/test_main.cpp @@ -63,6 +63,7 @@ void setup_tests() add_test("Motor Driver Tests", run_motor_driver_tests); add_test("Servo Driver Tests", run_servo_driver_tests); add_test("Remote GPIO Tests", run_remote_gpio_tests); + add_test("Remote ADC Tests", run_remote_gpio_adc_tests); add_test("GPIO Tests - SPI", run_gpio_spi_tests); add_test("UI Tests", run_ui_test); diff --git a/coresdk/src/test/test_main.h b/coresdk/src/test/test_main.h index 2c3c5afd..5f878b5a 100644 --- a/coresdk/src/test/test_main.h +++ b/coresdk/src/test/test_main.h @@ -37,6 +37,7 @@ void run_gpio_adc_tests(); void run_motor_driver_tests(); void run_servo_driver_tests(); void run_remote_gpio_tests(); +void run_remote_gpio_adc_tests(); void run_gpio_spi_tests(); void run_terminal_test(); void run_logging_test(); diff --git a/coresdk/src/test/test_raspi_adc.cpp b/coresdk/src/test/test_raspi_adc.cpp index 94606862..c5dad805 100644 --- a/coresdk/src/test/test_raspi_adc.cpp +++ b/coresdk/src/test/test_raspi_adc.cpp @@ -5,6 +5,7 @@ #include #include "raspi_gpio.h" +#include "raspi_adc.h" #include "input.h" #include "input_driver.h" @@ -67,6 +68,63 @@ void run_joystick_test(gpio_pin end_test_button) close_adc(adc); } + +void run_remote_potentiometer_test(connection pi, gpio_pin end_test_button) +{ + cout << "ADC Test 1: Using a Potentiometer with ADC device: ADS7830" << endl; + cout << "- Connect a potentiometer to A0 channel on the ADS7830 device" << endl; + + remote_adc_device dev = remote_open_adc(pi, "ADC1", 1, 0x48, ADS7830); + if (dev == nullptr) + { + cout << "Failed to open ADC device." << endl; + return; + } + + adc_pin channel = ADC_PIN_0; // Can change this to the desired channel + int value = 0; + + while (remote_raspi_read(pi, end_test_button) != GPIO_HIGH) + { + value = remote_read_adc(dev, channel); + cout << "ADC value: " << value << endl; + } + + remote_close_adc(dev); + cout << "ADC potentiometer test completed." << endl; +} + +void run_remote_joystick_test(connection pi, gpio_pin end_test_button) +{ + cout << "Test 2: Testing ADC (ADS7830) using a Joystick module." << endl; + cout << "- Connect VRx to A1 channel on the ADS7830 device." << endl; + cout << "- Connect VRy to A2 channel on the ADS7830 device." << endl; + + remote_adc_device adc = remote_open_adc(pi, "ADC1", 1, 0x48, ADS7830); + if (adc == nullptr) + { + cout << "Failed to open ADC device." << endl; + return; + } + + // Joystick movement values + int joystick_x; + int joystick_y; + + while (remote_raspi_read(pi, end_test_button) != GPIO_HIGH) + { + joystick_x = remote_read_adc(adc, ADC_PIN_1); + joystick_y = remote_read_adc(adc, ADC_PIN_2); + + cout << "\033[2J\033[H" << endl; + cout << "Joystick values:" << endl; + cout << " VRx: " << joystick_x << "\tVRy: " << joystick_y << endl; + } + + // Clean up + remote_close_adc(adc); +} + void run_gpio_adc_tests() { const int NUMBER_OF_ADC_TESTS = 2; // Update this if more tests are added @@ -112,4 +170,55 @@ void run_gpio_adc_tests() // Clean up raspi_cleanup(); cout << "ADC test complete." << endl; +} + +void run_remote_gpio_adc_tests() +{ + const int NUMBER_OF_ADC_TESTS = 2; // Update this if more tests are added + + cout << endl; + cout << "ADC Tests:" << endl; + cout << endl; + cout << "Note: The following GPIO tests require a button to wired to pin 7 on the remote Pi to allow the test to be stopped cleanly." << endl; + cout << endl; + + cout << "This test requires a remote Raspberry Pi running the Pigpio Daemon" << endl; + cout << "Enter IP Address of remote Pi:" << endl; + + std::string host; + std::getline(std::cin, host); + + cout << "Initializing GPIO" << endl; + connection pi = remote_raspi_init("Raspi", host, 8888); + + int user_input = 0; + while (user_input < 1 || user_input > NUMBER_OF_ADC_TESTS) { + cout << "ADC Tests using ADC device: ADS7830" << endl; + cout << "1: Potentiometer test" << endl; + cout << "2: Joystick test" << endl; + cout << "------------------------" << endl; + cout << "Select ADC test to run: "; + cin >> user_input; + cin.ignore(); + } + + // Set up button on the remote Pi for ending the program + gpio_pin end_button_pin = PIN_7; + remote_raspi_set_mode(pi, end_button_pin, GPIO_INPUT); + remote_raspi_set_pull_up_down(pi, end_button_pin, PUD_DOWN); + + switch (user_input) { + case 1: + run_remote_potentiometer_test(pi, end_button_pin); + break; + case 2: + run_remote_joystick_test(pi, end_button_pin); + break; + default: + break; + } + + // Clean up remote connection + remote_raspi_cleanup(pi); + cout << "ADC test complete." << endl; } \ No newline at end of file From c2b6cf2a9ddc63f03e8af0715befaee029f460a0 Mon Sep 17 00:00:00 2001 From: JPF2209 Date: Thu, 4 Sep 2025 18:27:10 +1000 Subject: [PATCH 3/4] Fixed ADC Code --- coresdk/src/backend/backend_types.h | 15 +++- coresdk/src/coresdk/raspi_adc.cpp | 13 +++ coresdk/src/coresdk/raspi_adc.h | 120 ++++++++++++---------------- 3 files changed, 76 insertions(+), 72 deletions(-) diff --git a/coresdk/src/backend/backend_types.h b/coresdk/src/backend/backend_types.h index 03083a86..90030ef1 100644 --- a/coresdk/src/backend/backend_types.h +++ b/coresdk/src/backend/backend_types.h @@ -343,8 +343,19 @@ namespace splashkit_lib GPIO_CMD_SET_PWM_DUTYCYCLE, GPIO_CMD_SET_PWM_RANGE, GPIO_CMD_SET_PWM_FREQ, - GPIO_CMD_CLEAR_BANK_1 = 12 - + GPIO_CMD_CLEAR_BANK_1 = 12, + + // I2C Command Codes + GPIO_I2C_CMD_OPEN, + GPIO_I2C_CMD_CLOSE, + GPIO_I2C_CMD_SLAVE_READ, + GPIO_I2C_CMD_SLAVE_WRITE, + GPIO_I2C_CMD_READ_DEVICE, + GPIO_I2C_CMD_WRITE_DEVICE, + GPIO_I2C_CMD_READ_BYTE_DATA, + GPIO_I2C_CMD_WRITE_BYTE_DATA, + GPIO_I2C_CMD_READ_WORD_DATA, + GPIO_I2C_CMD_WRITE_WORD_DATA }; } diff --git a/coresdk/src/coresdk/raspi_adc.cpp b/coresdk/src/coresdk/raspi_adc.cpp index 93ee7cfd..e8edac6e 100644 --- a/coresdk/src/coresdk/raspi_adc.cpp +++ b/coresdk/src/coresdk/raspi_adc.cpp @@ -14,6 +14,15 @@ using std::map; using std::string; +using connection = int; +using adc_type = int; +using adc_pin = int; +constexpr adc_type ADS7830 = 0; +constexpr adc_pin ADC_PIN_0 = 0; +constexpr adc_pin ADC_PIN_7 = 7; +constexpr int ADC_PTR = 123; +constexpr int NONE_PTR = 0; + namespace splashkit_lib { // Internal structure for the ADC device. @@ -333,6 +342,10 @@ namespace splashkit_lib #endif } + bool remote_has_adc_device(const std::string& name) { + return remote_adc_devices.count(name) > 0; + } + // Start of remote functions remote_adc_device remote_adc_device_named(const std::string& name) { if (remote_has_adc_device(name)) { diff --git a/coresdk/src/coresdk/raspi_adc.h b/coresdk/src/coresdk/raspi_adc.h index 0b06be4b..fad169c4 100644 --- a/coresdk/src/coresdk/raspi_adc.h +++ b/coresdk/src/coresdk/raspi_adc.h @@ -11,27 +11,16 @@ #define raspi_adc_hpp #include "types.h" +#include "networking.h" #include namespace splashkit_lib { - /** - * The `adc_device` type is used to refer to ADC (Analog-to-Digital Converter) - * devices that can be managed by the SplashKit ADC code, such as ADS7830. - * ADC devices are: - * - * - loaded with `open_adc`, - * - accessed using `adc_device_named` or checked with `has_adc_device`, - * - read using `read_adc` to retrieve analog values from specific channels, - * - and must be closed using `close_adc` (to release a specific - * ADC device) or `close_all_adc` (to release all loaded ADC devices). - * - * ADC devices allow you to interface with external analog sensors or inputs, - * converting their signals into digital values for processing in your application. - * - * @attribute class adc_device - */ - typedef struct _adc_data *adc_device; + // Forward declare adc_device as a pointer to internal struct _adc_data + typedef struct _adc_data* adc_device; + + typedef struct _remote_adc_data* remote_adc_device; + /** * Checks if an ADC device with the given name has been loaded. @@ -126,70 +115,61 @@ namespace splashkit_lib */ void close_all_adc(); /** - * @brief Initialises a remote connection to a Raspberry Pi. - * - * This function initialises a connection to a remote Raspberry Pi using the specified name, host, and port. + * Loads an ADC device on the specified I2C bus at a given address remotely. * - * @param name The name of the connection. - * @param host The host address of the Raspberry Pi. - * @param port The port to use for the connection. - * @returns The connection object used to communicate with the remote Raspberry Pi. + * @param name The name to assign this ADC device. + * @param bus The I2C bus number. + * @param address The I2C address of the ADC device. + * @param type The type of ADC device (e.g., ADS7830, PCF8591). + * @returns A valid adc_device on success, or nullptr on failure. */ - connection remote_raspi_init(const string &name, const string &host, unsigned short int port); - - /** - * @brief Sets the mode of the specified pin on a remote Raspberry Pi. - * - * This function sets the mode of a specific pin on a remote Raspberry Pi. + remote_adc_device remote_open_adc(connection pi, string name, int bus, int address, adc_type type); + + /** + * Opens an ADC device with the specified name and type using remote gpio. + * Defaults to bus 1 and address 0x48. * - * @param pi The connection object to the remote Raspberry Pi. - * @param pin The pin to set the mode for. - * @param mode The mode to set for the pin. + * @param name The name of the ADC device to open. + * @param type The type of ADC device (e.g., ADS7830, PCF8591). + * @returns A valid adc_device on success, or nullptr on failure. */ - void remote_raspi_set_mode(connection pi, gpio_pin pin, gpio_pin_mode mode); - - /** - * @brief Gets the mode of the specified pin on a remote Raspberry Pi. - * - * This function retrieves the mode of a specific pin on a remote Raspberry Pi. + remote_adc_device remote_open_adc(connection pi, string name, adc_type type); + + /** + * Reads an 8-bit value from the specified ADC channel on the device on remote gpio. * - * @param pi The connection object to the remote Raspberry Pi. - * @param pin The pin to get the mode for. - * @returns The mode of the pin. + * @param adc The ADC device to read from. + * @param channel The channel number to read (range depends on ADC type). + * @returns The ADC conversion value (0–255), or -1 on error. */ - gpio_pin_mode remote_raspi_get_mode(connection pi, gpio_pin pin); - - /** - * @brief Sets the pull up/down mode for the specified pin on a remote Raspberry Pi. - * - * This function sets the pull-up/down mode of a specific pin on a remote Raspberry Pi. + int remote_read_adc(remote_adc_device adc, adc_pin channel); + + /** + * Reads an 8-bit value from the specified ADC channel on the device using its name. * - * @param pi The connection object to the remote Raspberry Pi. - * @param pin The pin to set the pull up/down mode for. - * @param pud The pull up/down mode to set for the pin. + * @param name The ADC device name. + * @param channel The channel number to read (range depends on ADC type). + * @returns The ADC conversion value (0–255), or -1 on error. */ - void remote_raspi_set_pull_up_down(connection pi, gpio_pin pin, pull_up_down pud); - - /** - * @brief Writes a value to the specified pin on a remote Raspberry Pi. - * - * This function writes a specified value to a specific pin on a remote Raspberry Pi. + int remote_read_adc(const std::string &name, adc_pin channel); + + /** + * Closes an ADC device given its pointer. * - * @param pi The connection object to the remote Raspberry Pi. - * @param pin The pin to write the value to. - * @param value The value to write to the pin. + * @param adc The ADC device to close. */ - void remote_raspi_write(connection pi, gpio_pin pin, gpio_pin_value value); - - /** - * @brief Reads the value from the specified pin on a remote Raspberry Pi. - * - * This function reads the value from a specific pin on a remote Raspberry Pi. + void remote_close_adc(remote_adc_device adc); + + /** + * Closes an ADC device given its name. * - * @param pi The connection object to the remote Raspberry Pi. - * @param pin The pin to read the value from. - * @returns The value read from the pin. + * @param name The name of the ADC device to close. + */ + void remote_close_adc(const std::string &name); + + /** + * Closes a specific ADC device that have been opened. */ - gpio_pin_value remote_raspi_read(connection pi, gpio_pin pin); + void remote_close_adc_device(remote_adc_device dev); } #endif /* raspi_adc_hpp */ From 25bb617d5bca810fb3d302c265b22237637b23f3 Mon Sep 17 00:00:00 2001 From: JPF2209 Date: Tue, 16 Sep 2025 09:23:05 +1000 Subject: [PATCH 4/4] Add Fixes To ADC Code --- coresdk/src/coresdk/raspi_adc.cpp | 66 ++++++++++++------------------- 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/coresdk/src/coresdk/raspi_adc.cpp b/coresdk/src/coresdk/raspi_adc.cpp index e8edac6e..272555c8 100644 --- a/coresdk/src/coresdk/raspi_adc.cpp +++ b/coresdk/src/coresdk/raspi_adc.cpp @@ -50,33 +50,21 @@ namespace splashkit_lib // Static map to manage loaded ADC devices (keyed by name) static map _adc_devices; - static map remote_adc_devices; + std::map> remote_adc_devices; // a function to return address based on pin number of ads7830 - int _get_ads7830_pin_address(adc_pin pin) - { - switch (pin) - { - case ADC_PIN_0: - return 0x84; // CH0 - case ADC_PIN_1: - return 0xC4; // CH1 - case ADC_PIN_2: - return 0x94; // CH2 - case ADC_PIN_3: - return 0xD4; // CH3 - case ADC_PIN_4: - return 0xA4; // CH4 - case ADC_PIN_5: - return 0xE4; // CH5 - case ADC_PIN_6: - return 0xB4; // CH6 - case ADC_PIN_7: - return 0xF4; // CH7 - default: - return -1; // Invalid pin + int _get_ads7830_pin_address(adc_pin pin) { + switch (pin) { + case ADC_PIN_0: return 0x84; // Channel 0 + case ADC_PIN_1: return 0xC4; // Channel 1 + case ADC_PIN_2: return 0x94; // Channel 2 + case ADC_PIN_3: return 0xD4; // Channel 3 + case ADC_PIN_4: return 0xA4; // Channel 4 + case ADC_PIN_5: return 0xE4; // Channel 5 + case ADC_PIN_6: return 0xB4; // Channel 6 + case ADC_PIN_7: return 0xF4; // Channel 7 + default: return -1; // Or handle as an error } } - // const int CMD_CH0 = 0x84; bool has_adc_device(const string &name) { @@ -380,13 +368,13 @@ namespace splashkit_lib int test_result = sk_remote_i2c_write_byte(result->pi, result->i2c_handle, 0x84); if (test_result < 0) { - LOG(WARNING) << "Failed to communicate with ADC device " << name << " (write test byte failed)\n"; + LOG(WARNING) << "Failed to communicate with ADC device " << name << " (write test byte failed)"; sk_remote_i2c_close(result->pi, result->i2c_handle); delete result; return nullptr; } - LOG(INFO) << "ADC device " << name << " loaded on bus " << bus << " at address " << address << "\n"; + LOG(INFO) << "ADC device " << name << " loaded on bus " << bus << " at address " << address; // Transfer ownership to the map remote_adc_devices[name] = result; @@ -476,12 +464,12 @@ namespace splashkit_lib int remote_read_adc(const std::string& name, adc_pin channel) { _remote_adc_data* dev = remote_adc_device_named(name); if (dev == nullptr) { - LOG(ERROR) << "ADC device " << name << " not found.\n"; + LOG(ERROR) << "ADC device " << name << " not found."; return -1; } int channel_num = _get_ads7830_pin_address(channel); if (channel_num == -1) { - LOG(ERROR) << "Invalid ADC pin: " << channel << "\n"; + LOG(ERROR) << "Invalid ADC pin: " << channel <<; return -1; } return remote_read_adc_channel(remote_adc_devices.at(name), channel_num); @@ -492,28 +480,24 @@ namespace splashkit_lib remote_close_adc_device(adc); } - void remote_close_adc(const string &name) - { - // Find the device in the local map of remotely opened devices - auto it = remote_adc_devices.find(name); - if (it != remote_adc_devices.end()) { - remote_adc_device dev = it->second; - // Call the remote closing function - remote_close_adc_device(dev); - } else { - LOG(WARNING) << "Attempted to close unknown ADC device: " << name; - } + void remote_close_adc(const std::string &name) { + auto it = remote_adc_devices.find(name); + if (it != remote_adc_devices.end()) { + remote_adc_devices.erase(it); + } else { + LOG(WARNING) << "Attempted to close unknown ADC device: " << name; } +} void remote_close_adc_device(remote_adc_device dev) { auto it = remote_adc_devices.find(dev->name); if (it != remote_adc_devices.end()) { sk_remote_i2c_close(it->second->pi, it->second->i2c_handle); - LOG(INFO) << "Closed ADC device: " << dev->name << "\n"; + LOG(INFO) << "Closed ADC device: " << dev->name; remote_adc_devices.erase(it); } else { - LOG(WARNING) << "Attempted to close unknown ADC device: " << dev->name << "\n"; + LOG(WARNING) << "Attempted to close unknown ADC device: " << dev->name; } } }