keypad driver progress

master
Clyne 7 years ago
parent 603d04992c
commit 2b034ffa10

@ -23,34 +23,18 @@
#include <stdint.h> #include <stdint.h>
#define K0 (uint16_t)(1 << 0)
#define K1 (uint16_t)(1 << 1)
#define K2 (uint16_t)(1 << 2)
#define K3 (uint16_t)(1 << 3)
#define K4 (uint16_t)(1 << 4)
#define K5 (uint16_t)(1 << 5)
#define K6 (uint16_t)(1 << 6)
#define K7 (uint16_t)(1 << 7)
#define K8 (uint16_t)(1 << 8)
#define K9 (uint16_t)(1 << 9)
#define KS (uint16_t)(1 << 10)
#define KP (uint16_t)(1 << 11)
/** /**
* Initializes GPIO for the keypad. Must be called before any keypad reading. * Initializes GPIO for the keypad.
* Starts a task to poll the buttons. Must be called before any keypad reading.
*/ */
void keypad_init(void); void keypad_init(void);
/** /**
* Reads the state of the keypad and returns it. * Reads the last pressed key on the keypad.
* @return the keypad's state * This driver keeps an 8 key buffer for key presses.
* @return the pressed key (as the character it represents), zero if no presses
*/ */
uint16_t keypad_get(void); int keypad_get(void);
/**
* Tests if the given key is currently pressed, returning non-zero if it is.
* @return non-zero if pressed
*/
uint8_t keypad_isdown(uint16_t);
#endif // KEYPAD_H_ #endif // KEYPAD_H_

@ -0,0 +1,67 @@
# graph area
plotx = 50
ploty = 50
plotw = 380
ploth = 220
cx = plotx + plotw / 2
cy = ploty + ploth / 2
# graph range
xmin = 0 - 10
xmax = 10
ymin = 0 - 10
ymax = 10
xinc = plotw / (xmax - xmin)
yinc = ploth / (ymax - ymin)
mlines = color(3, 3, 3)
func(makegrid) {
rect(plotx, ploty, plotw, ploth, 0)
x = plotx
while (x <= plotx + plotw) {
line(x, ploty, x, ploty + ploth, mlines)
x = x + xinc
}
y = ploty
while (y <= ploty + ploth) {
line(plotx, y, plotx + plotw, y, mlines)
y = y + yinc
}
line(cx, ploty, cx, ploty + ploth, 32767)
line(plotx, cy, plotx + plotw, cy, 32767)
}
#
# BIG LOOP - ask for equ, graph it
#
makegrid
clearcmd = "clear"
while (1) {
rect(0, 0, 480, 40, 0)
print("f(x) = ")
Fx = gets()
if (Fx == clearcmd) {
makegrid
} else {
# do function
x = xmin
while (x < xmax) {
y = solve(Fx)
y = 0 - y
if ((y >= ymin) & (y <= ymax)) {
pixel(cx + x * xinc, cy + y * yinc, 511)
}
x = x + 1 / xinc
}
}
ppos(0, 0)
}

@ -1,67 +1,8 @@
# graph area input = 0
plotx = 50
ploty = 50
plotw = 380
ploth = 220
cx = plotx + plotw / 2
cy = ploty + ploth / 2
# graph range
xmin = 0 - 10
xmax = 10
ymin = 0 - 10
ymax = 10
xinc = plotw / (xmax - xmin)
yinc = ploth / (ymax - ymin)
mlines = color(3, 3, 3)
func(makegrid) {
rect(plotx, ploty, plotw, ploth, 0)
x = plotx
while (x <= plotx + plotw) {
line(x, ploty, x, ploty + ploth, mlines)
x = x + xinc
}
y = ploty
while (y <= ploty + ploth) {
line(plotx, y, plotx + plotw, y, mlines)
y = y + yinc
}
line(cx, ploty, cx, ploty + ploth, 32767)
line(plotx, cy, plotx + plotw, cy, 32767)
}
#
# BIG LOOP - ask for equ, graph it
#
makegrid
clearcmd = "clear"
while (1) { while (1) {
rect(0, 0, 480, 40, 0) while (input == 0) {
print("f(x) = ") input = getkey
Fx = gets()
if (Fx == clearcmd) {
makegrid
} else {
# do function
x = xmin
while (x < xmax) {
y = solve(Fx)
y = 0 - y
if ((y >= ymin) & (y <= ymax)) {
pixel(cx + x * xinc, cy + y * yinc, 511)
}
x = x + 1 / xinc
}
} }
print(input)
ppos(0, 0) input = 0
} }

