Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* A component to convert 7i73 bytecodes to bit pins */
- #include "rtapi.h"
- #include "rtapi_app.h"
- #include <linux/input.h>
- #include <linux/module.h>
- #include <linux/init.h>
- #include "hal.h"
- #define MAX_CHAN 8
- /* module information */
- MODULE_AUTHOR("Andy Pugh");
- MODULE_DESCRIPTION("Hal-to-text component for Mesa 7i73 and similar");
- MODULE_LICENSE("GPL");
- typedef struct {
- struct {
- hal_bit_t **key;
- hal_u32_t *keycode;
- } hal;
- struct {
- int *code;
- } param;
- hal_u32_t cols;
- hal_u32_t rows;
- char name[HAL_NAME_LEN + 1];
- struct input_dev *key_dev;
- }kb_inst_t;
- typedef struct {
- kb_inst_t *insts;
- int num_insts;
- }kb_t;
- static int comp_id;
- static kb_t *kb;
- char *config[MAX_CHAN];
- RTAPI_MP_ARRAY_STRING(config, MAX_CHAN, "screen formatting scancodes")
- void loop(void *arg, long period){
- int c, r;
- kb_inst_t *inst = arg;
- if (*inst->hal.keycode == 0) return;
- r = (*inst->hal.keycode & 0x38) >> 3;
- c = (*inst->hal.keycode & 0x07);
- if (inst->hal.key[r * inst->cols + c] == NULL){
- return;
- }
- switch (*inst->hal.keycode & 0xC0){
- case 0x40: //Keydown
- *inst->hal.key[r * inst->cols + c] = 1;
- if (inst->param.code[r * inst->cols + c]){
- input_report_key(inst->key_dev,
- inst->param.code[r * inst->cols + c] ,
- 1);
- input_sync(inst->key_dev);
- }
- break;
- default: //KeyUp
- *inst->hal.key[r * inst->cols + c] = 0;
- if (inst->param.code[r * inst->cols + c]){
- input_report_key(inst->key_dev,
- inst->param.code[r * inst->cols + c],
- 0);
- input_sync(inst->key_dev);
- }
- break;
- }
- }
- int rtapi_app_main(void){
- int i, j;
- int retval;
- comp_id = hal_init("matrix_kb");
- if (comp_id < 0) {
- rtapi_print_msg(RTAPI_MSG_ERR, "matrix_kb: ERROR: hal_init() failed\n");
- return -1;
- }
- // allocate shared memory for data
- kb = hal_malloc(sizeof(kb_t));
- if (kb == 0) {
- rtapi_print_msg(RTAPI_MSG_ERR,
- "matrix_kb component: Out of Memory\n");
- hal_exit(comp_id);
- return -1;
- }
- // Count the instances. Very unlikely to be more than one, but...
- for (kb->num_insts = 0; config[kb->num_insts];kb->num_insts++){}
- kb->insts = hal_malloc(kb->num_insts * sizeof(kb_inst_t));
- for (i = 0; i < kb->num_insts; i++){
- int a = 0;
- int c, r;
- kb_inst_t *inst = &kb->insts[i];
- inst->rows = 0;
- inst->cols = 0;
- for(j = 0; config[i][j] !=0; j++){
- int n = config[i][j];
- if (n == 'x' || n == 'X'){
- inst->rows = a;
- a = 0;
- }
- else if (n >= '0' && n <= '9'){
- a = (a * 10) + (n - '0');
- }
- }
- inst->cols = a;
- if (inst->cols == 0 || inst->cols == 0){
- rtapi_print_msg(RTAPI_MSG_ERR,
- "matrix_kb: Invalid config format. should be NxN\n");
- hal_exit(comp_id);
- return -1;
- }
- //inst->hal.keycode = hal_malloc(sizeof(hal_u32_t));
- inst->hal.key = (hal_bit_t **)hal_malloc(inst->rows * inst->cols * sizeof(hal_bit_t*));
- inst->param.code = hal_malloc(inst->rows * inst->cols * sizeof(inst->param.code));
- for (c = 0; c < inst->cols; c++){
- for (r = 0; r < inst->rows; r++){
- retval = hal_pin_bit_newf(HAL_OUT,
- &(inst->hal.key[r * inst->cols + c]), comp_id,
- "matrix_kb.%i.key.%i.%i", i, c, r);
- if (retval != 0) {
- rtapi_print_msg(RTAPI_MSG_ERR,
- "matrix_kb: Failed to create output pin\n");
- hal_exit(comp_id);
- return -1;
- }
- retval = hal_param_u32_newf(HAL_RW,
- &inst->param.code[r * inst->cols + c], comp_id,
- "matrix_kb.%i.code.%i.%i", i, c, r);
- if (retval != 0) {
- rtapi_print_msg(RTAPI_MSG_ERR,
- "matrix_kb: Failed to create output pin\n");
- hal_exit(comp_id);
- return -1;
- }
- }
- }
- retval = hal_pin_u32_newf(HAL_IN,
- &(inst->hal.keycode), comp_id,
- "matrix_kb.%i.keycode", i);
- if (retval != 0) {
- rtapi_print_msg(RTAPI_MSG_ERR,
- "matrix_kb: Failed to create input pin\n");
- hal_exit(comp_id);
- return -1;
- }
- rtapi_snprintf(inst->name, sizeof(inst->name), "matrix_kb.%i", i);
- retval = hal_export_funct(inst->name, loop, inst, 1, 0, comp_id); //needs fp?
- if (retval < 0) {
- rtapi_print_msg(RTAPI_MSG_ERR, "matrix_kb: ERROR: function export failed\n");
- return -1;
- }
- inst->key_dev = input_allocate_device();
- if (!inst->key_dev) {
- rtapi_print_msg(RTAPI_MSG_ERR,"matrix_kb: Not enough memory\n");
- return -1;
- }
- inst->key_dev->name = inst->name;
- set_bit(EV_KEY, inst->key_dev->evbit);
- for(j = 0; j < 226 ; j++){
- set_bit(j, inst->key_dev->keybit);
- }
- retval = input_register_device(inst->key_dev);
- if (retval) {
- printk(KERN_ERR "button.c: Failed to register device\n");
- return -1;
- }
- }
- hal_ready(comp_id);
- return 0;
- }
- void rtapi_app_exit(void)
- {
- int i;
- hal_exit(comp_id);
- for (i = 0; i < kb->num_insts ; input_unregister_device(kb->insts[i].key_dev), i++);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement