Advertisement
Guest User

Untitled

a guest
Mar 11th, 2013
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. /* A component to convert 7i73 bytecodes to bit pins */
  2. #include "rtapi.h"
  3. #include "rtapi_app.h"
  4. #include <linux/input.h>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include "hal.h"
  8.  
  9. #define MAX_CHAN 8
  10.  
  11. /* module information */
  12. MODULE_AUTHOR("Andy Pugh");
  13. MODULE_DESCRIPTION("Hal-to-text component for Mesa 7i73 and similar");
  14. MODULE_LICENSE("GPL");
  15.  
  16. typedef struct {
  17. struct {
  18. hal_bit_t **key;
  19. hal_u32_t *keycode;
  20. } hal;
  21. struct {
  22. int *code;
  23. } param;
  24. hal_u32_t cols;
  25. hal_u32_t rows;
  26. char name[HAL_NAME_LEN + 1];
  27. struct input_dev *key_dev;
  28. }kb_inst_t;
  29.  
  30. typedef struct {
  31. kb_inst_t *insts;
  32. int num_insts;
  33. }kb_t;
  34.  
  35. static int comp_id;
  36. static kb_t *kb;
  37.  
  38. char *config[MAX_CHAN];
  39. RTAPI_MP_ARRAY_STRING(config, MAX_CHAN, "screen formatting scancodes")
  40.  
  41.  
  42. void loop(void *arg, long period){
  43. int c, r;
  44. kb_inst_t *inst = arg;
  45. if (*inst->hal.keycode == 0) return;
  46. r = (*inst->hal.keycode & 0x38) >> 3;
  47. c = (*inst->hal.keycode & 0x07);
  48. if (inst->hal.key[r * inst->cols + c] == NULL){
  49. return;
  50. }
  51. switch (*inst->hal.keycode & 0xC0){
  52. case 0x40: //Keydown
  53. *inst->hal.key[r * inst->cols + c] = 1;
  54. if (inst->param.code[r * inst->cols + c]){
  55. input_report_key(inst->key_dev,
  56. inst->param.code[r * inst->cols + c] ,
  57. 1);
  58. input_sync(inst->key_dev);
  59. }
  60. break;
  61. default: //KeyUp
  62. *inst->hal.key[r * inst->cols + c] = 0;
  63. if (inst->param.code[r * inst->cols + c]){
  64. input_report_key(inst->key_dev,
  65. inst->param.code[r * inst->cols + c],
  66. 0);
  67. input_sync(inst->key_dev);
  68. }
  69. break;
  70. }
  71. }
  72.  
  73.  
  74. int rtapi_app_main(void){
  75. int i, j;
  76. int retval;
  77. comp_id = hal_init("matrix_kb");
  78. if (comp_id < 0) {
  79. rtapi_print_msg(RTAPI_MSG_ERR, "matrix_kb: ERROR: hal_init() failed\n");
  80. return -1;
  81. }
  82.  
  83. // allocate shared memory for data
  84. kb = hal_malloc(sizeof(kb_t));
  85. if (kb == 0) {
  86. rtapi_print_msg(RTAPI_MSG_ERR,
  87. "matrix_kb component: Out of Memory\n");
  88. hal_exit(comp_id);
  89. return -1;
  90. }
  91.  
  92. // Count the instances. Very unlikely to be more than one, but...
  93. for (kb->num_insts = 0; config[kb->num_insts];kb->num_insts++){}
  94. kb->insts = hal_malloc(kb->num_insts * sizeof(kb_inst_t));
  95.  
  96. for (i = 0; i < kb->num_insts; i++){
  97. int a = 0;
  98. int c, r;
  99. kb_inst_t *inst = &kb->insts[i];
  100. inst->rows = 0;
  101. inst->cols = 0;
  102.  
  103. for(j = 0; config[i][j] !=0; j++){
  104. int n = config[i][j];
  105. if (n == 'x' || n == 'X'){
  106. inst->rows = a;
  107. a = 0;
  108. }
  109. else if (n >= '0' && n <= '9'){
  110. a = (a * 10) + (n - '0');
  111. }
  112. }
  113. inst->cols = a;
  114.  
  115. if (inst->cols == 0 || inst->cols == 0){
  116. rtapi_print_msg(RTAPI_MSG_ERR,
  117. "matrix_kb: Invalid config format. should be NxN\n");
  118. hal_exit(comp_id);
  119. return -1;
  120. }
  121.  
  122. //inst->hal.keycode = hal_malloc(sizeof(hal_u32_t));
  123. inst->hal.key = (hal_bit_t **)hal_malloc(inst->rows * inst->cols * sizeof(hal_bit_t*));
  124. inst->param.code = hal_malloc(inst->rows * inst->cols * sizeof(inst->param.code));
  125.  
  126. for (c = 0; c < inst->cols; c++){
  127. for (r = 0; r < inst->rows; r++){
  128. retval = hal_pin_bit_newf(HAL_OUT,
  129. &(inst->hal.key[r * inst->cols + c]), comp_id,
  130. "matrix_kb.%i.key.%i.%i", i, c, r);
  131. if (retval != 0) {
  132. rtapi_print_msg(RTAPI_MSG_ERR,
  133. "matrix_kb: Failed to create output pin\n");
  134. hal_exit(comp_id);
  135. return -1;
  136. }
  137. retval = hal_param_u32_newf(HAL_RW,
  138. &inst->param.code[r * inst->cols + c], comp_id,
  139. "matrix_kb.%i.code.%i.%i", i, c, r);
  140. if (retval != 0) {
  141. rtapi_print_msg(RTAPI_MSG_ERR,
  142. "matrix_kb: Failed to create output pin\n");
  143. hal_exit(comp_id);
  144. return -1;
  145. }
  146. }
  147. }
  148.  
  149. retval = hal_pin_u32_newf(HAL_IN,
  150. &(inst->hal.keycode), comp_id,
  151. "matrix_kb.%i.keycode", i);
  152. if (retval != 0) {
  153. rtapi_print_msg(RTAPI_MSG_ERR,
  154. "matrix_kb: Failed to create input pin\n");
  155. hal_exit(comp_id);
  156. return -1;
  157. }
  158.  
  159. rtapi_snprintf(inst->name, sizeof(inst->name), "matrix_kb.%i", i);
  160. retval = hal_export_funct(inst->name, loop, inst, 1, 0, comp_id); //needs fp?
  161. if (retval < 0) {
  162. rtapi_print_msg(RTAPI_MSG_ERR, "matrix_kb: ERROR: function export failed\n");
  163. return -1;
  164. }
  165.  
  166. inst->key_dev = input_allocate_device();
  167. if (!inst->key_dev) {
  168. rtapi_print_msg(RTAPI_MSG_ERR,"matrix_kb: Not enough memory\n");
  169. return -1;
  170. }
  171.  
  172. inst->key_dev->name = inst->name;
  173. set_bit(EV_KEY, inst->key_dev->evbit);
  174.  
  175.  
  176. for(j = 0; j < 226 ; j++){
  177. set_bit(j, inst->key_dev->keybit);
  178. }
  179.  
  180. retval = input_register_device(inst->key_dev);
  181. if (retval) {
  182. printk(KERN_ERR "button.c: Failed to register device\n");
  183. return -1;
  184. }
  185.  
  186. }
  187. hal_ready(comp_id);
  188.  
  189. return 0;
  190. }
  191.  
  192. void rtapi_app_exit(void)
  193. {
  194. int i;
  195. hal_exit(comp_id);
  196. for (i = 0; i < kb->num_insts ; input_unregister_device(kb->insts[i].key_dev), i++);
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement