Skip to content

Instantly share code, notes, and snippets.

@rgerganov
Created July 13, 2018 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgerganov/4117829b7e6042b13d02b1a419bab362 to your computer and use it in GitHub Desktop.
Save rgerganov/4117829b7e6042b13d02b1a419bab362 to your computer and use it in GitHub Desktop.
RPi + char lcd + uinput
#include <stdio.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <wiringPi.h>
#include <lcd.h>
//#define LCD_RS 6 //Register select pin
//#define LCD_E 5 //Enable Pin
#define LCD_RS 25 //Register select pin
#define LCD_E 24 //Enable Pin
#define LCD_D4 4 //Data pin 4
#define LCD_D5 0 //Data pin 5
#define LCD_D6 1 //Data pin 6
#define LCD_D7 3 //Data pin 7
void show_lcd(int lcd, char *freq_str, char *step_str)
{
lcdPosition(lcd, 0, 0);
lcdPuts(lcd, freq_str);
lcdPosition(lcd, 0, 1);
lcdPuts(lcd, step_str);
}
void show_term(char *str)
{
printf("%s\r", str);
fflush(stdout);
}
void freq_to_str(int freq, char *str)
{
int mhz = freq / 1000000;
int hz = freq % 1000000;
sprintf(str, "%3d.%06d MHz", mhz, hz);
}
void step_to_str(int step, char *str)
{
char tmp[32];
if (step < 1000) {
sprintf(tmp, "step: %dHz", step);
sprintf(str, "%-16s", tmp);
} else if (step < 1000000) {
sprintf(tmp, "step: %dkHz", step / 1000);
sprintf(str, "%-16s", tmp);
} else {
sprintf(tmp, "step: %dMHz", step / 1000000);
sprintf(str, "%-16s", tmp);
}
}
int main(int argc, char *argv[])
{
int fd = open("/dev/input/event0", O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Cannot open input\n");
return 1;
}
wiringPiSetup();
int lcd = lcdInit(2, 16, 4, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0, 0, 0, 0);
int freq = 144000000;
char freq_str[32];
int step = 1;
char step_str[32];
while (1) {
freq_to_str(freq, freq_str);
step_to_str(step, step_str);
show_term(freq_str);
show_lcd(lcd, freq_str, step_str);
struct input_event ev;
int ret = read(fd, &ev, sizeof(ev));
if (ret != sizeof(ev)) {
fprintf(stderr, "unexpected read count\n");
continue;
}
if (ev.code == 8 && ev.type == 2) {
if (ev.value == 1) {
freq += step;
}
if (ev.value == -1) {
freq -= step;
}
} else if (ev.type == 1 && ev.value == 1) {
if (ev.code == 0x110) {
if (step * 10 <= 1000000) {
step *= 10;
}
}
if (ev.code == 0x111) {
if (step / 10 > 0) {
step /= 10;
}
}
}
//printf("type:%x value:%x code:%x\n", ev.type, ev.value, ev.code);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment