Advertisement
XirallicBolts

Flex - Power button test rough - 0.1

Sep 11th, 2019
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. #include <Canbus.h>
  2. #include <defaults.h>
  3. #include <global.h>
  4. #include <mcp2515.h>
  5. #include <mcp2515_defs.h>
  6.  
  7. void setup() {
  8. Serial.begin(500000); // For debug use
  9. Serial.println("CAN Read - Testing receival of CAN Bus message");
  10. delay(1000);
  11.  
  12. if(Canbus.init(CANSPEED_500)) //Initialise MCP2515 CAN controller at the specified speed
  13. Serial.println("CAN Init ok");
  14. else
  15. Serial.println("Can't init CAN");
  16.  
  17. delay(1000);
  18. }
  19.  
  20. //********************************Main Loop*********************************//
  21.  
  22. int StartCounting = 0;
  23. int PowerLoops = 0;
  24.  
  25. void loop(){
  26.  
  27. tCAN message;
  28. if (mcp2515_check_message())
  29. {
  30. if (mcp2515_get_message(&message))
  31. {
  32. if(message.id == 0x2A0 and message.data[0] == 0x1F and message.data[1] == 0xFF and message.data[2] == 0xFF and message.data[3] == 0xFF and message.data[4] == 0x10 and message.data[5] == 0x00 and message.data[6] == 0x1E and message.data[7] == 0x02)
  33. {
  34. Serial.println("power button pressed, counting");
  35. StartCounting = 1;
  36. }
  37.  
  38. if(StartCounting == 1)
  39. {
  40. if(message.id == 0x2A0 and message.data[0] == 0x1F and message.data[1] == 0xFF and message.data[2] == 0xFF and message.data[3] == 0xFF and message.data[4] == 0x10 and message.data[5] == 0x00 and message.data[6] == 0x1E and message.data[7] == 0x00)
  41. {
  42. Serial.println("still pressed");
  43. PowerLoops++;
  44. }
  45. }
  46. if(message.id == 0x2A0 and message.data[0] == 0x1F and message.data[1] == 0xFF and message.data[2] == 0xFF and message.data[3] == 0xFF and message.data[4] == 0x00 and message.data[5] == 0x00 and message.data[6] == 0x1E and message.data[7] == 0x00)
  47. {
  48. StartCounting = 0;
  49. Serial.println("button released. counts: ");
  50. Serial.println(PowerLoops);
  51. if(PowerLoops >= 3)
  52. {
  53. Serial.println("turn audio back on");
  54. Serial.println("power press");
  55. message.id = 0x2A0; //formatted in HEX
  56. message.header.rtr = 0;
  57. message.header.length = 8; //formatted in DEC
  58. message.data[0] = 0x1F;
  59. message.data[1] = 0xFF;
  60. message.data[2] = 0xFF;
  61. message.data[3] = 0xFF; //formatted in HEX
  62. message.data[4] = 0x10;
  63. message.data[5] = 0x00;
  64. message.data[6] = 0xFF;
  65. message.data[7] = 0x02;
  66. mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
  67. mcp2515_send_message(&message);
  68. Serial.println("power release");
  69. message.id = 0x2A0; //formatted in HEX
  70. message.header.rtr = 0;
  71. message.header.length = 8; //formatted in DEC
  72. message.data[0] = 0x1F;
  73. message.data[1] = 0xFF;
  74. message.data[2] = 0xFF;
  75. message.data[3] = 0xFF; //formatted in HEX
  76. message.data[4] = 0x00;
  77. message.data[5] = 0x00;
  78. message.data[6] = 0xFF;
  79. message.data[7] = 0x00;
  80. mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
  81. mcp2515_send_message(&message);
  82. Serial.println("screen off");
  83. message.id = 0x2A0; //formatted in HEX
  84. message.header.rtr = 0;
  85. message.header.length = 8; //formatted in DEC
  86. message.data[0] = 0x50;
  87. message.data[1] = 0xFF;
  88. message.data[2] = 0xFF;
  89. message.data[3] = 0xFF; //formatted in HEX
  90. message.data[4] = 0x10;
  91. message.data[5] = 0x00;
  92. message.data[6] = 0xFF;
  93. message.data[7] = 0x02;
  94. mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
  95. mcp2515_send_message(&message);
  96. message.id = 0x2A0; //formatted in HEX
  97. message.header.rtr = 0;
  98. message.header.length = 8; //formatted in DEC
  99. message.data[0] = 0x50;
  100. message.data[1] = 0xFF;
  101. message.data[2] = 0xFF;
  102. message.data[3] = 0xFF; //formatted in HEX
  103. message.data[4] = 0x00;
  104. message.data[5] = 0x00;
  105. message.data[6] = 0xFF;
  106. message.data[7] = 0x00;
  107. mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
  108. mcp2515_send_message(&message);
  109. }
  110. PowerLoops = 0;
  111. }
  112. }
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement