code cleanup
This commit is contained in:
		
							parent
							
								
									05e1028211
								
							
						
					
					
						commit
						2f6118d436
					
				
							
								
								
									
										13
									
								
								src/gpio.c
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								src/gpio.c
									
									
									
									
									
								
							@ -1,9 +1,4 @@
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <sys/mman.h>
 | 
			
		||||
#include <bcm2835.h>
 | 
			
		||||
#include "log.h"
 | 
			
		||||
#include "gpio.h"
 | 
			
		||||
@ -39,8 +34,8 @@ int Gpio_setup(void)
 | 
			
		||||
  bcm2835_gpio_fsel(GLINE_BACKLIGHT, BCM2835_GPIO_FSEL_ALT5);
 | 
			
		||||
  bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_2);
 | 
			
		||||
  bcm2835_pwm_set_mode(PWM_BACKLIGHT, 1, 1);
 | 
			
		||||
  bcm2835_pwm_set_range(PWM_BACKLIGHT, BACKLIGHT_MAX + 1);
 | 
			
		||||
  bcm2835_pwm_set_data(PWM_BACKLIGHT, BACKLIGHT_MAX);
 | 
			
		||||
  bcm2835_pwm_set_range(PWM_BACKLIGHT, GSB_BACKLIGHT_MAX + 1);
 | 
			
		||||
  bcm2835_pwm_set_data(PWM_BACKLIGHT, GSB_BACKLIGHT_MAX);
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
@ -83,7 +78,7 @@ int Gpio_read_buttons(void)
 | 
			
		||||
 | 
			
		||||
