adapted code to use the wintype.h types and the error codes

This commit is contained in:
Amy G. Bowersox 2019-12-05 18:21:37 -07:00
parent 0849582c7b
commit 09f4b5ceb5
11 changed files with 91 additions and 74 deletions

View File

@ -8,35 +8,39 @@
#include <linux/fb.h> #include <linux/fb.h>
#include "log.h" #include "log.h"
#include "fbinit.h" #include "fbinit.h"
#include "scode.h"
static int fb_fd = -1; static int fb_fd = -1;
static FBINFO local_info; static FBINFO local_info;
PCFBINFO Fb_Info = &local_info; PCFBINFO Fb_Info = &local_info;
uint16_t *Fb_Ptr = NULL; UINT16 *Fb_Ptr = NULL;
inline static unsigned makemask(unsigned offset, unsigned length) inline static UINT16 makemask(unsigned offset, unsigned length)
{ {
return ((1 << length) - 1) << offset; return (UINT16)(((1 << length) - 1) << offset);
} }
int Fb_setup(void) HRESULT Fb_setup(void)
{ {
HRESULT hr = S_OK;
struct fb_fix_screeninfo fixed; struct fb_fix_screeninfo fixed;
struct fb_var_screeninfo var; struct fb_var_screeninfo var;
fb_fd = open("/dev/fb1", O_RDWR); fb_fd = open("/dev/fb1", O_RDWR);
if (fb_fd == -1) if (fb_fd == -1)
{ {
Log(LFATAL, "Unable to open framebuffer (%d)", errno); hr = ERRNO_AS_SCODE;
return -1; Log(LFATAL, "Unable to open framebuffer (%08X)", hr);
return hr;
} }
if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &fixed)) if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &fixed))
{ {
Log(LFATAL, "Could not get fixed screen info (%d)", errno); hr = ERRNO_AS_SCODE;
return -1; Log(LFATAL, "Could not get fixed screen info (%08X)", hr);
return hr;
} }
local_info.linebytes = fixed.line_length; local_info.linebytes = fixed.line_length;
@ -44,8 +48,9 @@ int Fb_setup(void)
if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &var)) if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &var))
{ {
Log(LFATAL, "Could not get variable screen info (%d)", errno); hr = ERRNO_AS_SCODE;
return -1; Log(LFATAL, "Could not get variable screen info (%08X)", hr);
return hr;
} }
local_info.width = var.xres; local_info.width = var.xres;
@ -63,19 +68,20 @@ int Fb_setup(void)
local_info.blue_length = var.blue.length; local_info.blue_length = var.blue.length;
local_info.blue_mask = makemask(var.blue.offset, var.blue.length); local_info.blue_mask = makemask(var.blue.offset, var.blue.length);
Fb_Ptr = (uint16_t *)mmap(0, fixed.smem_len, PROT_READ|PROT_WRITE, MAP_SHARED, fb_fd, 0); Fb_Ptr = (UINT16 *)mmap(0, fixed.smem_len, PROT_READ|PROT_WRITE, MAP_SHARED, fb_fd, 0);
if ((int)Fb_Ptr == -1) if ((int)Fb_Ptr == -1)
{ {
Log(LFATAL, "Unable to memmap framebuffer (%d)", errno); hr = ERRNO_AS_SCODE;
Log(LFATAL, "Unable to memmap framebuffer (%08X)", hr);
Fb_Ptr = NULL; Fb_Ptr = NULL;
close(fb_fd); close(fb_fd);
fb_fd = -1; fb_fd = -1;
return -1; return hr;
} }
/* additional setup here */ /* additional setup here */
return 0; return hr;
} }
void Fb_cleanup(void) void Fb_cleanup(void)

View File

