Private
Public Access
2
0

examples: Remove heap allocation from C++ examples

Small cleanup of MRAA C++ examples.  Switched from heap allocation to
stack allocation when possible.  This simplifies the samples since it
removes the need for explicit memory management.

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck
2017-08-08 13:55:23 -07:00
parent d50b646c35
commit 1217c5c034
7 changed files with 29 additions and 47 deletions

View File

@@ -44,20 +44,17 @@ main()
{ {
uint16_t adc_value; uint16_t adc_value;
float adc_value_float; float adc_value_float;
mraa::Aio* a0; mraa::Aio a0(0);
a0 = new mraa::Aio(0);
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
while (running == 0) { while (running == 0) {
adc_value = a0->read(); adc_value = a0.read();
adc_value_float = a0->readFloat(); adc_value_float = a0.readFloat();
fprintf(stdout, "ADC A0 read %X - %d\n", adc_value, adc_value); fprintf(stdout, "ADC A0 read %X - %d\n", adc_value, adc_value);
fprintf(stdout, "ADC A0 read float - %.5f (Ctrl+C to exit)\n", adc_value_float); fprintf(stdout, "ADC A0 read float - %.5f (Ctrl+C to exit)\n", adc_value_float);
} }
delete a0;
return MRAA_SUCCESS; return MRAA_SUCCESS;
} }
//! [Interesting] //! [Interesting]

View File

@@ -55,21 +55,19 @@ main(int argc, char** argv)
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
mraa::Gpio* gpio = new mraa::Gpio(iopin); mraa::Gpio gpio(iopin);
mraa::Result response = gpio->dir(mraa::DIR_OUT); mraa::Result response = gpio.dir(mraa::DIR_OUT);
if (response != mraa::SUCCESS) { if (response != mraa::SUCCESS) {
mraa::printError(response); mraa::printError(response);
delete gpio;
return 1; return 1;
} }
while (running == 0) { while (running == 0) {
response = gpio->write(1); response = gpio.write(1);
sleep(1); sleep(1);
response = gpio->write(0); response = gpio.write(0);
sleep(1); sleep(1);
} }
delete gpio;
return response; return response;
//! [Interesting] //! [Interesting]
} }

View File

@@ -99,28 +99,27 @@ main()
uint8_t rx_tx_buf[MAX_BUFFER_LENGTH]; uint8_t rx_tx_buf[MAX_BUFFER_LENGTH];
//! [Interesting] //! [Interesting]
mraa::I2c* i2c; mraa::I2c i2c(0);
i2c = new mraa::I2c(0);
i2c->address(HMC5883L_I2C_ADDR); i2c.address(HMC5883L_I2C_ADDR);
rx_tx_buf[0] = HMC5883L_CONF_REG_B; rx_tx_buf[0] = HMC5883L_CONF_REG_B;
rx_tx_buf[1] = GA_1_3_REG; rx_tx_buf[1] = GA_1_3_REG;
i2c->write(rx_tx_buf, 2); i2c.write(rx_tx_buf, 2);
//! [Interesting] //! [Interesting]
i2c->address(HMC5883L_I2C_ADDR); i2c.address(HMC5883L_I2C_ADDR);
rx_tx_buf[0] = HMC5883L_MODE_REG; rx_tx_buf[0] = HMC5883L_MODE_REG;
rx_tx_buf[1] = HMC5883L_CONT_MODE; rx_tx_buf[1] = HMC5883L_CONT_MODE;
i2c->write(rx_tx_buf, 2); i2c.write(rx_tx_buf, 2);
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
while (running == 0) { while (running == 0) {
i2c->address(HMC5883L_I2C_ADDR); i2c.address(HMC5883L_I2C_ADDR);
i2c->writeByte(HMC5883L_DATA_REG); i2c.writeByte(HMC5883L_DATA_REG);
i2c->address(HMC5883L_I2C_ADDR); i2c.address(HMC5883L_I2C_ADDR);
i2c->read(rx_tx_buf, DATA_REG_SIZE); i2c.read(rx_tx_buf, DATA_REG_SIZE);
x = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8) | rx_tx_buf[HMC5883L_X_LSB_REG]; x = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8) | rx_tx_buf[HMC5883L_X_LSB_REG];
z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8) | rx_tx_buf[HMC5883L_Z_LSB_REG]; z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8) | rx_tx_buf[HMC5883L_Z_LSB_REG];
@@ -138,7 +137,6 @@ main()
printf("Heading : %f\n", direction * 180 / M_PI); printf("Heading : %f\n", direction * 180 / M_PI);
sleep(1); sleep(1);
} }
delete i2c;
return MRAA_SUCCESS; return MRAA_SUCCESS;
} }

View File