void Gpio_set_backlight(unsigned level)
 | 
			
		||||
{
 | 
			
		||||
  if (level > BACKLIGHT_MAX)
 | 
			
		||||
    level = BACKLIGHT_MAX;
 | 
			
		||||
  if (level > GSB_BACKLIGHT_MAX)
 | 
			
		||||
    level = GSB_BACKLIGHT_MAX;
 | 
			
		||||
  bcm2835_pwm_set_data(PWM_BACKLIGHT, level);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										12
									
								
								src/gpio.h
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								src/gpio.h
									
									
									
									
									
								
							@ -1,12 +1,14 @@
 | 
			
		||||
#ifndef __GPIO_H_INCLUDED
 | 
			
		||||
#define __GPIO_H_INCLUDED
 | 
			
		||||
 | 
			
		||||
#define STATE_BUTTON1  (1 << 0)
 | 
			
		||||
#define STATE_BUTTON2  (1 << 1)
 | 
			
		||||
#define STATE_BUTTON3  (1 << 2)
 | 
			
		||||
#define STATE_BUTTON4  (1 << 3)
 | 
			
		||||
#define GPIO_BUTTON_COUNT  4
 | 
			
		||||
 | 
			
		||||
#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 void Gpio_cleanup(void);
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										10
									
								
								src/main.c
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/main.c
									
									
									
									
									
								
							@ -9,19 +9,19 @@ int main(int argc, char *argv[])
 | 
			
		||||
  int running = 1;
 | 
			
		||||
  MSG msg;
 | 
			
		||||
  
 | 
			
		||||
  TimeInit();
 | 
			
		||||
  Time_init();
 | 
			
		||||
  if (Gpio_setup() != 0)
 | 
			
		||||
    return EXIT_FAILURE;
 | 
			
		||||
  atexit(Gpio_cleanup);
 | 
			
		||||
  if (SysEnableInput() != 0)
 | 
			
		||||
  if (Sys_enable_input() != 0)
 | 
			
		||||
    return EXIT_FAILURE;
 | 
			
		||||
  atexit(SysDisableInput);
 | 
			
		||||
  atexit(Sys_disable_input);
 | 
			
		||||
 | 
			
		||||
  Log(LINFO, "System ready.");
 | 
			
		||||
 | 
			
		||||
  while (running)
 | 
			
		||||
  {
 | 
			
		||||
    if (MqPeek(Sys_Queue, &msg, PEEK_REMOVE))
 | 
			
		||||
    if (Mq_peek(Sys_Queue, &msg, PEEK_REMOVE))
 | 
			
		||||
    {
 | 
			
		||||
      switch (msg.message)
 | 
			
		||||
      {
 | 
			
		||||
@ -34,7 +34,7 @@ int main(int argc, char *argv[])
 | 
			
		||||
	  if (msg.attrs[0] == 1)
 | 
			
		||||
	  {
 | 
			
		||||
	    Log(LINFO, "Backlight ON.");
 | 
			
		||||
	    Gpio_set_backlight(BACKLIGHT_MAX);
 | 
			
		||||
	    Gpio_set_backlight(GRB_BACKLIGHT_MAX);
 | 
			
		||||
	  }
 | 
			
		||||
	  if (msg.attrs[0] == 2)
 | 
			
		||||
	  {
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,7 @@
 | 
			
		||||
#include "time_func.h"
 | 
			
		||||
#include "msg_queue.h"
 | 
			
		||||
 | 
			
		||||
PMSG_QUEUE MqAlloc(int nentries)
 | 
			
		||||
PMSG_QUEUE Mq_alloc(int nentries)
 | 
			
		||||
{
 | 
			
		||||
  int sz = sizeof(MSG_QUEUE) + (nentries * sizeof(MSG));
 | 
			
		||||
  PMSG_QUEUE rc;
 | 
			
		||||
@ -22,7 +22,7 @@ PMSG_QUEUE MqAlloc(int nentries)
 | 
			
		||||
  return rc;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MqDestroy(PMSG_QUEUE queue)
 | 
			
		||||
void Mq_destroy(PMSG_QUEUE queue)
 | 
			
		||||
{
 | 
			
		||||
  pthread_mutex_destroy(&(queue->mutex));
 | 
			
		||||
  free(queue);
 | 
			
		||||
@ -46,7 +46,7 @@ static void post_internal(PMSG_QUEUE queue, PMSG msg)
 | 
			
		||||
  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;
 | 
			
		||||
 | 
			
		||||
@ -57,11 +57,11 @@ void MqPost(PMSG_QUEUE queue, uintptr_t target, unsigned message, const uintptr_
 | 
			
		||||
    nattrs = MSG_ATTRCOUNT;
 | 
			
		||||
  if (nattrs > 0)
 | 
			
		||||
    memcpy(&(tmpmsg.attrs), attrs, sizeof(uintptr_t) * nattrs);
 | 
			
		||||
  tmpmsg.timestamp = TimeSinceStart();
 | 
			
		||||
  tmpmsg.timestamp = Time_since_start();
 | 
			
		||||
  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;
 | 
			
		||||
  
 | 
			
		||||
@ -69,11 +69,11 @@ void MqPost1(PMSG_QUEUE queue, uintptr_t target, unsigned message, uintptr_t att
 | 
			
		||||
  tmpmsg.target = target;
 | 
			
		||||
  tmpmsg.message = message;
 | 
			
		||||
  tmpmsg.attrs[0] = attr1;
 | 
			
		||||
  tmpmsg.timestamp = TimeSinceStart();
 | 
			
		||||
  tmpmsg.timestamp = Time_since_start();
 | 
			
		||||
  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;
 | 
			
		||||
  PMSG nexthead;
 | 
			
		||||
 | 
			
		||||
@ -18,10 +18,10 @@ typedef struct tagMSG_QUEUE {
 | 
			
		||||
#define PEEK_REMOVE   0x0001
 | 
			
		||||
#define PEEK_NOREMOVE 0x0000
 | 
			
		||||
 | 
			
		||||
extern PMSG_QUEUE MqAlloc(int nentries);
 | 
			
		||||
extern void MqDestroy(PMSG_QUEUE queue);
 | 
			
		||||
extern void MqPost(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 int MqPeek(PMSG_QUEUE queue, PMSG msg, unsigned flags);
 | 
			
		||||
extern PMSG_QUEUE Mq_alloc(int nentries);
 | 
			
		||||
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_post1(PMSG_QUEUE queue, uintptr_t target, unsigned message, uintptr_t attr1);
 | 
			
		||||
extern int Mq_peek(PMSG_QUEUE queue, PMSG msg, unsigned flags);
 | 
			
		||||
 | 
			
		||||
#endif /* __MSG_QUEUE_H_INCLUDED */
 | 
			
		||||
 | 
			
		||||
@ -24,12 +24,12 @@ static void *input_thread(void *arg)
 | 
			
		||||
    {
 | 
			
		||||
      up = last_bstate & ~st;
 | 
			
		||||
      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)
 | 
			
		||||
	  MqPost1(Sys_Queue, 0, WM_HWBUTTONUP, attr);
 | 
			
		||||
	  Mq_post1(Sys_Queue, 0, WM_HWBUTTONUP, attr);
 | 
			
		||||
	else if (down & mask)
 | 
			
		||||
	  MqPost1(Sys_Queue, 0, WM_HWBUTTONDOWN, attr);
 | 
			
		||||
	  Mq_post1(Sys_Queue, 0, WM_HWBUTTONDOWN, attr);
 | 
			
		||||
      }
 | 
			
		||||
      last_bstate = st;
 | 
			
		||||
    }
 | 
			
		||||
@ -39,11 +39,11 @@ static void *input_thread(void *arg)
 | 
			
		||||
  return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int SysEnableInput(void)
 | 
			
		||||
int Sys_enable_input(void)
 | 
			
		||||
{
 | 
			
		||||
  int rc;
 | 
			
		||||
  
 | 
			
		||||
  Sys_Queue = MqAlloc(64);
 | 
			
		||||
  Sys_Queue = Mq_alloc(64);
 | 
			
		||||
  if (!Sys_Queue)
 | 
			
		||||
  {
 | 
			
		||||
    Log(LFATAL, "Unable to allocate system message queue.");
 | 
			
		||||
@ -56,10 +56,10 @@ int SysEnableInput(void)
 | 
			
		||||
  return rc;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SysDisableInput(void)
 | 
			
		||||
void Sys_disable_input(void)
 | 
			
		||||
{
 | 
			
		||||
  running = 0;
 | 
			
		||||
  pthread_join(ithread, NULL);
 | 
			
		||||
  MqDestroy(Sys_Queue);
 | 
			
		||||
  Mq_destroy(Sys_Queue);
 | 
			
		||||
  Sys_Queue = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,7 @@
 | 
			
		||||
 | 
			
		||||
extern PMSG_QUEUE Sys_Queue;
 | 
			
		||||
 | 
			
		||||
extern int SysEnableInput(void);
 | 
			
		||||
extern void SysDisableInput(void);
 | 
			
		||||
extern int Sys_enable_input(void);
 | 
			
		||||
extern void Sys_disable_input(void);
 | 
			
		||||
 | 
			
		||||
#endif /* __SYSINPUT_H_INCLUDED */
 | 
			
		||||
 | 
			
		||||
@ -4,7 +4,7 @@
 | 
			
		||||
 | 
			
		||||
static TIMESTAMP start_timestamp = 0;
 | 
			
		||||
 | 
			
		||||
TIMESTAMP TimeSinceEpoch(void)
 | 
			
		||||
TIMESTAMP Time_since_epoch(void)
 | 
			
		||||
{
 | 
			
		||||
  TIMESTAMP rc;
 | 
			
		||||
  struct timeval tv;
 | 
			
		||||
@ -14,12 +14,12 @@ TIMESTAMP TimeSinceEpoch(void)
 | 
			
		||||
  return rc;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TIMESTAMP TimeSinceStart(void)
 | 
			
		||||
TIMESTAMP Time_since_start(void)
 | 
			
		||||
{
 | 
			
		||||
  return TimeSinceEpoch() - start_timestamp;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void TimeInit(void)
 | 
			
		||||
void Time_init(void)
 | 
			
		||||
{
 | 
			
		||||
  start_timestamp = TimeSinceEpoch();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -3,8 +3,8 @@
 | 
			
		||||
 | 
			
		||||
#include "wintype.h"
 | 
			
		||||
 | 
			
		||||
extern TIMESTAMP TimeSinceEpoch(void);
 | 
			
		||||
extern TIMESTAMP TimeSinceStart(void);
 | 
			
		||||
extern void TimeInit(void);
 | 
			
		||||
extern TIMESTAMP Time_since_epoch(void);
 | 
			
		||||
extern TIMESTAMP Time_since_start(void);
 | 
			
		||||
extern void Time_init(void);
 | 
			
		||||
 | 
			
		||||
#endif /* __TIMEFUNC_H_INCLUDED */
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user