Binary file not shown.

@ -18,66 +18,105 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <clock.h>
#include <keypad.h> #include <keypad.h>
#include <gpio.h> #include <gpio.h>
#include <task.h>
#define PIN_0 GPIO_PORT(B, 2) #define ROW_0 GPIO_PORT(B, 15)
#define PIN_1 GPIO_PORT(B, 1) #define ROW_1 GPIO_PORT(B, 14)
#define PIN_2 GPIO_PORT(A, 11) #define ROW_2 GPIO_PORT(B, 13)
#define PIN_3 GPIO_PORT(C, 8) #define ROW_3 GPIO_PORT(C, 4)
#define PIN_4 GPIO_PORT(B, 15) #define COL_0 GPIO_PORT(B, 1)
#define PIN_5 GPIO_PORT(B, 12) #define COL_1 GPIO_PORT(B, 2)
#define PIN_6 GPIO_PORT(C, 6) #define COL_2 GPIO_PORT(B, 11)
#define PIN_7 GPIO_PORT(B, 14) #define COL_3 GPIO_PORT(B, 12)
#define PIN_8 GPIO_PORT(B, 11) #define COL_4 GPIO_PORT(A, 11)
#define PIN_9 GPIO_PORT(C, 5)
#define PIN_S GPIO_PORT(B, 13) #define ROWS 4
#define PIN_P GPIO_PORT(A, 12) #define COLS 5
typedef struct { typedef struct {
GPIO_TypeDef *port; GPIO_TypeDef *port;
uint16_t pin; uint16_t pin;
uint16_t keycode; } port_t;
} key_t;
static const port_t keypad_rows[ROWS] = {
{ ROW_0 }, { ROW_1 }, { ROW_2 }, { ROW_3 }
};
static const port_t keypad_cols[COLS] = {
{ COL_0 }, { COL_1 }, { COL_2 }, { COL_3 }, { COL_4 }
};
static const key_t keypad_map[12] = { static const int keypad_map[ROWS][COLS] = {
{ PIN_0, K0 }, { '7', '8', '9', 'x', '/' },
{ PIN_1, K1 }, { '4', '5', '6', 'y', '*' },
{ PIN_2, K2 }, { '3', '2', '1', 'z', '-' },
{ PIN_3, K3 }, { '.', '0', '\b', '\n', '+' }
{ PIN_4, K4 },
{ PIN_5, K5 },
{ PIN_6, K6 },
{ PIN_7, K7 },
{ PIN_8, K8 },
{ PIN_9, K9 },
{ PIN_S, KS },
{ PIN_P, KP }
}; };
#define BUFFER_SIZE 8
static char keypad_buffer = 'A';//[BUFFER_SIZE];
//static int keypad_buffer_pos = -1;
void keypad_task(void)
{
unsigned int col = 0;
while (1) {
gpio_dout(keypad_cols[col].port, keypad_cols[col].pin, 1);
for (unsigned int row = 0; row < ROWS; row++) {
if (gpio_din(keypad_rows[row].port, keypad_rows[row].pin)) {
//if (keypad_buffer_pos < BUFFER_SIZE)
keypad_buffer/*[++keypad_buffer_pos]*/ = keypad_map[row][col];
while (gpio_din(keypad_rows[row].port, keypad_rows[row].pin))
delay(1);
break;
}
}
gpio_dout(keypad_cols[col].port, keypad_cols[col].pin, 0);
col++;
if (col == COLS)
col = 0;
delay(10);
}
}
void keypad_init(void) void keypad_init(void)
{ {
for (uint8_t i = 0; i < 12; i++) { for (uint8_t i = 0; i < ROWS; i++) {
GPIO_TypeDef *p = keypad_map[i].port; GPIO_TypeDef *p = keypad_rows[i].port;
uint16_t pin = keypad_map[i].pin; uint16_t pin = keypad_rows[i].pin;
gpio_mode(p, pin, OUTPUT); gpio_mode(p, pin, OUTPUT);
gpio_speed(p, pin, VERYHIGH);
gpio_dout(p, pin, 0); gpio_dout(p, pin, 0);
gpio_mode(p, pin, INPUT); gpio_mode(p, pin, INPUT);
//gpio_pupd(p, pin, PULLDOWN); gpio_pupd(p, pin, PULLDOWN);
}
} }
uint16_t keypad_get(void) for (uint8_t i = 0; i < COLS; i++) {
{ GPIO_TypeDef *p = keypad_cols[i].port;
uint16_t state = 0; uint16_t pin = keypad_cols[i].pin;
for (uint8_t i = 0; i < 12; i++) { gpio_mode(p, pin, OUTPUT);
if (gpio_din(keypad_map[i].port, keypad_map[i].pin)) gpio_speed(p, pin, VERYHIGH);
state |= keypad_map[i].keycode; gpio_dout(p, pin, 0);
} }
return state;
task_start(keypad_task, 1024);
} }
uint8_t keypad_isdown(uint16_t keycode) int keypad_get(void)
{ {
return (keypad_get() & keycode); //if (keypad_buffer_pos < 0)
// return 0;
//int key = keypad_buffer[0];
//for (int i = keypad_buffer_pos - 1; i > 0; i--)
// keypad_buffer[i - 1] = keypad_buffer[i];
//keypad_buffer_pos--;
//return key;
int ret = keypad_buffer;
keypad_buffer = 0;
return ret;
} }

