capture_data.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2012-2014 Wind River Systems, Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <zephyr.h>
  7. #include <sys/printk.h>
  8. #include <device.h>
  9. #include <drivers/input/input_dev.h>
  10. #include <ir_data_hal.h>
  11. #include "IR_context.h"
  12. #include <drivers/nvram_config.h>
  13. struct capture_data_handle dp_handle;
  14. int capture_data_getlen(u32_t data_mask)
  15. {
  16. int result = 0;
  17. while(data_mask > 0) {
  18. result++;
  19. data_mask = data_mask >> 1;
  20. }
  21. return result;
  22. }
  23. u32_t capture_data_get_lsb(u32_t data, u32_t mask, u32_t offset)
  24. {
  25. u32_t result;
  26. result = data & (mask << offset);
  27. result = result >> offset;
  28. return result;
  29. }
  30. u32_t capture_data_get_msb(u32_t data, u32_t mask, u32_t offset)
  31. {
  32. u32_t result;
  33. result = data & (mask >> (32 - offset));
  34. result = result << (32 - offset);
  35. return result;
  36. }
  37. void capture_data_process(struct capture_protocol *protocol, u32_t *data)
  38. {
  39. u32_t data0, data1, addr0,addr1, data_offset, addr_offset;
  40. u32_t data_mask, data_len, data_temp;
  41. int i;
  42. data0 = capture_data_get_lsb(protocol->data[0], d_format[protocol->mode].dcode_mask, d_format[protocol->mode].dcode_offset);
  43. data1 = capture_data_get_msb(protocol->data[1], d_format[protocol->mode].dcode_mask, d_format[protocol->mode].dcode_offset);
  44. addr0 = capture_data_get_lsb(protocol->data[0], d_format[protocol->mode].ucode_mask, d_format[protocol->mode].ucode_offset);
  45. addr1 = capture_data_get_lsb(protocol->data[1], d_format[protocol->mode].ucode_mask, d_format[protocol->mode].ucode_offset);
  46. data_offset = d_format[protocol->mode].dcode_offset;
  47. addr_offset = d_format[protocol->mode].ucode_offset;
  48. *data = data1 | data0;
  49. data_mask = d_format[protocol->mode].dcode_mask;
  50. if(d_format[protocol->mode].code_endian == Little_Endian) {
  51. data_len = capture_data_getlen(data_mask);
  52. data_temp = 0;
  53. for(i = 0; i < data_len; i++) {
  54. if(*data & (1 << i))
  55. data_temp = data_temp | (1 << (data_len - 1 - i));
  56. }
  57. *data = data_temp;
  58. }
  59. }
  60. void capture_data_callback(struct device *dev, struct input_value *val)
  61. {
  62. struct capture_data_handle *capture_data = &dp_handle;
  63. struct capture_protocol *protocol_data = &val->ir.protocol;
  64. struct input_value report_val;
  65. u32_t cmd_val[2];
  66. u32_t data_code;
  67. capture_data_process(protocol_data, &data_code);
  68. report_val.ir.data.mode = protocol_data->mode;
  69. report_val.ir.data.onoff = 1;
  70. cmd_val[0] = data_code;
  71. cmd_val[1] = 0;
  72. report_val.ir.data.cmd = cmd_val;
  73. if(capture_data->capture_notify != NULL)
  74. capture_data->capture_notify(NULL, &report_val);
  75. }
  76. void capture_ir_init(capture_notify_cb cb)
  77. {
  78. struct capture_data_handle *capture_data = &dp_handle;
  79. capture_data->handle = capture_device_open(capture_data_callback,CAPTURE_DEVICE_NAME);
  80. if(capture_data->handle == NULL)
  81. printk("can not get right device\n");
  82. capture_data->capture_notify = cb;
  83. }