third_party.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. 'use strict';
  2. goog.provide('Blockly.Python.third_party');
  3. goog.require('Blockly.Python');
  4. Blockly.Python['sonar_setup'] = function(block) {
  5. Blockly.Python.addInclude('NewPing', '#include <NewPing.h>');
  6. let sName = 'sonar' + block.getFieldValue('NUMBER');
  7. let trig_pin = block.getFieldValue('TRIG_PIN');
  8. let echo_pin = block.getFieldValue('ECHO_PIN');
  9. Blockly.Python.reservePin(
  10. block, trig_pin, Blockly.Python.PinTypes.OUTPUT, 'Sonar_Trig_Pin' + trig_pin);
  11. Blockly.Python.reservePin(
  12. block, echo_pin, Blockly.Python.PinTypes.OUTPUT, 'Sonar_Echo_Pin' + echo_pin);
  13. var maxdist = block.getFieldValue('MAXDIST');
  14. let trigName = 'TRIG_' + trig_pin;
  15. let echoName = 'ECHO_' + echo_pin;
  16. let maxDistName = 'MAXDIST_' + trig_pin + '_' + echo_pin;
  17. var sonar_def = '#define ' + trigName + ' ' + trig_pin + '\n' +
  18. '#define ' + echoName + ' ' + echo_pin + '\n' +
  19. '#define ' + maxDistName + ' ' + maxdist + '\n' +
  20. 'NewPing ' + sName + '(' + trigName + ', ' + echoName + ', ' + maxDistName + ');\n';
  21. Blockly.Python.addDeclaration('define_' + sName, sonar_def);
  22. return '';
  23. }
  24. Blockly.Python['sonar_read_dist'] = function(block) {
  25. let sName = 'sonar' + block.getFieldValue('NUMBER');
  26. var code = sName + '.ping_cm()';
  27. return [code, Blockly.Python.ORDER_ATOMIC];
  28. }
  29. Blockly.Python['infraredRay_setup'] = function(block) {
  30. Blockly.Python.addInclude('IRLib', '' +
  31. '#include <IRLibDecodeBase.h>\n' +
  32. '#include <IRLib_P01_NEC.h>\n' +
  33. '#include <IRLibCombo.h>\n' +
  34. 'IRdecode myDecoder;\n' +
  35. '#include <IRLibRecv.h>\n' +
  36. 'IRrecv myReceiver(A2);\n' +
  37. '');
  38. var IR_keys_def = '#define KEYPAD_UP 0xFF18E7\n' +
  39. '#define KEYPAD_DOWN 0xFF4AB5\n' +
  40. '#define KEYPAD_LEFT 0xFF10EF\n' +
  41. '#define KEYPAD_RIGHT 0xFF5AA5\n' +
  42. '#define KEYPAD_OK 0xFF38C7\n' +
  43. '#define KEYPAD_1 0xFFA25D\n' +
  44. '#define KEYPAD_2 0xFF629D\n' +
  45. '#define KEYPAD_3 0xFFE21D\n' +
  46. '#define KEYPAD_4 0xFF22DD\n' +
  47. '#define KEYPAD_5 0xFF02FD\n' +
  48. '#define KEYPAD_6 0xFFC23D\n' +
  49. '#define KEYPAD_7 0xFFE01F\n' +
  50. '#define KEYPAD_8 0xFFA857\n' +
  51. '#define KEYPAD_9 0xFF906F\n' +
  52. '#define KEYPAD_0 0xFF9867\n' +
  53. '#define KEYPAD_STAR 0xFF6897\n' +
  54. '#define KEYPAD_NUMSIGN 0xFFB04F\n';
  55. Blockly.Python.addDeclaration('IR_keys_def', IR_keys_def);
  56. var IR_setup = 'Serial.begin(9600);\n' +
  57. 'delay(2000);\n' +
  58. 'myReceiver.enableIRIn();';
  59. Blockly.Python.addSetup('IR_setup', IR_setup, false);
  60. var code = '' +
  61. 'if (myReceiver.getResults()) {\n' +
  62. 'myDecoder.decode(); \n' +
  63. '// myDecoder.dumpResults(true);\n' +
  64. 'myReceiver.enableIRIn();\n' +
  65. '}\n' +
  66. '';
  67. return code;
  68. };
  69. Blockly.Python['infraredRay_receive_val'] = function(block) {
  70. var code = 'myDecoder.value';
  71. return [code, Blockly.Python.ORDER_ATOMIC];
  72. };
  73. Blockly.Python['infraredRay_send_val'] = function(block) {
  74. var dropdown_valtype = block.getFieldValue('VALTYPE');
  75. var code = dropdown_valtype;
  76. return [code, Blockly.Python.ORDER_ATOMIC];
  77. };
  78. /* RTC
  79. * Setup
  80. */
  81. Blockly.Python['RTC_setup'] = function(block) {
  82. Blockly.Python.addInclude('RTC_include', '#include <Wire.h> //include Wire.h library \n' +
  83. '#include "RTClib.h" //include Adafruit RTC library');
  84. Blockly.Python.addDeclaration("RTC_declaration", 'RTC_DS3231 rtc; //Make a RTC DS3231 object\n' +
  85. ' //Set the names of days\n' +
  86. 'char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};')
  87. Blockly.Python.addSetup("RTC_setup", ' Serial.begin(9600);\n' +
  88. ' if (! rtc.begin()) {\n' +
  89. ' Serial.println("Couldn\'t find RTC");\n' +
  90. ' while (1);\n' +
  91. ' }\n' +
  92. ' //Setup of time if RTC lost power or time is not set\n' +
  93. ' if (rtc.lostPower()) {\n' +
  94. ' //Sets the code compilation time to RTC DS3231\n' +
  95. ' rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));\n' +
  96. ' }\n');
  97. var code = "DateTime now = rtc.now();\n";
  98. return code;
  99. }
  100. /* RTC
  101. * Set Date/Week/Time
  102. */
  103. Blockly.Python['RTC_setDateTime'] = function(block) {
  104. var rtc_hour = block.getFieldValue('hour');
  105. var rtc_min = block.getFieldValue('minute');
  106. var rtc_sec = block.getFieldValue('second');
  107. var rtc_year = block.getFieldValue('year');
  108. var rtc_month = block.getFieldValue('month');
  109. var rtc_day = block.getFieldValue('day');
  110. Blockly.Python.addSetup("RTC_set", "rtc.adjust(DateTime(" + rtc_year + "," + rtc_month + "," + rtc_day + "," + rtc_hour + "," + rtc_min + "," + rtc_sec + "));");
  111. return "";
  112. }
  113. /* RTC
  114. * get Date
  115. */
  116. Blockly.Python['RTC_getDate'] = function(block) {
  117. var detail = block.getFieldValue("date_detail");
  118. var code = "";
  119. switch (detail) {
  120. case "year":
  121. code = "now.year()";
  122. break;
  123. case "month":
  124. code = "now.month()";
  125. break;
  126. case "day":
  127. code = "now.day()";
  128. break;
  129. default:
  130. break;
  131. }
  132. return [code, Blockly.Python.ORDER_ATOMIC];
  133. }
  134. /* RTC
  135. * get Time
  136. */
  137. Blockly.Python['RTC_getTime'] = function(block) {
  138. var detail = block.getFieldValue("time_detail");
  139. var code = "";
  140. switch (detail) {
  141. case "hour":
  142. code = "now.hour()";
  143. break;
  144. case "minute":
  145. code = "now.minute()";
  146. break;
  147. case "second":
  148. code = "now.second()";
  149. break;
  150. default:
  151. break;
  152. }
  153. return [code, Blockly.Python.ORDER_ATOMIC];
  154. }
  155. /* RTC
  156. * get Week
  157. */
  158. Blockly.Python['RTC_getWeek'] = function(block) {
  159. var code = "daysOfTheWeek[now.dayOfTheWeek()]";
  160. return [code, Blockly.Python.ORDER_ATOMIC];
  161. }
  162. /* PulseSensor */
  163. Blockly.Python['PulseSensor_setup'] = function(block) {
  164. var pin = block.getFieldValue("PIN")
  165. Blockly.Python.addInclude("PulseSensor_inc", "#define USE_ARDUINO_INTERRUPTS true\n" +
  166. "#include <PulseSensorPlayground.h>");
  167. Blockly.Python.addDeclaration("PulseSensor_dec", "const int OUTPUT_TYPE = SERIAL_PLOTTER;\n" +
  168. "const int PIN_INPUT = " + pin + ";\n" +
  169. "const int THRESHOLD = 550;\n" + // Adjust this number to avoid noise when idle
  170. "PulseSensorPlayground pulseSensor;");
  171. Blockly.Python.addSetup("PulseSensor_setup", "pulseSensor.analogInput(PIN_INPUT);\n" +
  172. " pulseSensor.setSerial(Serial);\n" +
  173. " pulseSensor.setOutputType(OUTPUT_TYPE);\n" +
  174. " pulseSensor.setThreshold(THRESHOLD);\n" +
  175. " if (!pulseSensor.begin())\n" +
  176. " {\n" +
  177. " for (;;) {}\n" +
  178. " }");
  179. return "";
  180. }
  181. /* PulseSensor */
  182. Blockly.Python['PulseSensor_getBPM'] = function(block) {
  183. var code = "pulseSensor.getBeatsPerMinute()";
  184. return [code, Blockly.Python.ORDER_ATOMIC];
  185. }
  186. /* PulseSensor */
  187. Blockly.Python['PulseSensor_Heart_Beats'] = function(block) {
  188. var code = "pulseSensor.sawStartOfBeat()";
  189. return [code, Blockly.Python.ORDER_ATOMIC];
  190. }
  191. /* PulseSensor */
  192. Blockly.Python['PulseSensor_Heart_noBeats'] = function(block) {
  193. var code = "pulseSensor.isInsideBeat() == false";
  194. return [code, Blockly.Python.ORDER_ATOMIC];
  195. }
  196. /* PulseSensor */
  197. Blockly.Python['PulseSensor_print'] = function(block) {
  198. var code = "pulseSensor.outputSample();\n";
  199. return code;
  200. }
  201. /*Encoder*/
  202. Blockly.Python['Encoder_Setup'] = function(block) {
  203. var pin = block.getFieldValue("PIN");
  204. Blockly.Python.addDeclaration("Encoder_dec", "int encoderCounter = 0;\n" +
  205. "unsigned long encoderTime = 0, old_encoderTime = 0;\n" +
  206. "unsigned long encoderTime1 = 0;\n" +
  207. "float encoderSpeed;\n")
  208. var setup = "Serial.begin(9600);\n" +
  209. "attachInterrupt(" + pin + ", encoderCount_CallBack, FALLING);\n";
  210. Blockly.Python.addSetup("Encoder_setup", setup);
  211. var func = "bool SpeedDetection()\n" +
  212. "{\n" +
  213. " encoderTime = millis();\n" +
  214. " if (abs(encoderTime - old_encoderTime) >= 1000)\n" +
  215. " {\n" +
  216. " detachInterrupt(" + pin + ");\n" +
  217. " encoderSpeed = (float)encoderCounter * 60 / 20;\n" +
  218. " encoderCounter = 0;\n" +
  219. " old_encoderTime = millis();\n" +
  220. " attachInterrupt(" + pin + ", encoderCount_CallBack, FALLING); \n" +
  221. " return 1;\n" +
  222. " }\n" +
  223. " else\n" +
  224. " return 0;\n" +
  225. "}\n\n" +
  226. "void encoderCount_CallBack()\n" +
  227. "{\n" +
  228. " encoderCounter++;\n" +
  229. "}";
  230. Blockly.Python.addFunction("Encoder_func", func);
  231. var code = "SpeedDetection();\n";
  232. return code;
  233. }
  234. /*Encoder*/
  235. Blockly.Python['Encoder_getRotationSpeed'] = function(block) {
  236. var code = "encoderSpeed";
  237. return [code, Blockly.Python.ORDER_ATOMIC];
  238. }
  239. /* RFID
  240. * Setup
  241. */
  242. Blockly.Python['RFID_Setup'] = function(block) {
  243. Blockly.Python.addInclude('RFID_include', '#include <SPI.h>\n' +
  244. '#include <MFRC522.h>');
  245. Blockly.Python.addDeclaration('RFID_dec', '#define SS_PIN SS\n' +
  246. 'MFRC522 rfid(SS_PIN); // Instance of the class\n' +
  247. 'MFRC522::MIFARE_Key key;\n' +
  248. 'byte nuidPICC[4];\n' +
  249. 'char NUID[4];\n' +
  250. 'String nuid_str="null";');
  251. Blockly.Python.addSetup('RFID_setup', 'Serial.begin(9600);\n' +
  252. 'SPI.begin(); // Init SPI bus\n' +
  253. 'rfid.PCD_Init(); // Init MFRC522\n' +
  254. 'for (byte i = 0; i < 6; i++) {\n' +
  255. ' key.keyByte[i] = 0xFF;\n' +
  256. '}\n' +
  257. 'Serial.println(F("This code scan the MIFARE Classsic NUID."));\n' +
  258. 'Serial.print(F("Using the following key:"));\n' +
  259. 'printHex(key.keyByte, MFRC522::MF_KEY_SIZE);\n');
  260. Blockly.Python.addFunction("RFID_printHex", 'void printHex(byte *buffer, byte bufferSize) {\n' +
  261. ' for (byte i = 0; i < bufferSize; i++) {\n' +
  262. //Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  263. ' Serial.print(buffer[i], HEX);\n' +
  264. ' }\n' +
  265. '}');
  266. Blockly.Python.addFunction("RFID_tohex", 'String tohex(int n) {\n' +
  267. ' if (n == 0) {\n' +
  268. ' return "00"; //n为0\n' +
  269. ' }\n' +
  270. ' String result = "";\n' +
  271. ' char _16[] = {\'0\',\'1\',\'2\',\'3\',\'4\',\'5\',\'6\',\'7\', \'8\',' +
  272. '\'9\',\'A\',\'B\',\'C\',\'D\',\'E\',\'F\'};\n' +
  273. ' const int radix = 16;\n' +
  274. ' while (n) {\n' +
  275. ' int i = n % radix;\n' +
  276. ' result = _16[i] + result;\n' +
  277. ' n /= radix;\n' +
  278. ' }\n' +
  279. ' if (result.length() < 2) {\n' +
  280. ' result = \'0\' + result;\n' +
  281. ' }\n' +
  282. ' return result;\n' +
  283. '}');
  284. var code = '// Look for new cards\n' +
  285. 'if (!rfid.PICC_IsNewCardPresent())\n' +
  286. ' return;\n' +
  287. // Verify if the NUID has been readed
  288. 'if (!rfid.PICC_ReadCardSerial())\n' +
  289. ' return;\n' +
  290. 'MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);\n' +
  291. '// Check is the PICC of Classic MIFARE type\n' +
  292. 'if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&\n' +
  293. ' piccType != MFRC522::PICC_TYPE_MIFARE_1K &&\n' +
  294. ' piccType != MFRC522::PICC_TYPE_MIFARE_4K) {\n' +
  295. ' Serial.println(F("Your tag is not supported"));\n' +
  296. ' return;\n' +
  297. '}\n' +
  298. '// Store NUID into nuidPICC array\n' +
  299. 'for (byte i = 0; i < 4; i++) {\n' +
  300. ' nuidPICC[i] = rfid.uid.uidByte[i];\n' +
  301. '}\n' +
  302. '//Store NUID into nuid_str\n' +
  303. 'nuid_str="";\n' +
  304. 'for (byte i = 0; i < 4; i++) {\n' +
  305. ' nuid_str += tohex(rfid.uid.uidByte[i]);\n' +
  306. '}\n' +
  307. // '// read nuid\n' +
  308. // 'printHex(rfid.uid.uidByte, rfid.uid.size);\n' +
  309. // 'Serial.println();\n' +
  310. '// Halt PICC\n' +
  311. 'rfid.PICC_HaltA();\n' +
  312. '// Stop encryption on PCD\n' +
  313. 'rfid.PCD_StopCrypto1();\n';
  314. return code;
  315. }
  316. /* RFID
  317. * Read ID
  318. */
  319. Blockly.Python['RFID_ReadID'] = function(block) {
  320. var code = "nuid_str";
  321. return [code, Blockly.Python.ORDER_ATOMIC];
  322. }
  323. /* GPS
  324. * Setup
  325. */
  326. Blockly.Python["GPS_setup"] = function(block) {
  327. Blockly.Python.addInclude('GPS_include', "#include <SoftwareSerial.h>\n" +
  328. "SoftwareSerial gpsSerial(11,10);// MOSI")
  329. Blockly.Python.addDeclaration("GPS_Dec", "struct\n" +
  330. "{\n" +
  331. " char GPS_Buffer[80];\n" +
  332. " bool isGetData;\n" +
  333. " bool isParseData;\n" +
  334. " char UTCTime[11];\n" +
  335. " char latitude[11];\n" +
  336. " char N_S[2];\n" +
  337. " char longitude[12];\n" +
  338. " char E_W[2];\n" +
  339. " bool isUsefull;\n" +
  340. "} Save_Data;\n" +
  341. "const unsigned int gpsRxBufferLength = 600;\n" +
  342. "char gpsRxBuffer[gpsRxBufferLength];\n" +
  343. "unsigned int ii = 0;\n" +
  344. "String GPSMODULEstringLat, GPSMODULEstringLong, GPSMODULEgpsLatitude, GPSMODULEgpsLongitude;\n" +
  345. "float GPSMODULEfloatLat, GPSMODULEfloatLon;\n"
  346. );
  347. Blockly.Python.addSetup("GPS_Setup", "gpsSerial.begin(9600);\n" +
  348. " Save_Data.isGetData = false;\n" +
  349. " Save_Data.isParseData = false;\n" +
  350. " Save_Data.isUsefull = false;\n"
  351. );
  352. Blockly.Python.addFunction("gpsMoudle", "void gpsModule() {\n" +
  353. " gpsRead();\n" + // Get GPS Data
  354. " parseGpsBuffer();\n" + // Parse GPS Data
  355. " convertRealCoordinate();\n" + // Get real coordinate
  356. "}\n"
  357. );
  358. Blockly.Python.addFunction("convertRealCoordinate", "void convertRealCoordinate() {\n" +
  359. " String GPSMODULEstringLat = Save_Data.latitude;\n" +
  360. " String GPSMODULEstringLon = Save_Data.longitude;\n" +
  361. " float GPSMODULEfloatLat = GPSMODULEstringLat.toFloat();\n" +
  362. " float GPSMODULEfloatLon = GPSMODULEstringLon.toFloat();\n" +
  363. " GPSMODULEgpsLatitude = String(convertDegMinToDecDeg(GPSMODULEfloatLat), 7);\n" +
  364. " GPSMODULEgpsLongitude = String(convertDegMinToDecDeg(GPSMODULEfloatLon), 7);\n" +
  365. // " Serial.println(GPSMODULEgpsLatitude);\n"+
  366. // " Serial.println(GPSMODULEgpsLongitude);\n"+
  367. "}\n"
  368. );
  369. Blockly.Python.addFunction("convertDegMinToDecDeg", "double convertDegMinToDecDeg (float degMin) {\n" +
  370. " double min = 0.0;\n" +
  371. " double decDeg = 0.0;\n" +
  372. " //get the minutes, fmod() requires double\n" +
  373. " min = fmod((double)degMin, 100.0);\n" +
  374. " //rebuild coordinates in decimal degrees\n" +
  375. " degMin = (int) ( degMin / 100 );\n" +
  376. " decDeg = degMin + ( min / 60 );\n" +
  377. " return decDeg;\n" +
  378. "}\n"
  379. );
  380. Blockly.Python.addFunction("errorLog", "void errorLog(int num)\n" +
  381. "{\n" +
  382. ' Serial.print("ERROR");\n' +
  383. " Serial.println(num);\n" +
  384. " while (1)\n" +
  385. " {\n" +
  386. " Serial.flush();\n" +
  387. " }\n" +
  388. "}\n"
  389. );
  390. Blockly.Python.addFunction("printGpsBuffer", "void printGpsBuffer()\n" +
  391. "{\n" +
  392. " if (Save_Data.isParseData)\n" +
  393. " {\n" +
  394. " Save_Data.isParseData = false;\n" +
  395. ' Serial.print("Save_Data.UTCTime = ");\n' +
  396. " Serial.println(Save_Data.UTCTime);\n" +
  397. " if (Save_Data.isUsefull)\n" +
  398. " {\n" +
  399. " Save_Data.isUsefull = false;\n" +
  400. ' Serial.print("Save_Data.latitude = ");\n' +
  401. ' Serial.println(Save_Data.latitude);\n' +
  402. ' Serial.print("Save_Data.N_S = ");\n' +
  403. ' Serial.println(Save_Data.N_S);\n' +
  404. ' Serial.print("Save_Data.longitude = ");\n' +
  405. ' Serial.println(Save_Data.longitude);\n' +
  406. ' Serial.print("Save_Data.E_W = ");\n' +
  407. ' Serial.println(Save_Data.E_W);\n' +
  408. " } else {\n" +
  409. ' Serial.println("GPS DATA is not usefull!");\n' +
  410. " }\n" +
  411. " }\n" +
  412. "}\n"
  413. );
  414. Blockly.Python.addFunction("parseGpsBuffer", "void parseGpsBuffer()\n" +
  415. "{\n" +
  416. " char *subString;\n" +
  417. " char *subStringNext;\n" +
  418. " if (Save_Data.isGetData)\n" +
  419. " {\n" +
  420. " Save_Data.isGetData = false;\n" +
  421. ' Serial.println("**************");\n' +
  422. " Serial.println(Save_Data.GPS_Buffer);\n" +
  423. " for (int i = 0 ; i <= 6 ; i++)\n" +
  424. " {\n" +
  425. " if (i == 0)\n" +
  426. " {\n" +
  427. " if ((subString = strstr(Save_Data.GPS_Buffer, \",\")) == NULL)\n" +
  428. " errorLog(1); // Parse error\n" +
  429. " }\n" +
  430. " else\n" +
  431. " {\n" +
  432. " subString++;\n" +
  433. ' if ((subStringNext = strstr(subString, ",")) != NULL)\n' +
  434. " {\n" +
  435. " char usefullBuffer[2];\n" +
  436. " switch (i)\n" +
  437. " {\n" +
  438. " case 1: memcpy(Save_Data.UTCTime, subString, subStringNext - subString); break;\n" +
  439. " case 2: memcpy(usefullBuffer, subString, subStringNext - subString); break;\n" +
  440. " case 3: memcpy(Save_Data.latitude, subString, subStringNext - subString); break;\n" +
  441. " case 4: memcpy(Save_Data.N_S, subString, subStringNext - subString); break;\n" +
  442. " case 5: memcpy(Save_Data.longitude, subString, subStringNext - subString); break;\n" +
  443. " case 6: memcpy(Save_Data.E_W, subString, subStringNext - subString); break;\n" +
  444. " default: break;\n" +
  445. " }\n" +
  446. " subString = subStringNext;\n" +
  447. " Save_Data.isParseData = true;\n" +
  448. " if (usefullBuffer[0] == 'A')\n" +
  449. " Save_Data.isUsefull = true;\n" +
  450. " else if (usefullBuffer[0] == 'V')\n" +
  451. " Save_Data.isUsefull = false;\n" +
  452. " }\n" +
  453. " else\n" +
  454. " {\n" +
  455. " errorLog(2);\n" +
  456. " }\n" +
  457. " }\n" +
  458. " }\n" +
  459. " }\n" +
  460. "}\n"
  461. );
  462. Blockly.Python.addFunction("gpsRead", 'void gpsRead() {\n' +
  463. ' while (gpsSerial.available())\n' +
  464. ' {\n' +
  465. ' gpsRxBuffer[ii++] = gpsSerial.read();\n' +
  466. ' if (ii == gpsRxBufferLength)clrGpsRxBuffer();\n' +
  467. ' }\n' +
  468. ' char* GPS_BufferHead;\n' +
  469. ' char* GPS_BufferTail;\n' +
  470. ' if ((GPS_BufferHead = strstr(gpsRxBuffer, "$GPRMC,")) != NULL || (GPS_BufferHead = strstr(gpsRxBuffer, "$GNRMC,")) != NULL )\n' +
  471. ' {\n' +
  472. ' if (((GPS_BufferTail = strstr(GPS_BufferHead, "\\r\\n")) != NULL) && (GPS_BufferTail > GPS_BufferHead))\n' +
  473. ' {\n' +
  474. ' memcpy(Save_Data.GPS_Buffer, GPS_BufferHead, GPS_BufferTail - GPS_BufferHead);\n' +
  475. ' Save_Data.isGetData = true;\n' +
  476. ' clrGpsRxBuffer();\n' +
  477. ' }\n' +
  478. ' }\n' +
  479. '}\n'
  480. );
  481. Blockly.Python.addFunction("clrGpsRxBuffer", 'void clrGpsRxBuffer(void) {\n' +
  482. ' memset(gpsRxBuffer, 0, gpsRxBufferLength);\n' +
  483. ' ii = 0;\n' +
  484. '}\n'
  485. );
  486. var code = "gpsModule();\n";
  487. return [code, Blockly.Python.ORDER_ATOMIC];
  488. };
  489. /* GPS
  490. * get latitude
  491. */
  492. Blockly.Python['GPS_getLatitude'] = function(block) {
  493. var code = "GPSMODULEgpsLatitude";
  494. return [code, Blockly.Python.ORDER_ATOMIC];
  495. }
  496. /* GPS
  497. * get longitude
  498. */
  499. Blockly.Python['GPS_getLongitude'] = function(block) {
  500. var code = "GPSMODULEgpsLongitude";
  501. return [code, Blockly.Python.ORDER_ATOMIC];
  502. }
  503. /* Water Temperature Sensor, DS18S20
  504. * setup
  505. */
  506. Blockly.Python['water_temp_sensor_setup'] = function(block) {
  507. var dropdown_water_temp_sensor_setup_dropdown = block.getFieldValue('water_temp_sensor_setup_dropdown');
  508. var number_water_temp_sensor_setup_pin = block.getFieldValue('water_temp_sensor_setup_pin');
  509. // TODO: Assemble JavaScript into code variable.
  510. Blockly.Python.addDeclaration("water_temp_sensor_setup_DECLARATION", "#include <OneWire.h>\n" +
  511. "int DS18S20_Pin = " + number_water_temp_sensor_setup_pin + ";\n" +
  512. "OneWire ds(DS18S20_Pin);\n"
  513. );
  514. Blockly.Python.addFunction("water_temp_sensor_setup_FUNCTION", "float getDS18S20WaterTemp() {\n" +
  515. "byte data[12];\n" +
  516. "byte addr[8];\n" +
  517. "if ( !ds.search(addr)) {\n" +
  518. " ds.reset_search();\n" +
  519. " return -1000;\n" +
  520. "}\n" +
  521. "if ( OneWire::crc8( addr, 7) != addr[7]) {\n" +
  522. " Serial.println(\"CRC is not valid!\");\n" +
  523. " return -1000;\n" +
  524. "}\n" +
  525. "if ( addr[0] != 0x10 && addr[0] != 0x28) {\n" +
  526. " Serial.print(\"Device is not recognized\");\n" +
  527. " return -1000;\n" +
  528. "}\n" +
  529. "ds.reset(); \n" +
  530. "ds.select(addr); \n" +
  531. "ds.write(0x44, 1); \n" +
  532. "byte present = ds.reset(); \n" +
  533. "ds.select(addr); \n" +
  534. "ds.write(0xBE); \n" +
  535. "for (int i = 0; i < 9; i++) { \n" +
  536. " data[i] = ds.read(); \n" +
  537. "} \n" +
  538. "ds.reset_search();\n" +
  539. "byte MSB = data[1];\n" +
  540. "byte LSB = data[0];\n" +
  541. "float tempRead = ((MSB << 8) | LSB);\n" +
  542. "float TemperatureSum = tempRead / 16;\n" +
  543. "return TemperatureSum;\n" +
  544. "}\n"
  545. );
  546. var code = "float getWaterTempValue = getDS18S20WaterTemp();\n"; // no code in loop function here
  547. return [code, Blockly.Python.ORDER_ATOMIC];
  548. };
  549. /* Water Temperature Sensor, get value
  550. */
  551. Blockly.Python['water_temp_sensor_get_value'] = function(block) {
  552. var code = "getWaterTempValue";
  553. return [code, Blockly.Python.ORDER_ATOMIC];
  554. }
  555. // CO2 sensor setup
  556. Blockly.Python['thirdparty_co2_sensor_setup'] = function(block) {
  557. var dropdown_thirdparty_co2_sensor_setup_sdapin = block.getFieldValue('thirdparty_co2_sensor_setup_sdapin');
  558. var dropdown_thirdparty_co2_sensor_setup_sclpin = block.getFieldValue('thirdparty_co2_sensor_setup_sclpin');
  559. // TODO: Assemble JavaScript into code variable.
  560. Blockly.Python.addInclude("thirdparty_co2_sensor_setup_include", '#include "Adafruit_CCS811.h"\n');
  561. Blockly.Python.addDeclaration("thirdparty_co2_sensor_setup_declaration", '' +
  562. 'Adafruit_CCS811 ccs;\n' +
  563. 'uint16_t _ccs811getCO2, _ccs811getTVOC;\n'
  564. );
  565. Blockly.Python.addFunction("thirdparty_co2_sensor_setup_function", '' +
  566. 'void _ccs811Setup() {\n' +
  567. ' Serial.begin(9600);\n' +
  568. ' Serial.println("CCS811 test");\n' +
  569. ' if (!ccs.begin()) {\n' +
  570. ' Serial.println("Failed to start sensor! Please check your wiring.");\n' +
  571. ' while (1);\n' +
  572. ' }\n' +
  573. ' while (!ccs.available());\n' +
  574. '}\n' +
  575. '\n' +
  576. 'void _ccs811Main() {\n' +
  577. ' if (ccs.available()) {\n' +
  578. ' if (!ccs.readData()) {\n' +
  579. ' _ccs811getCO2 = ccs.geteCO2();\n' +
  580. ' _ccs811getTVOC = ccs.getTVOC();\n' +
  581. ' }\n' +
  582. ' else {\n' +
  583. ' Serial.println("ERROR!");\n' +
  584. ' while (1);\n' +
  585. ' }\n' +
  586. ' }\n' +
  587. ' delay(100);\n' +
  588. '}\n'
  589. );
  590. Blockly.Python.addSetup("thirdparty_co2_sensor_setup_setup", '_ccs811Setup();\n');
  591. var code = "_ccs811Main();\n"; // no code in loop function here
  592. return [code, Blockly.Python.ORDER_ATOMIC];
  593. };
  594. /* CO2 sensor get Co2
  595. */
  596. Blockly.Python['thirdparty_co2_sensor_get_co2'] = function(block) {
  597. var code = "_ccs811getCO2";
  598. return [code, Blockly.Python.ORDER_ATOMIC];
  599. }
  600. /* CO2 sensor get TVOC
  601. */
  602. Blockly.Python['thirdparty_co2_sensor_get_tvoc'] = function(block) {
  603. var code = "_ccs811getTVOC";
  604. return [code, Blockly.Python.ORDER_ATOMIC];
  605. }
  606. // nrf module
  607. /*
  608. Blockly.Python.addInclude("", ''+
  609. '');
  610. Blockly.Python.addDeclaration("", ''+
  611. '');
  612. Blockly.Python.addSetup("", ''+
  613. '');
  614. Blockly.Python.addFunction("", ''+
  615. '');
  616. */
  617. Blockly.Python['thirdparty_nrf24l01_setup'] = function(block) {
  618. var dropdown_thirdparty_nrf24l01_setup_dropdown_cepin = block.getFieldValue('thirdparty_nrf24l01_setup_dropdown_cepin');
  619. var dropdown_thirdparty_nrf24l01_setup_dropdown_csnpin = block.getFieldValue('thirdparty_nrf24l01_setup_dropdown_csnpin');
  620. var text_thirdparty_nrf24l01_setup_addr = block.getFieldValue('thirdparty_nrf24l01_setup_addr');
  621. // TODO: Assemble JavaScript into code variable.
  622. Blockly.Python.addInclude("thirdparty_nrf24l01_setup_include", '' +
  623. '#include <SPI.h>\n' +
  624. '#include <nRF24L01.h>\n' +
  625. '#include <RF24.h>)\n' +
  626. '');
  627. Blockly.Python.addDeclaration("thirdparty_nrf24l01_setup_declaration", '' +
  628. 'RF24 radio(7, 5);\n' +
  629. 'const byte address[6] = "' + text_thirdparty_nrf24l01_setup_addr + '";\n' +
  630. '');
  631. Blockly.Python.addSetup("thirdparty_nrf24l01_setup_setup", '' +
  632. 'radio.begin();\n' +
  633. '');
  634. var code = '';
  635. return code;
  636. };
  637. Blockly.Python['thirdparty_nrf24l01_send_data'] = function(block) {
  638. var text_thirdparty_nrf24l01_send_data_data = block.getFieldValue('thirdparty_nrf24l01_send_data_data');
  639. // TODO: Assemble JavaScript into code variable.
  640. Blockly.Python.addSetup("thirdparty_nrf24l01_send_data_setup", '' +
  641. 'radio.openWritingPipe(address);\n' +
  642. 'radio.setPALevel(RF24_PA_MIN);\n' +
  643. 'radio.stopListening();\n' +
  644. '');
  645. var code = '' +
  646. 'const char text[] = "' + text_thirdparty_nrf24l01_send_data_data + '";\n' +
  647. 'radio.write(&text, sizeof(text));\n' +
  648. '';
  649. return code;
  650. };
  651. Blockly.Python['thirdparty_nrf24l01_receive_data'] = function(block) {
  652. // TODO: Assemble JavaScript into code variable.
  653. Blockly.Python.addDeclaration("thirdparty_nrf24l01_receive_data_declare", '' +
  654. 'char _nrfText[32] = "";\n' +
  655. '')
  656. Blockly.Python.addSetup("thirdparty_nrf24l01_receive_data_setup", '' +
  657. 'radio.openReadingPipe(0, address);\n' +
  658. 'radio.setPALevel(RF24_PA_MIN);\n' +
  659. 'radio.startListening();\n' +
  660. '');
  661. Blockly.Python.addFunction("thirdparty_nrf24l01_receive_data_function", '' +
  662. 'void _nrfReceiveDataSet() {\n' +
  663. ' if (radio.available()) {\n' +
  664. ' radio.read(&_nrfText, sizeof(_nrfText));\n' +
  665. ' }\n' +
  666. '}\n' +
  667. '');
  668. var code = '_nrfReceiveDataSet();\n';
  669. return code;
  670. };
  671. Blockly.Python['thirdparty_nrf24l01_data_received'] = function(block) {
  672. // TODO: Assemble JavaScript into code variable.
  673. var code = '_nrfText';
  674. // TODO: Change ORDER_NONE to the correct strength.
  675. return [code, Blockly.Python.ORDER_ATOMIC];
  676. };