@ -18,22 +18,22 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <stm32l476xx.h>
#include <clock.h> #include <clock.h>
#include <heap.h>
#include <task.h>
#include <gpio.h>
#include <lcd.h>
#include <display.h> #include <display.h>
#include <display_draw.h> #include <display_draw.h>
#include <flash.h>
#include <gpio.h>
#include <heap.h>
#include <initrd.h> #include <initrd.h>
#include <keypad.h>
#include <lcd.h>
#include <parser.h> #include <parser.h>
#include <random.h>
#include <script.h>
#include <serial.h> #include <serial.h>
#include <stm32l476xx.h>
#include <string.h> #include <string.h>
#include <script.h> #include <task.h>
#include <random.h>
#include <keypad.h>
#include <flash.h>
extern uint8_t __bss_end__; extern uint8_t __bss_end__;
extern char *itoa(int, char *, int); extern char *itoa(int, char *, int);
@ -54,12 +54,9 @@ int main(void)
clock_init(); clock_init();
heap_init(&__bss_end__); heap_init(&__bss_end__);
gpio_init(); gpio_init();
keypad_init();
serial_init(); serial_init();
random_init(); random_init();
keypad_init();
//extern void keypad_init(void);
//keypad_init();
gpio_mode(GPIOA, 5, OUTPUT); gpio_mode(GPIOA, 5, OUTPUT);
@ -77,6 +74,7 @@ void kmain(void)
dsp_init(); dsp_init();
dsp_rect(0, 0, LCD_WIDTH, LCD_HEIGHT, dsp_color(0, 0, 0)); dsp_rect(0, 0, LCD_WIDTH, LCD_HEIGHT, dsp_color(0, 0, 0));
dsp_cursoron(); dsp_cursoron();
keypad_init();
task_start(task_interpreter, 4096); task_start(task_interpreter, 4096);
/*char buf[2]; /*char buf[2];

@ -62,7 +62,6 @@ char *snprintf(char *buf, unsigned int max, const char *format, ...)
break; break;
case 'f': case 'f':
itoa((int)va_arg(args, double), nbuf, 10); itoa((int)va_arg(args, double), nbuf, 10);
continue;
break; break;
default: default:
buf[off++] = format[i]; buf[off++] = format[i];

Loading…
Cancel
Save