P5_Automation_can-dev-res.ino 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* P5_Automation_can-dev
  2. * 2020-06 testing communications CAN for integrate further with Procesy5
  3. * A.Binder
  4. * Uses:
  5. * LCD1602 - big 2x16 character display
  6. * TFT_ILI9163C 128x128 lcd display
  7. * MCP_2515 - can port controller
  8. *
  9. * controls
  10. * Slave ports via:
  11. * I2C
  12. * PCA9685 - 16port 12bit PWM driver - can controll 16 eg. LEDs which can be 0....12V dimmed=]
  13. * todo 74HC595 - Serial to Parallel Shifting-Out - from 8 ports to many if connected in paraller
  14. * based on https://www.arduino.cc/en/tutorial/ShiftOut
  15. * or 16 servos
  16. * - can have addressed many of PCA9685 on I2C bus - so can controll over 64 ports
  17. * to be added another devices tested
  18. * todo Rottary Swich { GND, +, SW, DT, CLK } - to provide LCD menu access
  19. * TODO structure of port is in _Slave_Ports.h
  20. * _Slave_Port _Slave_Ports[] = {
  21. {"CAN", 0x102, 0x80, 0x00, 0, 0x00, 0x00 },
  22. {"CAN", - Base port origin protocol
  23. 0x102, - Base port origin protocol address
  24. 0x80, - int PortTypes; 0xFF = 1111 1111
  25. 0x01 = 0000 0001 = PWM port
  26. {
  27. bit 1 - PWM port
  28. bit 2 - not allocated
  29. bit 3 - not allocated
  30. bit 4 - not allocated
  31. bit 2 - not allocated
  32. bit 5 - not allocated
  33. bit 6 - not allocated
  34. bit 7 - not allocated
  35. bit 8 - not allocated
  36. },
  37. 0x00, int Address - local address on e.g. I2C bus - 0x40 etc
  38. 0x00, - int LocalPort - local port number when used e.g. I2C extension
  39. 0x01 - Exp_state - 0xFF = 1111 1111
  40. 0x00 = 0000 0000 = OFF = 0V
  41. 0x01 = 0000 0001 = ON = PWM max
  42. {
  43. bit 1 - OFF = 0V
  44. bit 2 - ON = PWM max
  45. bit 3 - not allocated
  46. bit 4 - not allocated
  47. bit 2 - not allocated
  48. bit 5 - not allocated
  49. bit 6 - not allocated
  50. bit 7 - not allocated
  51. bit 8 - not allocated
  52. },
  53. 0x6d - testowy ttl - 10 sekund do przedawnienia stanu portu w tablicy i koniecznosci odswiezenia tablicy
  54. }
  55. };
  56. - to be optimalized for inegration
  57. - to allow of subscribe ports
  58. - to allow of concurent routing
  59. - to allow new port detectioj
  60. - to allow creating ports dependencies e.g. IF
  61. - port has value
  62. - port AND port
  63. THEN
  64. - set port
  65. - probably should be avilable creations of virtual ports which would describe
  66. - state of port AND port etc...
  67. * TO BE INTEGRATED WITH p5
  68. * - interface and WFST
  69. * TO BE INGEGRATED WITH MARLIN 3D PRINTERS - machine and automation purpose
  70. * - controll remote ports via g-code
  71. * - allows interrupts
  72. * TO ALLOW OF NEXT PACKETS TYPES WITH DIFFERENT MEANINGS
  73. * - dimming
  74. * logic
  75. */
  76. //#define HAS_TFT_ILI9163C
  77. //#define HAS_LCD1602
  78. //#define HAS_ROTTARY_SW
  79. #define HAS__PCINT_setup //to allow interrupts
  80. #define DEBUG 5 //Debug level
  81. #include "__main_struct.h"
  82. #include "__main_struct_funct.h"
  83. #include "__main_struct_limits.h"
  84. #include "_time_struct.h"
  85. #define _ControllerID 0x101
  86. #define _Slave1_ID 0x103
  87. #include "config_Active_ControllerID.h"
  88. #if defined(HAS_TFT_ILI9163C)
  89. #include "_TFT_ILI9163C_config.h"
  90. #endif
  91. //#include "Active_ControllerID_0x103.h"
  92. //#define Active_ControllerID _ControllerID
  93. //#define Active_ControllerID _Slave1_ID
  94. // #define HAS_I2C_driver_Adafruit_PWMServoDriver
  95. #include "_DISPLAY_struct.h"
  96. #include "_Port_ID_sruct.h"
  97. #include "_I2C_driver_Adafruit_PWMServoDriver_struct.h"
  98. #include "_TASK_TYPES_struct.h"
  99. #include "_Slave_Ports_Status_QUEUE_struct.h"
  100. //#include "Slave_Ports_Status_QUEUE.h"
  101. #include "Queue.h"
  102. Queue<int> _Slave_Ports_queue = Queue<int>(10);
  103. #if Active_ControllerID == _ControllerID
  104. String lcdPattern = "s" ;
  105. #endif
  106. #if Active_ControllerID == _Slave1_ID
  107. String lcdPattern = "." ;
  108. #endif
  109. //BEGIN 1602
  110. #if defined(HAS_LCD1602)
  111. #include <LiquidCrystal.h>
  112. // initialize the library with the numbers of the interface pins
  113. //LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
  114. //LiquidCrystal lcd(4, 6, 53, 51, 50, 52 );
  115. #include "_LCD1602_struct.h"
  116. // #include "_LCD1602_init.h"
  117. #endif
  118. #if defined(HAS_TFT_ILI9163C)
  119. #include "_TFT_ILI9163C_struct.h"
  120. #endif
  121. #if defined(HAS__PCINT_setup)
  122. #include "_PCINT_struct.h"
  123. #endif
  124. //BEGIN CAN
  125. #include "_CAN_init.h"
  126. //EOF CAN
  127. //REMOTE PORTS DEFINITIONS
  128. #include "_PortTypes_struct.h"
  129. #include "_PortStates_struct.h"
  130. #include "_Port_Exp_state_ttl_struct.h"
  131. #include "_Port_Broadcast_state_struct.h"
  132. #include "_Port_I2C_driver_struct.h"
  133. #include "_Slave_Ports_Status_struct.h"
  134. #include "_Slave_Ports_Protocol_CAN_struct.h"
  135. #if defined(HAS_TFT_ILI9163C)
  136. #include "_TFT_ILI9163C_func.h"
  137. #endif
  138. #include "_DISPLAY_func.h"
  139. #if defined(HAS_LCD1602)
  140. #include "_LCD1602_func.h"
  141. //#include "_LCD1602_init.h"
  142. #endif
  143. #if defined(HAS_ROTTARY_SW)
  144. #include "_ROTTARY_SW_func.h"
  145. #endif
  146. #include "_TASK_TYPES_func.h"
  147. #include "_Slave_Ports_Status_QUEUE_func.h"
  148. #include "_Slave_Ports_Protocol_CAN_func.h"
  149. #include "_Slave_Ports_Status_func.h"
  150. #include "_Slave_Ports_broadcast_func.h"
  151. #include "_Slave_Ports_ttl_decrement_func.h"
  152. #include "_Slave_Ports.h"
  153. // #include "_Slave_Ports_ttl_decrement_POP.h"
  154. // #define _CAN_P_0x102_PWM0x00_0 { _Slave1_ID , "PWM", 0x00, 0 }
  155. //#define _CAN_P_0x102_PWMServoDriver0x40
  156. // #define Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver();
  157. // Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);
  158. // #define __CSpin 53 // 10 // chip select
  159. #if defined(HAS_LCD1602)
  160. #include "_LCD1602_init.h"
  161. #endif
  162. #if defined(HAS_TFT_ILI9163C)
  163. #include "_TFT_ILI9163C_init.h"
  164. #endif
  165. #include "_DISPLAY_init.h"
  166. #if defined(HAS_ROTTARY_SW)
  167. #include "_ROTTARY_SW_init.h"
  168. #endif
  169. #if defined(HAS_I2C_driver_Adafruit_PWMServoDriver)
  170. #include "_I2C_driver_Adafruit_PWMServoDriver_init.h"
  171. #endif
  172. #if defined(HAS__PCINT_setup)
  173. #include "_PCINT_init.h"
  174. #endif
  175. #include "config_Slave_Ports_LOCAL_init.h"
  176. ARDUINO_FUNCT_INIT_RESULT_T ARDUINO_FUNCT_INIT_RESULT__Slave_Ports_LOCAL_init =
  177. _Slave_Ports_LOCAL_init(
  178. Slave_Ports_Status_A ,
  179. Slave_Ports_Status_T_ARDUINO_ATTR_ARRAY_A,
  180. Slave_Ports_Status_T_ARDUINO_ARRAY_INDEX_TABLE_A,
  181. Slave_Ports_Status_T_ARDUINO_ARRAY_INDEX_T_TO_DELETE_A,
  182. Slave_Ports_Status_T_ARDUINO_ARRAY_ASSOC_TABLE_S_Port_ID_A,
  183. DISPLAY_A , DISPLAY_T_ARDUINO_ATTR_ARRAY_A
  184. //, DISPLAY_PHYSICAL_CHAR_A
  185. );
  186. void setup() {
  187. Serial.begin(115200);
  188. // Serial.begin(9600);
  189. // Serial.println("#233 setup" );
  190. // _LCD_print_queue.push(1);
  191. // _LCD_print_queue.push(2);
  192. // _LCD_print_queue.push(3);
  193. //String character = "Geas";
  194. //char b = abc.charAt(0);
  195. //int b_ascii_value = b;
  196. // LCD_print_background_add( _LCD_print_buffors, 0, 0, 0, "W", 5000, 6000);
  197. // LCD_print_background_add( _LCD_print_buffors, 1, 0, 0, "H", 5000, 6000);
  198. // LCD_print_background_add( _LCD_print_buffors, 2, 0, 0, "W", 5000, 6000);
  199. // LCD_print_background_add( _LCD_print_buffors, 3, 0, 0, "K", 5000, 6000);
  200. //todo PCINT service
  201. #include "_PCINT_setup.h"
  202. #if defined(HAS_ROTTARY_SW)
  203. #include "_ROTTARY_SW_setup.h"
  204. #endif
  205. /* TODO
  206. String LCDarray1="CAN_1602_LCD "; //the string to print onthe LCD
  207. String LCDarray2="Testing id "; //the string to print onthe LCD
  208. LCDarray2.concat(String(Active_ControllerID, HEX) ) ;
  209. */
  210. // put your setup code here, to run once:
  211. #if defined(HAS_LCD1602)
  212. #include "_LCD1602_setup.h"
  213. #endif
  214. #if defined(HAS_TFT_ILI9163C)
  215. #include "_TFT_ILI9163C_setup.h"
  216. #endif
  217. //BEGIN CAN
  218. #include "_CAN_setup.h"
  219. #if defined(HAS_LCD1602)
  220. /* todo
  221. LCDarray1 = "loop.... ";
  222. LCDarray2 = "....";
  223. LCD_print(LCDarray1, LCDarray2, LCDtim/10);
  224. */
  225. #endif
  226. #include "__main_setup.h"
  227. }
  228. //#if Active_ControllerID == _ControllerID
  229. // byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  230. //#endif
  231. //String LCDarray1 ;
  232. //String LCDarray2 ;
  233. unsigned long time;
  234. void loop() {
  235. #include "_PCINT_loop.h"
  236. time = millis();
  237. Serial.println("Time: " );
  238. Slave_Ports_Status__DISPLAY_TARGET(
  239. Slave_Ports_Status_A ,
  240. Slave_Ports_Status_T_ARDUINO_ATTR_ARRAY_A,
  241. 0 ,
  242. DISPLAY_A ,
  243. DISPLAY_T_ARDUINO_ATTR_ARRAY_A ,
  244. DISPLAY_PHYSICAL_CHAR_A,
  245. DISPLAY_pos_seq_A_NEXT + DISPLAY_pos_seq_A_IMMEDIATE,
  246. "336 PORTS:",
  247. DISPLAY_TARGET_DISPLAY128,
  248. 0,
  249. 0
  250. );
  251. // Serial.println(time);
  252. #include "_SlaveID_loop1.h" //todo PCINT
  253. #include "_ControllerID_loop1.h" //todo make default
  254. #include "__main_loop_end.h"
  255. // DISPLAY_DEV.setCursor(10,10);
  256. // DISPLAY_DEV.print(" Test307");
  257. /*
  258. DISPLAY__print_ANY(
  259. DISPLAY_T_ARDUINO_ATTR_ARRAY_A ,
  260. DISPLAY_PHYSICAL_CHAR_A,
  261. DISPLAY_pos_seq_A_NEXT,
  262. "123456789012345678901234567890"
  263. ); */
  264. delay(10000);
  265. }
  266. #include "_PCINT_func_loop.h" //todo empty implement display etc
  267. #include "_Slave_Ports_func_loop.h" //todo implement