Skip to content

Commit 4437b60

Browse files
committed
Add buffer flushing function to the Python module definition
Additional improves error checking and exitcode for this function to react if the buffer update has not been suceeded
1 parent f5d0bc0 commit 4437b60

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

native/framebuffers.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ void pyfb_init(void) {
1919
framebuffers[i].users = 0;
2020
framebuffers[i].fb_info.fb_size_b = 0;
2121
framebuffers[i].u32_buffer = NULL;
22-
23-
atomic_flag flag = ATOMIC_FLAG_INIT;
24-
25-
framebuffers[i].fb_lock = flag;
22+
atomic_flag flag = ATOMIC_FLAG_INIT;
23+
framebuffers[i].fb_lock = flag;
2624
}
2725
}
2826

@@ -110,9 +108,6 @@ int pyfb_open(uint8_t fbnum) {
110108

111109
if(buffer == NULL) {
112110
// got out of memory for the offscreen buffers
113-
if(buffer != NULL) {
114-
free(buffer);
115-
}
116111

117112
// free up all other resources for the new buffer
118113
close(fb_fd);
@@ -234,10 +229,10 @@ void pyfb_vinfo(uint8_t fbnum, struct pyfb_videomode_info* info_ptr) {
234229
unlock(framebuffers[fbnum].fb_lock);
235230
}
236231

237-
void pyfb_flushBuffer(uint8_t fbnum) {
232+
int pyfb_flushBuffer(uint8_t fbnum) {
238233
// first test if this device number is valid
239234
if(fbnum >= MAX_FRAMEBUFFERS) {
240-
return;
235+
return -1;
241236
}
242237

243238
lock(framebuffers[fbnum].fb_lock);
@@ -246,19 +241,27 @@ void pyfb_flushBuffer(uint8_t fbnum) {
246241
if(framebuffers[fbnum].fb_fd == -1) {
247242
// this framebuffer is not in use, so ignore
248243
unlock(framebuffers[fbnum].fb_lock);
249-
return;
244+
return -1;
250245
}
251246

252247
// if we get here, flush the offscreen buffer to the framebuffer
253-
lseek(framebuffers[fbnum].fb_fd, 0L, SEEK_SET);
248+
if(lseek(framebuffers[fbnum].fb_fd, 0L, SEEK_SET) == -1) {
249+
unlock(framebuffers[fbnum].fb_lock);
250+
return -1;
251+
}
252+
253+
size_t buf_len = (size_t)framebuffers[fbnum].fb_info.fb_size_b;
254+
unsigned long int exitcode = 0;
255+
254256
if(framebuffers[fbnum].fb_info.vinfo.bits_per_pixel == 32) {
255-
write(framebuffers[fbnum].fb_fd, (void*)framebuffers[fbnum].u32_buffer, (size_t)framebuffers[fbnum].fb_info.fb_size_b);
257+
exitcode = (unsigned int)write(framebuffers[fbnum].fb_fd, (void*)framebuffers[fbnum].u32_buffer, buf_len);
256258
} else {
257-
write(framebuffers[fbnum].fb_fd, (void*)framebuffers[fbnum].u16_buffer, (size_t)framebuffers[fbnum].fb_info.fb_size_b);
259+
exitcode = (unsigned int)write(framebuffers[fbnum].fb_fd, (void*)framebuffers[fbnum].u16_buffer, buf_len);
258260
}
259261

260262
// okay, ready flushed
261263
unlock(framebuffers[fbnum].fb_lock);
264+
return buf_len == exitcode ? 0 : -1;
262265
}
263266

264267
/**
@@ -308,6 +311,7 @@ void __APISTATUS_internal pyfb_setPixel(uint8_t fbnum,
308311
// do
309312
unsigned int xres = framebuffers[fbnum].fb_info.vinfo.xres;
310313
unsigned int width = framebuffers[fbnum].fb_info.vinfo.bits_per_pixel;
314+
311315
if(width == 16) {
312316
pyfb_pixel16(fbnum, x, y, color, xres);
313317
} else {
@@ -343,6 +347,7 @@ void pyfb_ssetPixel(uint8_t fbnum, unsigned long int x, unsigned long int y, con
343347

344348
// else all is okay and we can continue
345349
unsigned int width = framebuffers[fbnum].fb_info.vinfo.bits_per_pixel;
350+
346351
if(width == 16) {
347352
pyfb_pixel16(fbnum, x, y, color, xres);
348353
} else {
@@ -388,6 +393,7 @@ void pyfb_sdrawHorizontalLine(uint8_t fbnum,
388393
// Now test if the ranges are okay
389394
unsigned long int xres = framebuffers[fbnum].fb_info.vinfo.xres;
390395
unsigned long int yres = framebuffers[fbnum].fb_info.vinfo.yres;
396+
391397
if(y >= yres) {
392398
unlock(framebuffers[fbnum].fb_lock);
393399
return;
@@ -440,6 +446,7 @@ void pyfb_sdrawVerticalLine(uint8_t fbnum,
440446
// Now test if the ranges are okay
441447
unsigned long int xres = framebuffers[fbnum].fb_info.vinfo.xres;
442448
unsigned long int yres = framebuffers[fbnum].fb_info.vinfo.yres;
449+
443450
if(y >= yres || (y + len - 1) >= yres) {
444451
unlock(framebuffers[fbnum].fb_lock);
445452
return;

native/module_pyfb.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ static PyObject* pyfunc_pyfb_sdrawVerticalLine(PyObject* self, PyObject* args) {
140140
return PyLong_FromLong(exitcode);
141141
}
142142

143+
/**
144+
* Python wrapper for the pyfb_flushBuffer function.
145+
*
146+
* @param self The function
147+
* @param args The arguments, expecting long of the fbnum
148+
*
149+
* @return The exitstatus
150+
*/
151+
static PyObject* pyfunc_pyfb_flushBuffer(PyObject* self, PyObject* args) {
152+
unsigned char fbnum_c;
153+
154+
if(!PyArg_ParseTuple(args, "b", &fbnum_c)) {
155+
PyErr_SetString(PyExc_TypeError, "Expecting arguments of type (byte)");
156+
return NULL;
157+
}
158+
159+
// Now invoke the function
160+
int exitcode = pyfb_flushBuffer((uint8_t)fbnum_c);
161+
return PyLong_FromLong(exitcode);
162+
}
163+
143164
// The module def
144165

145166
/**
@@ -151,6 +172,7 @@ static PyMethodDef pyfb_methods[] = {
151172
{"pyfb_setPixel", pyfunc_pyfb_ssetPixel, METH_VARARGS, "Draw a pixel on the framebuffer"},
152173
{"pyfb_drawHorizontalLine", pyfunc_pyfb_sdrawHorizontalLine, METH_VARARGS, "Draw a horizontal line on the framebuffer"},
153174
{"pyfb_drawVerticalLine", pyfunc_pyfb_sdrawVerticalLine, METH_VARARGS, "Draw a vertical line on the framebuffer"},
175+
{"pyfb_flushBuffer", pyfunc_pyfb_flushBuffer, METH_VARARGS, "Flush the offscreen buffer to the framebuffer"},
154176
{NULL, NULL, 0, NULL}};
155177

156178
static struct PyModuleDef module__pyfb = {PyModuleDef_HEAD_INIT,

native/pyframebuffer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,9 @@ extern void __APISTATUS_internal pyfb_drawVerticalLine(uint8_t fbnum,
360360
* it says that it can take something between 20 and 100 milliseconds.
361361
*
362362
* @param fbnum The framebuffer number of which to flush all buffers
363+
*
364+
* @return If succeeded 0, else -1
363365
*/
364-
extern void pyfb_flushBuffer(uint8_t fbnum);
366+
extern int pyfb_flushBuffer(uint8_t fbnum);
365367

366368
#endif

0 commit comments

Comments
 (0)