@@ -39,11 +39,11 @@ interrupt(void* args)
int int
main() main()
{ {
mraa::Gpio* x = new mraa::Gpio(6); mraa::Gpio x(6);
x->dir(mraa::DIR_IN); x.dir(mraa::DIR_IN);
x->isr(mraa::EDGE_BOTH, &interrupt, NULL); x.isr(mraa::EDGE_BOTH, &interrupt, NULL);
int i = 100; int i = 100;
for (; i > 0; --i) { for (; i > 0; --i) {
@@ -55,6 +55,5 @@ main()
sleep(1); sleep(1);
} }
delete x;
return MRAA_SUCCESS; return MRAA_SUCCESS;
} }

View File

@@ -43,22 +43,19 @@ main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
mraa::Pwm* pwm; mraa::Pwm pwm(3);
pwm = new mraa::Pwm(3);
fprintf(stdout, "Cycling PWM on IO3 (pwm3) \n"); fprintf(stdout, "Cycling PWM on IO3 (pwm3) \n");
pwm->enable(true); pwm.enable(true);
float value = 0.0f; float value = 0.0f;
while (running == 0) { while (running == 0) {
value = value + 0.01f; value = value + 0.01f;
pwm->write(value); pwm.write(value);
usleep(50000); usleep(50000);
if (value >= 1.0f) { if (value >= 1.0f) {
value = 0.0f; value = 0.0f;
} }
} }
delete pwm;
//! [Interesting] //! [Interesting]
return MRAA_SUCCESS; return MRAA_SUCCESS;

View File

@@ -45,9 +45,7 @@ main()
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]
mraa::Spi* spi; mraa::Spi spi(0);
spi = new mraa::Spi(0);
uint8_t data[] = { 0x00, 100 }; uint8_t data[] = { 0x00, 100 };
uint8_t rxBuf[2]; uint8_t rxBuf[2];
@@ -56,7 +54,7 @@ main()
int i; int i;
for (i = 90; i < 130; i++) { for (i = 90; i < 130; i++) {
data[1] = i; data[1] = i;
recv = spi->write(data, 2); recv = spi.write(data, 2);
printf("Writing -%i", i); printf("Writing -%i", i);
if (recv) { if (recv) {
printf("RECIVED-%i-%i\n", recv[0], recv[1]); printf("RECIVED-%i-%i\n", recv[0], recv[1]);
@@ -66,14 +64,13 @@ main()
} }
for (i = 130; i > 90; i--) { for (i = 130; i > 90; i--) {
data[1] = i; data[1] = i;
if (spi->transfer(data, rxBuf, 2) == mraa::SUCCESS) { if (spi.transfer(data, rxBuf, 2) == mraa::SUCCESS) {
printf("Writing -%i", i); printf("Writing -%i", i);
printf("RECIVED-%i-%i\n", rxBuf[0], rxBuf[1]); printf("RECIVED-%i-%i\n", rxBuf[0], rxBuf[1]);
} }
usleep(100000); usleep(100000);
} }
} }
delete spi;
//! [Interesting] //! [Interesting]
return mraa::SUCCESS; return mraa::SUCCESS;

View File

@@ -30,16 +30,15 @@
int int
main(int argc, char** argv) main(int argc, char** argv)
{ {
mraa::UartOW* uart = new mraa::UartOW(0); mraa::UartOW uart(0);
// Reset the ow bus and see if anything is present // Reset the ow bus and see if anything is present
mraa::Result rv; mraa::Result rv;
if ((rv = uart->reset()) == mraa::SUCCESS) { if ((rv = uart.reset()) == mraa::SUCCESS) {
std::cout << "Reset succeeded, device(s) detected!" << std::endl; std::cout << "Reset succeeded, device(s) detected!" << std::endl;
} else { } else {
std::cout << "Reset failed, returned " << int(rv) << ". No devices on bus?" << std::endl; std::cout << "Reset failed, returned " << int(rv) << ". No devices on bus?" << std::endl;
delete uart;
return 1; return 1;
} }
@@ -48,11 +47,10 @@ main(int argc, char** argv)
uint8_t count = 0; uint8_t count = 0;
// start the search from scratch // start the search from scratch
std::string id = uart->search(true); std::string id = uart.search(true);
if (id.empty()) { if (id.empty()) {
std::cout << "No devices detected." << std::endl; std::cout << "No devices detected." << std::endl;
delete uart;
return 1; return 1;
} }
@@ -70,13 +68,11 @@ main(int argc, char** argv)
count++; count++;
// continue the search with start argument set to false // continue the search with start argument set to false
id = uart->search(false); id = uart.search(false);
} }
std::cout << "Exiting..." << std::endl; std::cout << "Exiting..." << std::endl;
delete uart;
return 0; return 0;
} }
//! [Interesting] //! [Interesting]