Use dynamic buffer everywhere
This commit is contained in:
parent
289a9a1e34
commit
be5ea9174b
|
@ -26,16 +26,12 @@ void* do_display(void *arg) {
|
|||
display_t disp = *((display_t*) arg);
|
||||
pthread_t id = pthread_self();
|
||||
uint8_t h, w, y1, y2;
|
||||
uint8_t *buf;
|
||||
|
||||
printf("Thread %lu start\n", id);
|
||||
u8g2_t u8g2;
|
||||
// Initialization
|
||||
u8g2_Setup_ssd1306_i2c_128x32_univision_f(&u8g2, U8G2_R0, u8x8_byte_sw_i2c,
|
||||
u8x8_arm_linux_gpio_and_delay);
|
||||
// We need unique buffer for each display in order to be thread friendly
|
||||
buf = (uint8_t*)malloc(u8g2_GetBufferSize(&u8g2));
|
||||
u8g2_SetBufferPtr(&u8g2, buf);
|
||||
init_i2c_sw(&u8g2, disp.gpio_chip, disp.scl, disp.sda, disp.res,
|
||||
disp.delay);
|
||||
u8g2_InitDisplay(&u8g2);
|
||||
|
@ -54,7 +50,6 @@ void* do_display(void *arg) {
|
|||
u8g2_SetPowerSave(&u8g2, 1);
|
||||
// Close and deallocate GPIO resources
|
||||
done_user_data(&u8g2);
|
||||
free(buf);
|
||||
printf("Thread %lu end\n", id);
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -142,7 +142,8 @@ public:
|
|||
}
|
||||
void initSpiHw(uint8_t gpio_chip, uint8_t bus, uint8_t dc, uint8_t res,
|
||||
uint8_t cs, unsigned int spi_mode, uint32_t max_speed) {
|
||||
init_spi_hw_advanced(&u8g2, gpio_chip, bus, dc, res, cs, spi_mode, max_speed);
|
||||
init_spi_hw_advanced(&u8g2, gpio_chip, bus, dc, res, cs, spi_mode,
|
||||
max_speed);
|
||||
}
|
||||
void initSpiHw(uint8_t gpio_chip, uint8_t bus, uint8_t dc, uint8_t res,
|
||||
uint8_t cs) {
|
||||
|
@ -209,15 +210,10 @@ public:
|
|||
}
|
||||
|
||||
bool begin(void) {
|
||||
/* note: call to u8x8_utf8_init is not required here, this is done in the setup procedures before */
|
||||
#ifndef U8G2_USE_DYNAMIC_ALLOC
|
||||
initDisplay();
|
||||
clearDisplay();
|
||||
setPowerSave(0);
|
||||
return 1;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void beginSimple(void) {
|
||||
|
|
|
@ -48,8 +48,12 @@ void sleep_ns(unsigned long nanoseconds) {
|
|||
* Allocate user_data_struct, set common values and set user_ptr.
|
||||
*/
|
||||
user_data_t* init_user_data(u8g2_t *u8g2) {
|
||||
// Dynamically allocate u8x8_buffer_struct
|
||||
// Dynamically allocate user data_struct
|
||||
user_data_t *user_data = (user_data_t*) malloc(sizeof(user_data_t));
|
||||
// Dynamically allocate internal buffer
|
||||
user_data->int_buf = (uint8_t*)malloc(u8g2_GetBufferSize(u8g2));
|
||||
// We need a unique buffer for each display in order to be thread friendly
|
||||
u8g2_SetBufferPtr(u8g2, user_data->int_buf);
|
||||
for (int i = 0; i < U8X8_PIN_CNT; ++i) {
|
||||
user_data->pins[i] = NULL;
|
||||
}
|
||||
|
@ -124,12 +128,16 @@ void init_spi_sw(u8g2_t *u8g2, uint8_t gpio_chip, uint8_t dc, uint8_t res,
|
|||
void done_user_data(u8g2_t *u8g2) {
|
||||
user_data_t *user_data = u8g2_GetUserPtr(u8g2);
|
||||
if (user_data != NULL) {
|
||||
// Close all GPIO pins
|
||||
for (int i = 0; i < U8X8_PIN_CNT; ++i) {
|
||||
if (user_data->pins[i] != NULL) {
|
||||
gpio_close(user_data->pins[i]);
|
||||
gpio_free(user_data->pins[i]);
|
||||
}
|
||||
}
|
||||
// Free internal buffer
|
||||
free(user_data->int_buf);
|
||||
// Free user data struct
|
||||
free(user_data);
|
||||
u8g2_SetUserPtr(u8g2, NULL);
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ struct user_data_struct {
|
|||
uint8_t bus;
|
||||
// Index into buffer
|
||||
uint8_t index;
|
||||
// Display buffer, I2C should send 32 bytes max and SPI 128 bytes max
|
||||
// Callback buffer, I2C should send 32 bytes max and SPI 128 bytes max
|
||||
uint8_t buffer[128];
|
||||
// Nanosecond delay for U8X8_MSG_DELAY_I2C
|
||||
unsigned long delay;
|
||||
|
@ -36,6 +36,8 @@ struct user_data_struct {
|
|||
unsigned int spi_mode;
|
||||
// SPI max speed
|
||||
uint32_t max_speed;
|
||||
// Internal buffer
|
||||
uint8_t *int_buf;
|
||||
};
|
||||
|
||||
typedef struct user_data_struct user_data_t;
|
||||
|
|
Loading…
Reference in New Issue