@ -1,32 +1,32 @@
#ifndef __FBINIT_H_INCLUDED #ifndef __FBINIT_H_INCLUDED
#define __FBINIT_H_INCLUDED #define __FBINIT_H_INCLUDED
#include <stdint.h> #include "wintype.h"
typedef struct tagFBINFO { typedef struct tagFBINFO {
uint32_t width; UINT32 width;
uint32_t height; UINT32 height;
uint32_t virtual_width; UINT32 virtual_width;
uint32_t virtual_height; UINT32 virtual_height;
uint32_t bpp; UINT32 bpp;
uint32_t linebytes; UINT32 linebytes;
uint32_t screenbytes; UINT32 screenbytes;
uint16_t red_offset; UINT16 red_offset;
uint16_t red_length; UINT16 red_length;
uint16_t red_mask; UINT16 red_mask;
uint16_t green_offset; UINT16 green_offset;
uint16_t green_length; UINT16 green_length;
uint16_t green_mask; UINT16 green_mask;
uint16_t blue_offset; UINT16 blue_offset;
uint16_t blue_length; UINT16 blue_length;
uint16_t blue_mask; UINT16 blue_mask;
} FBINFO; } FBINFO;
typedef const FBINFO * const PCFBINFO; typedef const FBINFO * const PCFBINFO;
extern PCFBINFO Fb_Info; extern PCFBINFO Fb_Info;
extern uint16_t *Fb_Ptr; extern UINT16 *Fb_Ptr;
extern int Fb_setup(void); extern HRESULT Fb_setup(void);
extern void Fb_cleanup(void); extern void Fb_cleanup(void);
#endif /* __FBINIT_H_INCLUDED */ #endif /* __FBINIT_H_INCLUDED */

View File

