code cleanup

This commit is contained in:
Amy G. Bowersox 2019-12-01 15:43:05 -07:00
parent 05e1028211
commit 2f6118d436
9 changed files with 43 additions and 46 deletions

View File

@ -1,9 +1,4 @@
#include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <bcm2835.h> #include <bcm2835.h>
#include "log.h" #include "log.h"
#include "gpio.h" #include "gpio.h"
@ -39,8 +34,8 @@ int Gpio_setup(void)
bcm2835_gpio_fsel(GLINE_BACKLIGHT, BCM2835_GPIO_FSEL_ALT5); bcm2835_gpio_fsel(GLINE_BACKLIGHT, BCM2835_GPIO_FSEL_ALT5);
bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_2); bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_2);
bcm2835_pwm_set_mode(PWM_BACKLIGHT, 1, 1); bcm2835_pwm_set_mode(PWM_BACKLIGHT, 1, 1);
bcm2835_pwm_set_range(PWM_BACKLIGHT, BACKLIGHT_MAX + 1); bcm2835_pwm_set_range(PWM_BACKLIGHT, GSB_BACKLIGHT_MAX + 1);
bcm2835_pwm_set_data(PWM_BACKLIGHT, BACKLIGHT_MAX); bcm2835_pwm_set_data(PWM_BACKLIGHT, GSB_BACKLIGHT_MAX);
return 0; return 0;
} }
@ -83,7 +78,7 @@ int Gpio_read_buttons(void)
void Gpio_set_backlight(unsigned level) void Gpio_set_backlight(unsigned level)
{ {
if (level > BACKLIGHT_MAX) if (level > GSB_BACKLIGHT_MAX)
level = BACKLIGHT_MAX; level = GSB_BACKLIGHT_MAX;
bcm2835_pwm_set_data(PWM_BACKLIGHT, level); bcm2835_pwm_set_data(PWM_BACKLIGHT, level);
} }

View File

@ -1,12 +1,14 @@
#ifndef __GPIO_H_INCLUDED #ifndef __GPIO_H_INCLUDED
#define __GPIO_H_INCLUDED #define __GPIO_H_INCLUDED
#define STATE_BUTTON1 (1 << 0) #define GPIO_BUTTON_COUNT 4
#define STATE_BUTTON2 (1 << 1)
#define STATE_BUTTON3 (1 << 2)
#define STATE_BUTTON4 (1 << 3)
#define BACKLIGHT_MAX 1023 #define GRB_STATE_BUTTON1 (1 << 0)
#define GRB_STATE_BUTTON2 (1 << 1)
#define GRB_STATE_BUTTON3 (1 << 2)
#define GRB_STATE_BUTTON4 (1 << 3)
#define GSB_BACKLIGHT_MAX 1023
extern int Gpio_setup(void); extern int Gpio_setup(void);
extern void Gpio_cleanup(void); extern void Gpio_cleanup(void);

View File

