ibutton_mode_cyfral_read.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #pragma once
  2. #include "ibutton.h"
  3. #include "cyfral_reader_comp.h"
  4. class AppiButtonModeCyfralRead : public AppTemplateMode<AppiButtonState, AppiButtonEvent> {
  5. public:
  6. const char* name = "cyfral read";
  7. AppiButton* app;
  8. CyfralReaderComp* reader;
  9. void event(AppiButtonEvent* event, AppiButtonState* state);
  10. void render(Canvas* canvas, AppiButtonState* state);
  11. void acquire();
  12. void release();
  13. AppiButtonModeCyfralRead(AppiButton* parent_app) {
  14. app = parent_app;
  15. // TODO open record
  16. const GpioPin* one_wire_pin_record = &ibutton_gpio;
  17. reader = new CyfralReaderComp(one_wire_pin_record);
  18. };
  19. static const uint8_t key_length = 4;
  20. static const uint8_t num_keys_to_check = 4;
  21. uint8_t key_index = 0;
  22. uint8_t keys[num_keys_to_check][4];
  23. };
  24. void AppiButtonModeCyfralRead::event(AppiButtonEvent* event, AppiButtonState* state) {
  25. if(event->type == AppiButtonEvent::EventTypeTick) {
  26. // if we read a key
  27. if(reader->read(keys[key_index], key_length)) {
  28. // read next key
  29. key_index++;
  30. // if we read sufficient amount of keys
  31. if(key_index >= num_keys_to_check) {
  32. bool result = true;
  33. key_index = 0;
  34. // compare all keys
  35. for(uint8_t i = 1; i < num_keys_to_check; i++) {
  36. if(memcmp(keys[i], keys[i - 1], key_length) != 0) {
  37. result = false;
  38. break;
  39. }
  40. }
  41. // if all keys is same
  42. if(result) {
  43. // copy key to mem and blink
  44. memcpy(
  45. app->state.cyfral_address[app->state.cyfral_address_index],
  46. keys[key_index],
  47. key_length);
  48. app->blink_green();
  49. }
  50. }
  51. }
  52. } else if(event->type == AppiButtonEvent::EventTypeKey) {
  53. if(event->value.input.type == InputTypeShort && event->value.input.key == InputKeyUp) {
  54. app->decrease_cyfral_address();
  55. }
  56. if(event->value.input.type == InputTypeShort && event->value.input.key == InputKeyDown) {
  57. app->increase_cyfral_address();
  58. }
  59. }
  60. }
  61. void AppiButtonModeCyfralRead::render(Canvas* canvas, AppiButtonState* state) {
  62. canvas_set_font(canvas, FontSecondary);
  63. canvas_draw_str(canvas, 2, 25, "< Cyfral read >");
  64. app->render_cyfral_list(canvas, state);
  65. }
  66. void AppiButtonModeCyfralRead::acquire() {
  67. reader->start();
  68. }
  69. void AppiButtonModeCyfralRead::release() {
  70. reader->stop();
  71. }