@ -1,6 +1,7 @@
#include <stdint.h> #include <stdint.h>
#include <bcm2835.h> #include <bcm2835.h>
#include "log.h" #include "log.h"
#include "scode.h"
#include "gpio.h" #include "gpio.h"
#define GLINE_BUTTON1 17 #define GLINE_BUTTON1 17
@ -11,12 +12,12 @@
#define PWM_BACKLIGHT 0 #define PWM_BACKLIGHT 0
int Gpio_setup(void) HRESULT Gpio_setup(void)
{ {
if (!bcm2835_init()) if (!bcm2835_init())
{ {
Log(LFATAL, "Error initializing BCM2835 library"); Log(LFATAL, "Error initializing BCM2835 library");
return -1; return E_FAIL;
} }
/* configure the buttons */ /* configure the buttons */
@ -37,7 +38,7 @@ int Gpio_setup(void)
bcm2835_pwm_set_range(PWM_BACKLIGHT, GSB_BACKLIGHT_MAX + 1); bcm2835_pwm_set_range(PWM_BACKLIGHT, GSB_BACKLIGHT_MAX + 1);
bcm2835_pwm_set_data(PWM_BACKLIGHT, GSB_BACKLIGHT_MAX); bcm2835_pwm_set_data(PWM_BACKLIGHT, GSB_BACKLIGHT_MAX);
return 0; return S_OK;
} }
void Gpio_cleanup(void) void Gpio_cleanup(void)
@ -61,9 +62,9 @@ void Gpio_cleanup(void)
Log(LWARN, "Closing BCM2835 library failed"); Log(LWARN, "Closing BCM2835 library failed");
} }
int Gpio_read_buttons(void) UINT32 Gpio_read_buttons(void)
{ {
int rc = 0; UINT32 rc = 0;
if (bcm2835_gpio_lev(GLINE_BUTTON1) == LOW) if (bcm2835_gpio_lev(GLINE_BUTTON1) == LOW)
rc |= GRB_STATE_BUTTON1; rc |= GRB_STATE_BUTTON1;
@ -76,7 +77,7 @@ int Gpio_read_buttons(void)
return rc; return rc;
} }
void Gpio_set_backlight(unsigned level) void Gpio_set_backlight(UINT32 level)
{ {
if (level > GSB_BACKLIGHT_MAX) if (level > GSB_BACKLIGHT_MAX)
level = GSB_BACKLIGHT_MAX; level = GSB_BACKLIGHT_MAX;

View File

@ -1,6 +1,8 @@
#ifndef __GPIO_H_INCLUDED #ifndef __GPIO_H_INCLUDED
#define __GPIO_H_INCLUDED #define __GPIO_H_INCLUDED
#include "wintype.h"
#define GPIO_BUTTON_COUNT 4 #define GPIO_BUTTON_COUNT 4
#define GRB_STATE_BUTTON1 (1 << 0) #define GRB_STATE_BUTTON1 (1 << 0)
@ -10,9 +12,9 @@
#define GSB_BACKLIGHT_MAX 1023 #define GSB_BACKLIGHT_MAX 1023
extern int Gpio_setup(void); extern HRESULT Gpio_setup(void);
extern void Gpio_cleanup(void); extern void Gpio_cleanup(void);
extern int Gpio_read_buttons(void); extern UINT32 Gpio_read_buttons(void);
extern void Gpio_set_backlight(unsigned level); extern void Gpio_set_backlight(UINT32 level);
#endif /* __GPIO_H_INCLUDED */ #endif /* __GPIO_H_INCLUDED */

View File

@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "scode.h"
#include "gpio.h" #include "gpio.h"
#include "log.h" #include "log.h"
#include "fbinit.h" #include "fbinit.h"
@ -13,9 +14,9 @@ static void do_draw(void)
memset(tmp, 0xFF, Fb_Info->screenbytes / 2); memset(tmp, 0xFF, Fb_Info->screenbytes / 2);
memset(tmp + (Fb_Info->screenbytes / 2), 0x1B, Fb_Info->screenbytes / 2); memset(tmp + (Fb_Info->screenbytes / 2), 0x1B, Fb_Info->screenbytes / 2);
*/ */
uint16_t pixel = Fb_Info->green_mask; UINT16 pixel = Fb_Info->green_mask;
unsigned npix = Fb_Info->screenbytes / sizeof(uint16_t); unsigned npix = Fb_Info->screenbytes / sizeof(UINT16);
uint16_t *p = Fb_Ptr; UINT16 *p = Fb_Ptr;
unsigned i; unsigned i;
for (i=0; i<npix; i++) for (i=0; i<npix; i++)
@ -29,13 +30,13 @@ int main(int argc, char *argv[])
char *tmp; char *tmp;
Time_init(); Time_init();
if (Fb_setup() != 0) if (FAILED(Fb_setup()))
return EXIT_FAILURE; return EXIT_FAILURE;
atexit(Fb_cleanup); atexit(Fb_cleanup);
if (Gpio_setup() != 0) if (FAILED(Gpio_setup()))
return EXIT_FAILURE; return EXIT_FAILURE;
atexit(Gpio_cleanup); atexit(Gpio_cleanup);
if (Sys_enable_input() != 0) if (FAILED(Sys_enable_input()))
return EXIT_FAILURE; return EXIT_FAILURE;
atexit(Sys_disable_input); atexit(Sys_disable_input);

View File

@ -6,9 +6,9 @@
#define MSG_ATTRCOUNT 2 #define MSG_ATTRCOUNT 2
typedef struct tagMSG { typedef struct tagMSG {
uintptr_t target; HANDLE target;
unsigned message; UINT32 message;
uintptr_t attrs[MSG_ATTRCOUNT]; UINT_PTR attrs[MSG_ATTRCOUNT];
TIMESTAMP timestamp; TIMESTAMP timestamp;
} MSG, *PMSG; } MSG, *PMSG;

View File

@ -5,7 +5,7 @@
#include "time_func.h" #include "time_func.h"
#include "msg_queue.h" #include "msg_queue.h"
PMSG_QUEUE Mq_alloc(int nentries) PMSG_QUEUE Mq_alloc(UINT32 nentries)
{ {
int sz = sizeof(MSG_QUEUE) + (nentries * sizeof(MSG)); int sz = sizeof(MSG_QUEUE) + (nentries * sizeof(MSG));
PMSG_QUEUE rc; PMSG_QUEUE rc;
@ -46,7 +46,7 @@ static void post_internal(PMSG_QUEUE queue, PMSG msg)
pthread_mutex_unlock(&(queue->mutex)); pthread_mutex_unlock(&(queue->mutex));
} }
void Mq_post(PMSG_QUEUE queue, uintptr_t target, unsigned message, const uintptr_t *attrs, int nattrs) void Mq_post(PMSG_QUEUE queue, HANDLE target, UINT32 message, const UINT_PTR *attrs, int nattrs)
{ {
MSG tmpmsg; MSG tmpmsg;
@ -61,7 +61,7 @@ void Mq_post(PMSG_QUEUE queue, uintptr_t target, unsigned message, const uintptr
post_internal(queue, &tmpmsg); post_internal(queue, &tmpmsg);
} }
void Mq_post1(PMSG_QUEUE queue, uintptr_t target, unsigned message, uintptr_t attr1) void Mq_post1(PMSG_QUEUE queue, HANDLE target, UINT32 message, UINT_PTR attr1)
{ {
MSG tmpmsg; MSG tmpmsg;
@ -73,9 +73,9 @@ void Mq_post1(PMSG_QUEUE queue, uintptr_t target, unsigned message, uintptr_t at
post_internal(queue, &tmpmsg); post_internal(queue, &tmpmsg);
} }
int Mq_peek(PMSG_QUEUE queue, PMSG msg, unsigned flags) BOOL Mq_peek(PMSG_QUEUE queue, PMSG msg, UINT32 flags)
{ {
int rc = 0; BOOL rc = FALSE;
PMSG nexthead; PMSG nexthead;
pthread_mutex_lock(&(queue->mutex)); pthread_mutex_lock(&(queue->mutex));
@ -87,7 +87,7 @@ int Mq_peek(PMSG_QUEUE queue, PMSG msg, unsigned flags)
memcpy(msg, queue->head, sizeof(MSG)); memcpy(msg, queue->head, sizeof(MSG));
if (flags & PEEK_REMOVE) if (flags & PEEK_REMOVE)
queue->head = nexthead; queue->head = nexthead;
rc = 1; rc = TRUE;
} }
pthread_mutex_unlock(&(queue->mutex)); pthread_mutex_unlock(&(queue->mutex));
return rc; return rc;

View File

@ -10,7 +10,7 @@ typedef struct tagMSG_QUEUE {
PMSG endbound; PMSG endbound;
PMSG head; PMSG head;
PMSG tail; PMSG tail;
int nentries; UINT32 nentries;
pthread_mutex_t mutex; pthread_mutex_t mutex;
MSG messagestore[0]; MSG messagestore[0];
} MSG_QUEUE, *PMSG_QUEUE; } MSG_QUEUE, *PMSG_QUEUE;
@ -18,10 +18,10 @@ typedef struct tagMSG_QUEUE {
#define PEEK_REMOVE 0x0001 #define PEEK_REMOVE 0x0001
#define PEEK_NOREMOVE 0x0000 #define PEEK_NOREMOVE 0x0000
extern PMSG_QUEUE Mq_alloc(int nentries); extern PMSG_QUEUE Mq_alloc(UINT32 nentries);
extern void Mq_destroy(PMSG_QUEUE queue); extern void Mq_destroy(PMSG_QUEUE queue);
extern void Mq_post(PMSG_QUEUE queue, uintptr_t target, unsigned message, const uintptr_t *attrs, int nattrs); extern void Mq_post(PMSG_QUEUE queue, HANDLE target, UINT32 message, const UINT_PTR *attrs, int nattrs);
extern void Mq_post1(PMSG_QUEUE queue, uintptr_t target, unsigned message, uintptr_t attr1); extern void Mq_post1(PMSG_QUEUE queue, HANDLE target, UINT32 message, UINT_PTR attr1);
extern int Mq_peek(PMSG_QUEUE queue, PMSG msg, unsigned flags); extern BOOL Mq_peek(PMSG_QUEUE queue, PMSG msg, UINT32 flags);
#endif /* __MSG_QUEUE_H_INCLUDED */ #endif /* __MSG_QUEUE_H_INCLUDED */

View File

@ -1,7 +1,7 @@
#ifndef __SCODE_H_INCLUDED #ifndef __SCODE_H_INCLUDED
#define __SCODE_H_INCLUDED #define __SCODE_H_INCLUDED
#include <wintype.h> #include "wintype.h"
/*------------------------------------------------------------------- /*-------------------------------------------------------------------
* Status codes are defined as follows: * Status codes are defined as follows:
@ -37,7 +37,8 @@
#define MAKE_SCODE(sev, fac, err) ((SCODE)((sev) | (((fac) & 0x7FFF) << 16) | ((err) & 0xFFFF))) #define MAKE_SCODE(sev, fac, err) ((SCODE)((sev) | (((fac) & 0x7FFF) << 16) | ((err) & 0xFFFF)))
#define ERRNO_AS_SCODE MAKE_SCODE(SEVERITY_ERROR, FACILITY_UNIX, errno) #define SCODE_FROM_ERRNO(x) MAKE_SCODE(SEVERITY_ERROR, FACILITY_UNIX, (x))
#define ERRNO_AS_SCODE SCODE_FROM_ERRNO(errno)
#define SCODE_CAST(x) ((SCODE)(x)) #define SCODE_CAST(x) ((SCODE)(x))

View File

@ -1,6 +1,7 @@
#include <stddef.h> #include <stddef.h>
#include <signal.h> #include <signal.h>
#include <pthread.h> #include <pthread.h>
#include "scode.h"
#include "log.h" #include "log.h"
#include "msg_queue.h" #include "msg_queue.h"
#include "gpio.h" #include "gpio.h"
@ -9,12 +10,12 @@ PMSG_QUEUE Sys_Queue = NULL;
static pthread_t ithread; static pthread_t ithread;
static volatile sig_atomic_t running = 1; static volatile sig_atomic_t running = 1;
static int last_bstate = 0; static UINT32 last_bstate = 0;
static void *input_thread(void *arg) static void *input_thread(void *arg)
{ {
int st, down, up, mask; UINT32 st, down, up, mask;
uintptr_t attr; UINT_PTR attr;
while (running) while (running)
{ {
@ -39,20 +40,24 @@ static void *input_thread(void *arg)
return NULL; return NULL;
} }
int Sys_enable_input(void) HRESULT Sys_enable_input(void)
{ {
int rc; HRESULT rc = S_OK;
int threadrc;
Sys_Queue = Mq_alloc(64); Sys_Queue = Mq_alloc(64);
if (!Sys_Queue) if (!Sys_Queue)
{ {
Log(LFATAL, "Unable to allocate system message queue."); Log(LFATAL, "Unable to allocate system message queue.");
return 1; return E_OUTOFMEMORY;
} }
running = 1; running = 1;
rc = pthread_create(&ithread, NULL, input_thread, NULL); threadrc = pthread_create(&ithread, NULL, input_thread, NULL);
if (rc != 0) if (threadrc != 0)
Log(LFATAL, "Unable to start system input thread (%d).", rc); {
rc = SCODE_FROM_ERRNO(threadrc);
Log(LFATAL, "Unable to start system input thread (%08X).", rc);
}
return rc; return rc;
} }

View File

@ -1,11 +1,12 @@
#ifndef __SYSINPUT_H_INCLUDED #ifndef __SYSINPUT_H_INCLUDED
#define __SYSINPUT_H_INCLUDED #define __SYSINPUT_H_INCLUDED
#include "wintype.h"
#include "msg_queue.h" #include "msg_queue.h"
extern PMSG_QUEUE Sys_Queue; extern PMSG_QUEUE Sys_Queue;
extern int Sys_enable_input(void); extern HRESULT Sys_enable_input(void);
extern void Sys_disable_input(void); extern void Sys_disable_input(void);
#endif /* __SYSINPUT_H_INCLUDED */ #endif /* __SYSINPUT_H_INCLUDED */