@ -9,19 +9,19 @@ int main(int argc, char *argv[])
int running = 1; int running = 1;
MSG msg; MSG msg;
TimeInit(); Time_init();
if (Gpio_setup() != 0) if (Gpio_setup() != 0)
return EXIT_FAILURE; return EXIT_FAILURE;
atexit(Gpio_cleanup); atexit(Gpio_cleanup);
if (SysEnableInput() != 0) if (Sys_enable_input() != 0)
return EXIT_FAILURE; return EXIT_FAILURE;
atexit(SysDisableInput); atexit(Sys_disable_input);
Log(LINFO, "System ready."); Log(LINFO, "System ready.");
while (running) while (running)
{ {
if (MqPeek(Sys_Queue, &msg, PEEK_REMOVE)) if (Mq_peek(Sys_Queue, &msg, PEEK_REMOVE))
{ {
switch (msg.message) switch (msg.message)
{ {
@ -34,7 +34,7 @@ int main(int argc, char *argv[])
if (msg.attrs[0] == 1) if (msg.attrs[0] == 1)
{ {
Log(LINFO, "Backlight ON."); Log(LINFO, "Backlight ON.");
Gpio_set_backlight(BACKLIGHT_MAX); Gpio_set_backlight(GRB_BACKLIGHT_MAX);
} }
if (msg.attrs[0] == 2) if (msg.attrs[0] == 2)
{ {

View File

@ -5,7 +5,7 @@
#include "time_func.h" #include "time_func.h"
#include "msg_queue.h" #include "msg_queue.h"
PMSG_QUEUE MqAlloc(int nentries) PMSG_QUEUE Mq_alloc(int nentries)
{ {
int sz = sizeof(MSG_QUEUE) + (nentries * sizeof(MSG)); int sz = sizeof(MSG_QUEUE) + (nentries * sizeof(MSG));
PMSG_QUEUE rc; PMSG_QUEUE rc;
@ -22,7 +22,7 @@ PMSG_QUEUE MqAlloc(int nentries)
return rc; return rc;
} }
void MqDestroy(PMSG_QUEUE queue) void Mq_destroy(PMSG_QUEUE queue)
{ {
pthread_mutex_destroy(&(queue->mutex)); pthread_mutex_destroy(&(queue->mutex));
free(queue); free(queue);
@ -46,7 +46,7 @@ static void post_internal(PMSG_QUEUE queue, PMSG msg)
pthread_mutex_unlock(&(queue->mutex)); pthread_mutex_unlock(&(queue->mutex));
} }
void MqPost(PMSG_QUEUE queue, uintptr_t target, unsigned message, const uintptr_t *attrs, int nattrs) void Mq_post(PMSG_QUEUE queue, uintptr_t target, unsigned message, const uintptr_t *attrs, int nattrs)
{ {
MSG tmpmsg; MSG tmpmsg;
@ -57,11 +57,11 @@ void MqPost(PMSG_QUEUE queue, uintptr_t target, unsigned message, const uintptr_
nattrs = MSG_ATTRCOUNT; nattrs = MSG_ATTRCOUNT;
if (nattrs > 0) if (nattrs > 0)
memcpy(&(tmpmsg.attrs), attrs, sizeof(uintptr_t) * nattrs); memcpy(&(tmpmsg.attrs), attrs, sizeof(uintptr_t) * nattrs);
tmpmsg.timestamp = TimeSinceStart(); tmpmsg.timestamp = Time_since_start();
post_internal(queue, &tmpmsg); post_internal(queue, &tmpmsg);
} }
void MqPost1(PMSG_QUEUE queue, uintptr_t target, unsigned message, uintptr_t attr1) void Mq_post1(PMSG_QUEUE queue, uintptr_t target, unsigned message, uintptr_t attr1)
{ {
MSG tmpmsg; MSG tmpmsg;
@ -69,11 +69,11 @@ void MqPost1(PMSG_QUEUE queue, uintptr_t target, unsigned message, uintptr_t att
tmpmsg.target = target; tmpmsg.target = target;
tmpmsg.message = message; tmpmsg.message = message;
tmpmsg.attrs[0] = attr1; tmpmsg.attrs[0] = attr1;
tmpmsg.timestamp = TimeSinceStart(); tmpmsg.timestamp = Time_since_start();
post_internal(queue, &tmpmsg); post_internal(queue, &tmpmsg);
} }
int MqPeek(PMSG_QUEUE queue, PMSG msg, unsigned flags) int Mq_peek(PMSG_QUEUE queue, PMSG msg, unsigned flags)
{ {
int rc = 0; int rc = 0;
PMSG nexthead; PMSG nexthead;

View File

@ -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 MqAlloc(int nentries); extern PMSG_QUEUE Mq_alloc(int nentries);
extern void MqDestroy(PMSG_QUEUE queue); extern void Mq_destroy(PMSG_QUEUE queue);
extern void MqPost(PMSG_QUEUE queue, uintptr_t target, unsigned message, const uintptr_t *attrs, int nattrs); extern void Mq_post(PMSG_QUEUE queue, uintptr_t target, unsigned message, const uintptr_t *attrs, int nattrs);
extern void MqPost1(PMSG_QUEUE queue, uintptr_t target, unsigned message, uintptr_t attr1); extern void Mq_post1(PMSG_QUEUE queue, uintptr_t target, unsigned message, uintptr_t attr1);
extern int MqPeek(PMSG_QUEUE queue, PMSG msg, unsigned flags); extern int Mq_peek(PMSG_QUEUE queue, PMSG msg, unsigned flags);
#endif /* __MSG_QUEUE_H_INCLUDED */ #endif /* __MSG_QUEUE_H_INCLUDED */

View File

@ -24,12 +24,12 @@ static void *input_thread(void *arg)
{ {
up = last_bstate & ~st; up = last_bstate & ~st;
down = st & ~last_bstate; down = st & ~last_bstate;
for (attr = 1, mask = 1; attr <= 4; attr++, mask <<= 1) for (attr = 1, mask = GRB_STATE_BUTTON1; attr <= GPIO_BUTTON_COUNT; attr++, mask <<= 1)
{ {
if (up & mask) if (up & mask)
MqPost1(Sys_Queue, 0, WM_HWBUTTONUP, attr); Mq_post1(Sys_Queue, 0, WM_HWBUTTONUP, attr);
else if (down & mask) else if (down & mask)
MqPost1(Sys_Queue, 0, WM_HWBUTTONDOWN, attr); Mq_post1(Sys_Queue, 0, WM_HWBUTTONDOWN, attr);
} }
last_bstate = st; last_bstate = st;
} }
@ -39,11 +39,11 @@ static void *input_thread(void *arg)
return NULL; return NULL;
} }
int SysEnableInput(void) int Sys_enable_input(void)
{ {
int rc; int rc;
Sys_Queue = MqAlloc(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.");
@ -56,10 +56,10 @@ int SysEnableInput(void)
return rc; return rc;
} }
void SysDisableInput(void) void Sys_disable_input(void)
{ {
running = 0; running = 0;
pthread_join(ithread, NULL); pthread_join(ithread, NULL);
MqDestroy(Sys_Queue); Mq_destroy(Sys_Queue);
Sys_Queue = NULL; Sys_Queue = NULL;
} }

View File

@ -5,7 +5,7 @@
extern PMSG_QUEUE Sys_Queue; extern PMSG_QUEUE Sys_Queue;
extern int SysEnableInput(void); extern int Sys_enable_input(void);
extern void SysDisableInput(void); extern void Sys_disable_input(void);
#endif /* __SYSINPUT_H_INCLUDED */ #endif /* __SYSINPUT_H_INCLUDED */

View File

@ -4,7 +4,7 @@
static TIMESTAMP start_timestamp = 0; static TIMESTAMP start_timestamp = 0;
TIMESTAMP TimeSinceEpoch(void) TIMESTAMP Time_since_epoch(void)
{ {
TIMESTAMP rc; TIMESTAMP rc;
struct timeval tv; struct timeval tv;
@ -14,12 +14,12 @@ TIMESTAMP TimeSinceEpoch(void)
return rc; return rc;
} }
TIMESTAMP TimeSinceStart(void) TIMESTAMP Time_since_start(void)
{ {
return TimeSinceEpoch() - start_timestamp; return TimeSinceEpoch() - start_timestamp;
} }
void TimeInit(void) void Time_init(void)
{ {
start_timestamp = TimeSinceEpoch(); start_timestamp = TimeSinceEpoch();
} }

View File

@ -3,8 +3,8 @@
#include "wintype.h" #include "wintype.h"
extern TIMESTAMP TimeSinceEpoch(void); extern TIMESTAMP Time_since_epoch(void);
extern TIMESTAMP TimeSinceStart(void); extern TIMESTAMP Time_since_start(void);
extern void TimeInit(void); extern void Time_init(void);
#endif /* __TIMEFUNC_H_INCLUDED */ #endif /* __TIMEFUNC_H_INCLUDED */