extension_module.js 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320
  1. // Serial
  2. Blockly.Blocks['serial_write_data'] = {
  3. init: function () {
  4. let types = getLocalStorage("type")
  5. this.appendDummyInput()
  6. .appendField(new Blockly.FieldImage("blockly/media/uart_serial_send_header.png", 110, 35, { alt: "*", flipRtl: "FALSE" }));
  7. types == 1 ? this.appendDummyInput()
  8. .appendField(Blockly.Msg.serialcomm_write_on)
  9. .appendField(new Blockly.FieldDropdown([
  10. [Blockly.Msg.serialcomm_write_on_ai, "ai"],
  11. [Blockly.Msg.serialcomm_write_on_iot, "iot"]
  12. ]), "type") :
  13. this.appendDummyInput()
  14. .appendField(Blockly.Msg.serialcomm_write_on)
  15. .appendField(new Blockly.FieldDropdown([
  16. [Blockly.Msg.serialcomm_write_on_iot, "iot"],
  17. [Blockly.Msg.serialcomm_write_on_ai, "ai"]
  18. ]), "type");
  19. this.appendDummyInput()
  20. .appendField(Blockly.Msg.serialcomm_write_title_text);
  21. this.appendDummyInput()
  22. .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
  23. .appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps")
  24. .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
  25. /*this.appendValueInput("first_input")
  26. .setCheck(null)
  27. .appendField("Field 0");*/
  28. this.itemCount_ = 1;
  29. this.updateShape_();
  30. this.setMutator(new Blockly.Mutator(['serial_write_data_create_with_item']));
  31. this.setColour("#22b845");
  32. this.setPreviousStatement(true, null);
  33. this.setNextStatement(true, null);
  34. var thisBlock = this;
  35. this.setTooltip(function () {
  36. var mode = thisBlock.getFieldValue('type');
  37. var TOOLTIPS = {
  38. 'iot': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
  39. 'ai': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
  40. };
  41. return TOOLTIPS[mode];
  42. });
  43. this.setHelpUrl("");
  44. },
  45. mutationToDom: function () {
  46. var container = document.createElement('mutation');
  47. container.setAttribute('items', this.itemCount_);
  48. return container;
  49. },
  50. domToMutation: function (xmlElement) {
  51. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  52. this.updateShape_();
  53. },
  54. decompose: function (workspace) {
  55. var containerBlock = workspace.newBlock('serial_write_data_create_with_container');
  56. containerBlock.initSvg();
  57. var connection = containerBlock.getInput('STACK').connection;
  58. for (var i = 0; i < this.itemCount_; i++) {
  59. var itemBlock = workspace.newBlock('serial_write_data_create_with_item');
  60. itemBlock.initSvg();
  61. connection.connect(itemBlock.previousConnection);
  62. connection = itemBlock.nextConnection;
  63. }
  64. return containerBlock;
  65. },
  66. compose: function (containerBlock) {
  67. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  68. // Count number of inputs.
  69. var connections = [];
  70. while (itemBlock) {
  71. connections.push(itemBlock.valueConnection_);
  72. itemBlock = itemBlock.nextConnection &&
  73. itemBlock.nextConnection.targetBlock();
  74. }
  75. // Disconnect any children that don't belong.
  76. for (var i = 0; i < this.itemCount_; i++) {
  77. var connection = this.getInput('ADD' + i).connection.targetConnection;
  78. if (connection && connections.indexOf(connection) == -1) {
  79. connection.disconnect();
  80. }
  81. }
  82. this.itemCount_ = connections.length;
  83. this.updateShape_();
  84. // Reconnect any child blocks.
  85. for (var i = 0; i < this.itemCount_; i++) {
  86. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  87. }
  88. },
  89. saveConnections: function (containerBlock) {
  90. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  91. var i = 0;
  92. while (itemBlock) {
  93. var input = this.getInput('ADD' + i);
  94. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  95. i++;
  96. itemBlock = itemBlock.nextConnection &&
  97. itemBlock.nextConnection.targetBlock();
  98. }
  99. },
  100. updateShape_: function () {
  101. for (var i = 0; i < this.itemCount_; i++) {
  102. if (!this.getInput('ADD' + i)) {
  103. var input = this.appendValueInput('ADD' + i);
  104. input.appendField(Blockly.Msg.serialcomm_write_item_first + i + Blockly.Msg.serialcomm_write_item_last);
  105. //input.appendField("資料 " + (i + 1) + ":");
  106. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  107. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  108. }
  109. }
  110. while (this.getInput('ADD' + i)) {
  111. this.removeInput('ADD' + i);
  112. i++;
  113. }
  114. },
  115. };
  116. Blockly.Blocks['serial_write_data_create_with_container'] = {
  117. init: function () {
  118. this.setColour("#22b845");
  119. this.appendDummyInput()
  120. .appendField(Blockly.Msg.serialcomm_write_item);
  121. this.appendStatementInput('STACK');
  122. this.setTooltip('');
  123. this.contextMenu = false;
  124. }
  125. };
  126. Blockly.Blocks['serial_write_data_create_with_item'] = {
  127. init: function () {
  128. this.setColour("#22b845");
  129. this.appendDummyInput()
  130. .appendField(Blockly.Msg.serialcomm_write_data);
  131. this.setPreviousStatement(true);
  132. this.setNextStatement(true);
  133. this.setTooltip('');
  134. this.contextMenu = false;
  135. }
  136. };
  137. Blockly.Python['serial_write_data'] = function (block) {
  138. var api = block.getFieldValue('t_api');
  139. var number_uart_bps = block.getFieldValue('uart_bps');
  140. var dropdown_type = block.getFieldValue('type');
  141. if (dropdown_type == "ai") {
  142. Blockly.Python.definitions_['import_machine_uart'] = 'import machine, time\n' +
  143. 'from fpioa_manager import fm\n' +
  144. '\n';
  145. Blockly.Python.addVariable('uart_data', 'uart_data = ""', true);
  146. Blockly.Python.definitions_['uart_setup'] = '' +
  147. 'fm.register(13,fm.fpioa.UART2_TX)\n' +
  148. 'fm.register(14,fm.fpioa.UART2_RX)\n' +
  149. 'ai_uart = machine.UART(machine.UART.UART2,' + number_uart_bps + ',bits=8,parity=None,stop=1)\n' +
  150. '';
  151. // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
  152. item_value = '';
  153. var ai_uart_data = "";
  154. for (var n = 0; n < this.itemCount_; n++) {
  155. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  156. ai_uart_data += 'str(' + item_value + ') + \"|\" + ';
  157. }
  158. var ai_uart_data_final = ai_uart_data + "";
  159. var code = '' +
  160. 'uart_data = ' + ai_uart_data_final.slice(0, -8) + '\n' +
  161. 'ai_uart.write(b"SOF|" + str(uart_data) + "|\\r\\n")\n' +
  162. 'time.sleep_ms(20)\n' +
  163. '';
  164. } else if (dropdown_type == "iot") {
  165. Blockly.Python.definitions_['import_machine_uart'] = 'from machine import UART, Pin\n' +
  166. 'import time\n' +
  167. '\n';
  168. Blockly.Python.definitions_['uart_setup'] = '' +
  169. '_iot_uart = UART(2, tx=17, rx=16)\n' +
  170. '_iot_uart.init(115200, bits=8, parity=None, stop=1)\n' +
  171. '';
  172. Blockly.Python.addVariable('_iot_uart_data', '_iot_uart_data = ""', true);
  173. item_value = '';
  174. var iot_uart_data = "";
  175. for (var n = 0; n < this.itemCount_; n++) {
  176. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  177. iot_uart_data += 'str(' + item_value + ') + \"|\" + ';
  178. }
  179. var iot_uart_data_final = iot_uart_data + "";
  180. var code = '' +
  181. '_iot_uart_data = ' + iot_uart_data_final.slice(0, -8) + '\n' +
  182. '_iot_uart.write("SOF|" + str(_iot_uart_data) + "|\\r\\n")\n' +
  183. 'time.sleep_ms(20)\n' +
  184. '';
  185. }
  186. return code;
  187. };
  188. Blockly.Blocks['serial_send_data_to_microbit'] = {
  189. init: function () {
  190. let types = getLocalStorage("type")
  191. this.appendDummyInput()
  192. .appendField(new Blockly.FieldImage("blockly/media/microbit_data_send_header.png", 90, 70, { alt: "*", flipRtl: "FALSE" }));
  193. types == 1 ? this.appendValueInput("NAME")
  194. .appendField(Blockly.Msg.serial_send_data_on)
  195. .setCheck(null)
  196. .appendField(new Blockly.FieldDropdown([
  197. ["A.I.", "ai"],
  198. ["IoT", "iot"]
  199. ]), "module_type_microbit")
  200. .appendField(Blockly.Msg.serial_send_data_on_send_data) :
  201. this.appendValueInput("NAME")
  202. .appendField(Blockly.Msg.serial_send_data_on)
  203. .setCheck(null)
  204. .appendField(new Blockly.FieldDropdown([
  205. ["IoT", "iot"],
  206. ["A.I.", "ai"]
  207. ]), "module_type_microbit")
  208. .appendField(Blockly.Msg.serial_send_data_on_send_data);
  209. this.setInputsInline(false);
  210. this.setColour("#22b845");
  211. this.setPreviousStatement(true, null);
  212. this.setNextStatement(true, null);
  213. this.setHelpUrl("");
  214. var thisBlock = this;
  215. this.setTooltip(function () {
  216. var mode = thisBlock.getFieldValue('module_type_microbit');
  217. var TOOLTIPS = {
  218. 'iot': Blockly.Msg.Serial_Send_Data_To_Microbit_TOOLTIP.replace('%1', "IoT"),
  219. 'ai': Blockly.Msg.Serial_Send_Data_To_Microbit_TOOLTIP.replace('%1', "A.I.")
  220. };
  221. return TOOLTIPS[mode];
  222. });
  223. }
  224. };
  225. Blockly.Python['serial_send_data_to_microbit'] = function (block) {
  226. var dropdown_module_type = block.getFieldValue('module_type_microbit');
  227. var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC) || "";
  228. // TODO: Assemble Python into code variable.
  229. var code = '';
  230. if (dropdown_module_type == 'iot') {
  231. Blockly.Python.definitions_['import_microbit_iot_uart'] = "from machine import UART, Pin \nimport time\n";
  232. let iot = `
  233. _iot_microbit_uart = UART(2, tx=17, rx=16)
  234. _iot_microbit_uart.init(115200, bits=8, parity=None, stop=1)`;
  235. Blockly.Python.addVariable('_iot_microbit_uart', iot, true);
  236. code = '\n' +
  237. "_iot_microbit_uart_data =" + value_name + "\n" +
  238. '_iot_microbit_uart.write(str(_iot_uart_data))\n' +
  239. "time.sleep_ms(20)\n" +
  240. '';
  241. } else {
  242. Blockly.Python.definitions_['import_microbit_ai_uart'] = "import machine, time \nfrom fpioa_manager import fm\n";
  243. let ai = `
  244. fm.register(13,fm.fpioa.UART2_TX)
  245. fm.register(14,fm.fpioa.UART2_RX)
  246. ai_microbit_uart = machine.UART(machine.UART.UART2,115200,bits=8,parity=None,stop=1)`;
  247. Blockly.Python.addVariable('ai_microbit_uart', ai, true);
  248. code = '\n' +
  249. "uart_microbit_data =" + value_name + "\n" +
  250. 'ai_microbit_uart.write(b""+str(uart_microbit_data)+"")\n' +
  251. "time.sleep_ms(20)\n" +
  252. '';
  253. }
  254. return code;
  255. };
  256. /*
  257. ____
  258. / ___| ___ _ ____ _____
  259. \___ \ / _ \ '__\ \ / / _ \
  260. ___) | __/ | \ V / (_) |
  261. |____/ \___|_| \_/ \___/
  262. */
  263. var SERVO_BLOCK_COLOR = "#5bb2d6";
  264. var ai_servo_timer = [
  265. "Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM",
  266. "Timer.TIMER0, Timer.CHANNEL1, mode=Timer.MODE_PWM",
  267. "Timer.TIMER0, Timer.CHANNEL2, mode=Timer.MODE_PWM",
  268. "Timer.TIMER0, Timer.CHANNEL3, mode=Timer.MODE_PWM",
  269. "Timer.TIMER1, Timer.CHANNEL0, mode=Timer.MODE_PWM",
  270. "Timer.TIMER1, Timer.CHANNEL1, mode=Timer.MODE_PWM",
  271. "Timer.TIMER1, Timer.CHANNEL2, mode=Timer.MODE_PWM",
  272. "Timer.TIMER1, Timer.CHANNEL3, mode=Timer.MODE_PWM",
  273. "Timer.TIMER2, Timer.CHANNEL0, mode=Timer.MODE_PWM",
  274. "Timer.TIMER2, Timer.CHANNEL1, mode=Timer.MODE_PWM",
  275. "Timer.TIMER2, Timer.CHANNEL2, mode=Timer.MODE_PWM",
  276. "Timer.TIMER2, Timer.CHANNEL3, mode=Timer.MODE_PWM",
  277. "Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM"
  278. // "Timer.TIMER0, Timer.CHANNEL1, mode=Timer.MODE_PWM"
  279. ];
  280. var ai_servo_pin = [
  281. // '0',
  282. '1', '2', '3', '13', '14', '15', '17', '21', '22', '23', '24', '29','30']
  283. Blockly.Blocks['extension_servo_setup_on_ai'] = {
  284. init: function () {
  285. this.appendDummyInput()
  286. .appendField(new Blockly.FieldImage("blockly/media/esp32_servo_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  287. this.appendDummyInput()
  288. .appendField(Blockly.Msg.servo_setup_ai);
  289. this.setInputsInline(false);
  290. this.setPreviousStatement(true, null);
  291. this.setNextStatement(true, null);
  292. this.setColour(servoCloor);
  293. this.setTooltip(Blockly.Msg.extension_servo_setup_on_ai_TOOLTIP);
  294. this.setHelpUrl("");
  295. }
  296. };
  297. Blockly.Python['extension_servo_setup_on_ai'] = function (block) {
  298. Blockly.Python.definitions_.extension_servo_import = "from machine import Timer,PWM";
  299. Blockly.Python.addSetup("extension_servo_setup", '' +
  300. 'def _ai_servo_write(servo,angle):\n' +
  301. '\tangle = angle - 90\n' +
  302. '\tleftSpan = 180 - 0\n' +
  303. '\trightSpan = 175 - 5\n' +
  304. '\tvalueScaled = float(angle - 0) / float(leftSpan)\n' +
  305. '\tservo.duty(((10 + (valueScaled * rightSpan))+90)/180*10+2.5)\n' +
  306. '');
  307. var code = '\n';
  308. return code;
  309. };
  310. Blockly.Blocks['extension_servo_write_on_ai'] = {
  311. init: function () {
  312. this.appendDummyInput()
  313. .appendField(Blockly.Msg.servo_set_gpio_ai)
  314. .appendField(new Blockly.FieldDropdown([
  315. // ["0", "0"],
  316. // ["1", "1"],
  317. // ["2", "2"],
  318. // ["3", "3"],
  319. // ["13", "4"],
  320. // ["14", "5"],
  321. // ["15", "6"],
  322. // ["17", "7"],
  323. // ["21", "8"],
  324. // ["22", "9"],
  325. // ["23", "10"],
  326. // ["24", "11"],
  327. // ["29", "12"],
  328. // ["30", "13"]
  329. ["1", "0"],
  330. ["2", "1"],
  331. ["3", "2"],
  332. ["13", "3"],
  333. ["14", "4"],
  334. ["15", "5"],
  335. ["17", "6"],
  336. ["21", "7"],
  337. ["22", "8"],
  338. ["23", "9"],
  339. ["24", "10"],
  340. ["29", "11"],
  341. ["30", "12"]
  342. ]), "gpio");
  343. this.appendValueInput("degree")
  344. .setCheck(null)
  345. .appendField(Blockly.Msg.servo_rotate_to_ai);
  346. this.appendDummyInput()
  347. .appendField(Blockly.Msg.servo_degree_ai);
  348. this.setInputsInline(true);
  349. this.setPreviousStatement(true, null);
  350. this.setNextStatement(true, null);
  351. this.setColour(servoCloor);
  352. this.setTooltip(Blockly.Msg.extension_servo_write_on_ai_TOOLTIP);
  353. this.setHelpUrl("");
  354. }
  355. };
  356. Blockly.Python['extension_servo_write_on_ai'] = function (block) {
  357. var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC);
  358. var value_gpio = block.getFieldValue('gpio');
  359. Blockly.Python.addSetup("extension_servo_write_ai_" + value_gpio, '' +
  360. '_servo_tim1 = Timer(' + ai_servo_timer[value_gpio] + ')\n' +
  361. '_servo_gpio' + ai_servo_pin[value_gpio] + ' = PWM(_servo_tim1, freq=50, duty=0, pin=' + ai_servo_pin[value_gpio] + ')\n' +
  362. '');
  363. var real_servo_degree = value_degree;
  364. var code = '' +
  365. '_ai_servo_write(_servo_gpio' + ai_servo_pin[value_gpio] + ',' + real_servo_degree + ')\n' +
  366. '';
  367. return code;
  368. };
  369. let servoCloor = "#386dc8"
  370. Blockly.Blocks['esp32_main_controller_servo_setup'] = {
  371. init: function () {
  372. this.appendDummyInput()
  373. .appendField(new Blockly.FieldImage("blockly/media/esp32_servo_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  374. this.appendDummyInput()
  375. .appendField(Blockly.Msg.servo_setup);
  376. this.setPreviousStatement(true, null);
  377. this.setNextStatement(true, null);
  378. this.setColour(servoCloor);
  379. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Servo_Setup_TOOLTIP);
  380. this.setHelpUrl("");
  381. }
  382. };
  383. Blockly.Python['esp32_main_controller_servo_setup'] = function (block) {
  384. // TODO: Assemble Python into code variable.
  385. Blockly.Python.definitions_['definition_servo_setup'] = '' +
  386. 'from machine import Pin, PWM\n' +
  387. '';
  388. var code = '\n';
  389. return code;
  390. };
  391. Blockly.Blocks['esp32_main_controller_servo_set'] = {
  392. init: function () {
  393. this.appendDummyInput()
  394. .appendField(Blockly.Msg.servo_set_gpio);
  395. this.appendValueInput("pin_select")
  396. .setCheck(null);
  397. this.appendDummyInput()
  398. .appendField(Blockly.Msg.servo_rotate_to);
  399. this.appendValueInput("degree")
  400. .setCheck(null);
  401. this.appendDummyInput()
  402. .appendField(Blockly.Msg.servo_degree);
  403. this.setPreviousStatement(true, null);
  404. this.setNextStatement(true, null);
  405. this.setColour(servoCloor);
  406. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Servo_Set_TOOLTIP);
  407. this.setHelpUrl("");
  408. }
  409. };
  410. Blockly.Python['esp32_main_controller_servo_set'] = function (block) {
  411. var value_pin_select = Blockly.Python.valueToCode(block, 'pin_select', Blockly.Python.ORDER_ATOMIC);
  412. var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC);
  413. // TODO: Assemble Python into code variable.
  414. Blockly.Python.addVariable('S' + value_pin_select, 'S' + value_pin_select + ' = PWM(Pin(' + value_pin_select + '), freq=50, duty=0)', true);
  415. Blockly.Python.definitions_['definition_servo_set'] = '' +
  416. 'def Servo(servo,angle):\n' +
  417. ' angle = angle - 90\n' +
  418. ' servo.duty(int(((angle+90)*2/180+0.5)/20*1023))\n' +
  419. '';
  420. var code = '' +
  421. 'Servo(S' + value_pin_select + ',' + value_degree + ')\n' +
  422. '';
  423. return code;
  424. };
  425. /*
  426. __ __ _
  427. | \/ | ___ | |_ ___ _ __
  428. | |\/| |/ _ \| __/ _ \| '__|
  429. | | | | (_) | || (_) | |
  430. |_| |_|\___/ \__\___/|_|
  431. */
  432. var MOTOR_BLOCK_COLOR = "#0000FF";
  433. Blockly.Blocks['esp32_motor_setup'] = {
  434. init: function () {
  435. this.appendDummyInput()
  436. .appendField(new Blockly.FieldImage("blockly/media/motor_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  437. this.appendDummyInput()
  438. .appendField(Blockly.Msg.x_motor_set_motor)
  439. this.setInputsInline(false);
  440. this.setPreviousStatement(true, null);
  441. this.setNextStatement(true, null);
  442. this.setColour(MOTOR_BLOCK_COLOR);
  443. this.setTooltip(Blockly.Msg.Esp32_Motor_Setup_TOOLTIP);
  444. this.setHelpUrl("");
  445. }
  446. };
  447. Blockly.Python['esp32_motor_setup'] = function (block) {
  448. // TODO: Assemble Python into code variable.
  449. Blockly.Python.definitions_['esp32_controller_set_motor'] = '' +
  450. 'import pca9685, motor\n' +
  451. '\n' +
  452. '_iot_motor_i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))\n' +
  453. '_iot_motor = motor.DCMotors(_iot_motor_i2c)';
  454. var code = '';
  455. return code;
  456. };
  457. Blockly.Blocks['esp32_motor_run'] = {
  458. init: function () {
  459. this.appendDummyInput()
  460. .appendField(Blockly.Msg.x_motor_set_motor_turn)
  461. .appendField(new Blockly.FieldDropdown([
  462. ["A", "0"],
  463. ["B", "1"],
  464. ["C", "3"],
  465. ["D", "2"]
  466. ]), "motor_type")
  467. .appendField(Blockly.Msg.x_motor_set_motor_speed);
  468. this.appendValueInput("speed")
  469. .setCheck(null);
  470. this.appendDummyInput()
  471. .appendField(Blockly.Msg.x_motor_exec)
  472. .appendField(new Blockly.FieldDropdown([
  473. [Blockly.Msg.x_motor_cw, "pos"],
  474. [Blockly.Msg.x_motor_acw, "neg"]
  475. ]), "direction")
  476. .appendField(Blockly.Msg.x_motor_set_motor_turns);
  477. this.setInputsInline(true);
  478. this.setPreviousStatement(true, null);
  479. this.setNextStatement(true, null);
  480. this.setColour(MOTOR_BLOCK_COLOR);
  481. this.setTooltip(Blockly.Msg.Esp32_Motor_Run_TOOLTIP);
  482. this.setHelpUrl("");
  483. }
  484. };
  485. Blockly.Python['esp32_motor_run'] = function (block) {
  486. var dropdown_motor_type = block.getFieldValue('motor_type');
  487. var value_speed = Blockly.Python.valueToCode(block, 'speed', Blockly.Python.ORDER_ATOMIC);
  488. var dropdown_direction = block.getFieldValue('direction');
  489. var code;
  490. if (dropdown_direction == "pos") {
  491. code = '_iot_motor.speed(' + dropdown_motor_type + ', (' + value_speed + '*16))\n';
  492. } else if (dropdown_direction == "neg") {
  493. code = '_iot_motor.speed(' + dropdown_motor_type + ', -(' + value_speed + '*16))\n';
  494. }
  495. return code;
  496. };
  497. Blockly.Blocks['ai_motor_setup'] = {
  498. init: function () {
  499. this.appendDummyInput()
  500. .appendField(new Blockly.FieldImage("blockly/media/motor_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  501. this.appendDummyInput()
  502. .appendField(Blockly.Msg.x_motor_set_motor_ai)
  503. this.setInputsInline(false);
  504. this.setPreviousStatement(true, null);
  505. this.setNextStatement(true, null);
  506. this.setColour(MOTOR_BLOCK_COLOR);
  507. this.setTooltip(Blockly.Msg.ai_motor_setup_TOOLTIP);
  508. this.setHelpUrl("");
  509. }
  510. };
  511. Blockly.Python['ai_motor_setup'] = function (block) {
  512. // TODO: Assemble Python into code variable.
  513. Blockly.Python.definitions_['ai_controller_set_motor'] = '' +
  514. 'import pca9685, motor\n' +
  515. 'from machine import I2C\n' +
  516. '\n' +
  517. '_ai_motor_scl = 30\n' +
  518. '_ai_motor_sda = 29\n' +
  519. '\n' +
  520. '_ai_motor_i2c = I2C(I2C.I2C0, freq=100000, scl=_ai_motor_scl, sda=_ai_motor_sda)\n' +
  521. '_ai_motor = motor.DCMotors(_ai_motor_i2c)\n' +
  522. '';
  523. var code = '';
  524. return code;
  525. };
  526. Blockly.Blocks['ai_motor_run'] = {
  527. init: function () {
  528. this.appendDummyInput()
  529. .appendField(Blockly.Msg.x_motor_set_motor_turn_ai)
  530. .appendField(new Blockly.FieldDropdown([
  531. ["A", "0"],
  532. ["B", "1"],
  533. ["C", "3"],
  534. ["D", "2"]
  535. ]), "motor_type")
  536. .appendField(Blockly.Msg.x_motor_set_motor_speed_ai);
  537. this.appendValueInput("speed")
  538. .setCheck(null);
  539. this.appendDummyInput()
  540. .appendField(Blockly.Msg.x_motor_exec_ai)
  541. .appendField(new Blockly.FieldDropdown([
  542. [Blockly.Msg.x_motor_cw_ai, "pos"],
  543. [Blockly.Msg.x_motor_acw_ai, "neg"]
  544. ]), "direction")
  545. .appendField(Blockly.Msg.x_motor_set_motor_turns_ai);
  546. this.setInputsInline(true);
  547. this.setPreviousStatement(true, null);
  548. this.setNextStatement(true, null);
  549. this.setColour(MOTOR_BLOCK_COLOR);
  550. this.setTooltip(Blockly.Msg.ai_motor_run_TOOLTIP);
  551. this.setHelpUrl("");
  552. }
  553. };
  554. Blockly.Python['ai_motor_run'] = function (block) {
  555. var dropdown_motor_type = block.getFieldValue('motor_type');
  556. var value_speed = Blockly.Python.valueToCode(block, 'speed', Blockly.Python.ORDER_ATOMIC);
  557. var dropdown_direction = block.getFieldValue('direction');
  558. if (dropdown_direction == "pos") {
  559. var code = '_ai_motor.speed(' + dropdown_motor_type + ', (' + value_speed + '*16))\n';
  560. } else if (dropdown_direction == "neg") {
  561. var code = '_ai_motor.speed(' + dropdown_motor_type + ', -(' + value_speed + '*16))\n';
  562. }
  563. return code;
  564. };
  565. /*
  566. _ _____ ____ __ __ _ _
  567. | | | ____| _ \ | \/ | __ _| |_ _ __(_)_ __
  568. | | | _| | | | | | |\/| |/ _` | __| '__| \ \/ /
  569. | |___| |___| |_| | | | | | (_| | |_| | | |> <
  570. |_____|_____|____/ |_| |_|\__,_|\__|_| |_/_/\_\
  571. */
  572. let AILEDCOLOR = "#e8795b"
  573. Blockly.Blocks['ai_led_matrix_setup'] = {
  574. init: function () {
  575. this.appendDummyInput()
  576. .appendField(new Blockly.FieldImage("blockly/media/led_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  577. this.appendDummyInput()
  578. .appendField(Blockly.Msg.x_led_matrix_setup_ai);
  579. this.appendDummyInput()
  580. .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_1)
  581. .appendField(new Blockly.FieldNumber(50, 0, 255, 1), "brightness")
  582. .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_range);
  583. this.setInputsInline(false);
  584. this.setPreviousStatement(true, null);
  585. this.setNextStatement(true, null);
  586. this.setColour(AILEDCOLOR);
  587. this.setTooltip(Blockly.Msg.ai_led_matrix_setup_TOOLTIP);
  588. this.setHelpUrl("");
  589. }
  590. };
  591. Blockly.Python['ai_led_matrix_setup'] = function (block) {
  592. var number_brightness = block.getFieldValue('brightness');
  593. Blockly.Python.definitions_['ai_controller_led_setup'] = '' +
  594. 'from modules import ws2812\n' +
  595. '\n' +
  596. '_ai_led_matrix_total = 36\n' +
  597. '_ai_led_matrix = ws2812(24,_ai_led_matrix_total)\n' +
  598. '_ai_brightness = ' + number_brightness + '\n' +
  599. '\n' +
  600. 'def rgba_to_rgb_conversion(red, green, blue):\n' +
  601. ' if _ai_brightness <= 255:\n' +
  602. ' alpha = _ai_brightness / 255\n' +
  603. ' elif _ai_brightness > 255:\n' +
  604. ' alpha = 255 / 255\n' +
  605. ' elif _ai_brightness < 0:\n' +
  606. ' alpha = 0 / 255\n' +
  607. ' final_red = int((1 - alpha) * 0 + alpha * red)\n' +
  608. ' final_green = int((1 - alpha) * 0 + alpha * green)\n' +
  609. ' final_blue = int((1 - alpha) * 0 + alpha * blue)\n' +
  610. ' final = (final_red, final_green, final_blue)\n' +
  611. ' return final\n' +
  612. '';
  613. // TODO: Assemble Python into code variable.
  614. var code = '';
  615. return code;
  616. };
  617. Blockly.Blocks['ai_led_matrix_set_bright'] = {
  618. init: function () {
  619. this.appendValueInput("brightness")
  620. .setCheck(null)
  621. .appendField(Blockly.Msg.x_led_matrix_width_ai_set_brightness_1);
  622. this.appendDummyInput()
  623. .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_range);
  624. this.setInputsInline(true);
  625. this.setPreviousStatement(true, null);
  626. this.setNextStatement(true, null);
  627. this.setColour(AILEDCOLOR);
  628. this.setTooltip(Blockly.Msg.ai_led_matrix_set_bright_TOOLTIP);
  629. this.setHelpUrl("");
  630. }
  631. };
  632. Blockly.Python['ai_led_matrix_set_bright'] = function (block) {
  633. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  634. // TODO: Assemble Python into code variable.
  635. var code = '_ai_brightness = ' + value_brightness + '\n';
  636. return code;
  637. };
  638. Blockly.Blocks['ai_led_matrix_xy'] = {
  639. init: function () {
  640. this.appendValueInput("x")
  641. .setCheck(null)
  642. .appendField(Blockly.Msg.x_led_matrix_width_ai_x);
  643. this.appendValueInput("y")
  644. .setCheck(null)
  645. .appendField(Blockly.Msg.x_led_matrix_width_ai_y);
  646. this.setInputsInline(true);
  647. this.setOutput(true, null);
  648. this.setColour(AILEDCOLOR);
  649. this.setTooltip(Blockly.Msg.ai_led_matrix_xy_TOOLTIP);
  650. this.setHelpUrl("");
  651. }
  652. };
  653. Blockly.Python['ai_led_matrix_xy'] = function (block) {
  654. var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
  655. var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC);
  656. // TODO: Assemble Python into code variable.
  657. var code = value_x + ',' + value_y;
  658. // TODO: Change ORDER_NONE to the correct strength.
  659. return [code, Blockly.Python.ORDER_NONE];
  660. };
  661. Blockly.Blocks['ai_led_matrix_wh'] = {
  662. init: function () {
  663. this.appendValueInput("w")
  664. .setCheck(null)
  665. .appendField(Blockly.Msg.x_led_matrix_width_ai);
  666. this.appendValueInput("h")
  667. .setCheck(null)
  668. .appendField(Blockly.Msg.x_led_matrix_height_ai);
  669. this.setInputsInline(true);
  670. this.setOutput(true, null);
  671. this.setColour(AILEDCOLOR);
  672. this.setTooltip(Blockly.Msg.ai_led_matrix_wh_TOOLTIP);
  673. this.setHelpUrl("");
  674. }
  675. };
  676. Blockly.Python['ai_led_matrix_wh'] = function (block) {
  677. var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
  678. var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
  679. // TODO: Assemble Python into code variable.
  680. var code = value_w + ',' + value_h;
  681. // TODO: Change ORDER_NONE to the correct strength.
  682. return [code, Blockly.Python.ORDER_NONE];
  683. };
  684. Blockly.Blocks['ai_led_matrix_color_picker'] = {
  685. init: function () {
  686. this.appendDummyInput()
  687. .appendField(Blockly.Msg.x_led_matrix_color_ai)
  688. .appendField(new Blockly.FieldColour("#ff0000"), "color");
  689. this.setInputsInline(true);
  690. this.setOutput(true, null);
  691. this.setColour(AILEDCOLOR);
  692. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Color_Picker_TOOLTIP);
  693. this.setHelpUrl("");
  694. }
  695. };
  696. Blockly.Python['ai_led_matrix_color_picker'] = function (block) {
  697. var color = block.getFieldValue('color');
  698. var d = 0,
  699. e = 0,
  700. f = 0;
  701. try {
  702. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  703. e = parseInt(color.substring(3, 5), 16),
  704. f = parseInt(color.substring(5, 7), 16))
  705. } catch (g) { }
  706. // TODO: Assemble Python into code variable.
  707. var code = d + ',' + e + ',' + f;
  708. // TODO: Change ORDER_NONE to the correct strength.
  709. return [code, Blockly.Python.ORDER_NONE];
  710. };
  711. Blockly.Blocks['ai_led_matrix_draw_pixel'] = {
  712. init: function () {
  713. this.appendDummyInput()
  714. .appendField(new Blockly.FieldImage("blockly/media/led_draw-pixel.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
  715. this.appendDummyInput()
  716. .appendField(Blockly.Msg.x_led_matrix_draw_title_ai);
  717. this.appendDummyInput()
  718. .appendField(Blockly.Msg.x_led_matrix_draw_pixel_ai);
  719. this.appendValueInput("color")
  720. .setCheck(null)
  721. .appendField(Blockly.Msg.x_led_matrix_parameter_color_ai);
  722. this.appendValueInput("coordinate")
  723. .setCheck(null)
  724. .appendField(Blockly.Msg.x_led_matrix_parameter_coord_ai);
  725. this.setInputsInline(false);
  726. this.setPreviousStatement(true, null);
  727. this.setNextStatement(true, null);
  728. this.setColour(AILEDCOLOR);
  729. this.setTooltip(Blockly.Msg.ai_led_matrix_draw_pixel_TOOLTIP);
  730. this.setHelpUrl("");
  731. }
  732. };
  733. Blockly.Python['ai_led_matrix_draw_pixel'] = function (block) {
  734. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  735. var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
  736. // TODO: Assemble Python into code variable.
  737. var comma_location = value_coordinate.indexOf(",");
  738. var last_bracket_location = value_coordinate.length;
  739. var cord_x = value_coordinate.substr(1, comma_location - 1);
  740. var cord_y_pre = value_coordinate.substr(comma_location + 1, last_bracket_location - 1);
  741. var cord_y = cord_y_pre.substring(0, cord_y_pre.length - 1)
  742. // console.log(value_coordinate + ": " + cord_x + ", " + cord_y);
  743. var code = '_a = _ai_led_matrix.set_led((((' + cord_y + '*6)//6)-1)*6+(' + cord_x + '-1),rgba_to_rgb_conversion' + value_color + ')\n';
  744. return code;
  745. };
  746. Blockly.Blocks['ai_led_matrix_draw_rectangle'] = {
  747. init: function () {
  748. this.appendDummyInput()
  749. .appendField(new Blockly.FieldImage("blockly/media/led_draw-rect.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
  750. this.appendDummyInput()
  751. .appendField(Blockly.Msg.x_led_matrix_draw_title_ai);
  752. this.appendDummyInput()
  753. .appendField(Blockly.Msg.x_led_matrix_draw_rect_ai);
  754. this.appendValueInput("color")
  755. .setCheck(null)
  756. .appendField(Blockly.Msg.x_led_matrix_parameter_color_ai);
  757. this.appendValueInput("coordinate")
  758. .setCheck(null)
  759. .appendField(Blockly.Msg.x_led_matrix_parameter_coord_ai);
  760. this.appendValueInput("size")
  761. .setCheck(null)
  762. .appendField(Blockly.Msg.x_led_matrix_parameter_size_ai);
  763. this.setInputsInline(false);
  764. this.setPreviousStatement(true, null);
  765. this.setNextStatement(true, null);
  766. this.setColour(AILEDCOLOR);
  767. this.setTooltip(Blockly.Msg.ai_led_matrix_draw_rectangle_TOOLTIP);
  768. this.setHelpUrl("");
  769. }
  770. };
  771. Blockly.Python['ai_led_matrix_draw_rectangle'] = function (block) {
  772. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  773. var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
  774. var value_size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);
  775. Blockly.Python.codeFunctions_['ai_led_matrix_draw_rectangle_setup'] = '' +
  776. 'def _ai_led_draw_rectangle(x, y, w, h, rgb):\n' +
  777. ' for i in range(x, x+ w, 1):\n' +
  778. ' for j in range(y, y+h, 1):\n' +
  779. ' _a = _ai_led_matrix.set_led((((i*6)//6)-1)*6+(j-1),(rgb))\n' +
  780. '\n';
  781. // TODO: Assemble Python into code variable.
  782. var comma_location = value_coordinate.indexOf(",");
  783. var last_bracket_location = value_coordinate.length;
  784. var cord_x = value_coordinate.substr(1, comma_location - 1);
  785. var cord_y_pre = value_coordinate.substr(comma_location + 1, last_bracket_location - 1);
  786. var cord_y = cord_y_pre.substring(0, cord_y_pre.length - 1)
  787. // console.log(value_coordinate + ": " + cord_x + ", " + cord_y);
  788. var comma_location_size = value_size.indexOf(",");
  789. var last_bracket_location_size = value_size.length;
  790. var size_width = value_size.substr(1, comma_location_size - 1);
  791. var size_height_pre = value_size.substr(comma_location_size + 1, last_bracket_location_size - 1);
  792. var size_height = size_height_pre.substring(0, size_height_pre.length - 1)
  793. // console.log("Size string length: " + last_bracket_location_size);
  794. // console.log("Size is " + value_size + ": " + size_width + ", " + size_height);
  795. var code = '_ai_led_draw_rectangle(' + cord_y + ',' + cord_x + ',' + size_width + ',' + size_height + ',rgba_to_rgb_conversion' + value_color + ')\n';
  796. return code;
  797. };
  798. Blockly.Blocks['ai_led_matrix_show_above'] = {
  799. init: function () {
  800. this.appendDummyInput()
  801. .appendField(Blockly.Msg.x_led_matrix_show_above_ai);
  802. this.setInputsInline(false);
  803. this.setPreviousStatement(true, null);
  804. this.setNextStatement(true, null);
  805. this.setColour(AILEDCOLOR);
  806. this.setTooltip(Blockly.Msg.ai_led_matrix_show_above_TOOLTIP);
  807. this.setHelpUrl("");
  808. }
  809. };
  810. Blockly.Python['ai_led_matrix_show_above'] = function (block) {
  811. // TODO: Assemble Python into code variable.
  812. var code = '_ai_led_matrix.display()\n';
  813. return code;
  814. };
  815. Blockly.Blocks['ai_led_matrix_clear_screen'] = {
  816. init: function () {
  817. this.appendDummyInput()
  818. .appendField(Blockly.Msg.x_led_matrix_clear_all_ai);
  819. this.setInputsInline(false);
  820. this.setPreviousStatement(true, null);
  821. this.setNextStatement(true, null);
  822. this.setColour(AILEDCOLOR);
  823. this.setTooltip(Blockly.Msg.ai_led_matrix_clear_screen_TOOLTIP);
  824. this.setHelpUrl("");
  825. }
  826. };
  827. Blockly.Python['ai_led_matrix_clear_screen'] = function (block) {
  828. // TODO: Assemble Python into code variable.
  829. Blockly.Python.definitions_['ai_led_matrix_clear_screen_setup'] = '' +
  830. 'def _ai_led_draw_rectangle(x, y, w, h, rgb):\n' +
  831. ' for i in range(x, x+ w, 1):\n' +
  832. ' for j in range(y, y+h, 1):\n' +
  833. ' _a = _ai_led_matrix.set_led((((i*6)//6)-1)*6+(j-1),(rgb))\n' +
  834. '';
  835. var code = '' +
  836. '_ai_led_draw_rectangle(1, 1, 6, 6, rgba_to_rgb_conversion(0, 0, 0))\n' +
  837. '_ai_led_matrix.display()\n' +
  838. '';
  839. return code;
  840. };
  841. Blockly.Blocks['ai_led_matrix_rgb_value_input'] = {
  842. init: function () {
  843. this.appendDummyInput()
  844. .appendField(Blockly.Msg.image_process_rgb_r);
  845. this.appendValueInput("rgb_value_r")
  846. .setCheck(null)
  847. .appendField("");
  848. this.appendDummyInput()
  849. .appendField(Blockly.Msg.image_process_rgb_g);
  850. this.appendValueInput("rgb_value_g")
  851. .setCheck(null)
  852. .appendField("");
  853. this.appendDummyInput()
  854. .appendField(Blockly.Msg.image_process_rgb_b);
  855. this.appendValueInput("rgb_value_b")
  856. .setCheck(null)
  857. .appendField("");
  858. this.setInputsInline(true);
  859. this.setOutput(true, null);
  860. this.setColour(AILEDCOLOR);
  861. this.setTooltip(Blockly.Msg.ai_led_matrix_rgb_value_input_TOOLTIP);
  862. this.setHelpUrl("");
  863. }
  864. };
  865. Blockly.Python['ai_led_matrix_rgb_value_input'] = function (block) {
  866. var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
  867. var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
  868. var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
  869. // TODO: Assemble Python into code variable.
  870. var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
  871. // TODO: Change ORDER_NONE to the correct strength.
  872. return [code, Blockly.Python.ORDER_NONE];
  873. };
  874. Blockly.Blocks['iot_led_matrix_rgb_value_input'] = {
  875. init: function () {
  876. this.appendDummyInput()
  877. .appendField(Blockly.Msg.image_process_rgb_r);
  878. this.appendValueInput("rgb_value_r")
  879. .setCheck(null)
  880. .appendField("");
  881. this.appendDummyInput()
  882. .appendField(Blockly.Msg.image_process_rgb_g);
  883. this.appendValueInput("rgb_value_g")
  884. .setCheck(null)
  885. .appendField("");
  886. this.appendDummyInput()
  887. .appendField(Blockly.Msg.image_process_rgb_b);
  888. this.appendValueInput("rgb_value_b")
  889. .setCheck(null)
  890. .appendField("");
  891. this.setInputsInline(true);
  892. this.setOutput(true, null);
  893. this.setColour(AILEDCOLOR);
  894. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Rgb_Value_Input_TOOLTIP);
  895. this.setHelpUrl("");
  896. }
  897. };
  898. Blockly.Python['iot_led_matrix_rgb_value_input'] = function (block) {
  899. var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
  900. var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
  901. var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
  902. // TODO: Assemble Python into code variable.
  903. var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
  904. // TODO: Change ORDER_NONE to the correct strength.
  905. return [code, Blockly.Python.ORDER_NONE];
  906. };
  907. Blockly.Blocks['iot_led_matrix_setup'] = {
  908. init: function () {
  909. this.appendDummyInput()
  910. .appendField(new Blockly.FieldImage("blockly/media/led_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  911. this.appendDummyInput()
  912. .appendField(Blockly.Msg.x_led_matrix_setup_iot);
  913. this.appendDummyInput()
  914. .appendField(Blockly.Msg.x_led_matrix_setup_iot_io)
  915. .appendField(new Blockly.FieldDropdown([
  916. ["25", "25"],
  917. ["15", "15"]
  918. ]), "io");
  919. this.appendDummyInput()
  920. .appendField(Blockly.Msg.x_led_matrix_setup_iot_set_bright_1)
  921. .appendField(new Blockly.FieldNumber(50, 0, 255, 1), "brightness")
  922. .appendField(Blockly.Msg.x_led_matrix_setup_iot_set_bright_range);
  923. this.setInputsInline(false);
  924. this.setPreviousStatement(true, null);
  925. this.setNextStatement(true, null);
  926. this.setColour(AILEDCOLOR);
  927. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Setup_TOOLTIP);
  928. this.setHelpUrl("");
  929. }
  930. };
  931. Blockly.Python['iot_led_matrix_setup'] = function (block) {
  932. var number_brightness = block.getFieldValue('brightness');
  933. var dropdown_io = block.getFieldValue('io');
  934. Blockly.Python.definitions_['iot_controller_led_setup'] = '' +
  935. 'import machine, neopixel, time\n' +
  936. '_6x6_led_matrix = neopixel.NeoPixel(machine.Pin(' + dropdown_io + '), 36)\n' +
  937. '_iot_brightness = ' + number_brightness + '\n' +
  938. '\n' +
  939. 'def rgba_to_rgb_conversion(red, green, blue):\n' +
  940. ' if _iot_brightness <= 255:\n' +
  941. ' alpha = _iot_brightness / 255\n' +
  942. ' elif _iot_brightness > 255:\n' +
  943. ' alpha = 255 / 255\n' +
  944. ' elif _iot_brightness < 0:\n' +
  945. ' alpha = 0 / 255\n' +
  946. ' final_red = int((1 - alpha) * 0 + alpha * red)\n' +
  947. ' final_green = int((1 - alpha) * 0 + alpha * green)\n' +
  948. ' final_blue = int((1 - alpha) * 0 + alpha * blue)\n' +
  949. ' final = (final_red, final_green, final_blue)\n' +
  950. ' return final\n' +
  951. '\n';
  952. // TODO: Assemble Python into code variable.
  953. var code = '';
  954. return code;
  955. };
  956. Blockly.Blocks['iot_led_matrix_set_bright'] = {
  957. init: function () {
  958. this.appendValueInput("brightness")
  959. .setCheck(null)
  960. .appendField(Blockly.Msg.x_led_matrix_width_ai_set_brightness_1);
  961. this.appendDummyInput()
  962. .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_range);
  963. this.setInputsInline(true);
  964. this.setPreviousStatement(true, null);
  965. this.setNextStatement(true, null);
  966. this.setColour(AILEDCOLOR);
  967. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Set_Bright_TOOLTIP);
  968. this.setHelpUrl("");
  969. }
  970. };
  971. Blockly.Python['iot_led_matrix_set_bright'] = function (block) {
  972. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  973. // TODO: Assemble Python into code variable.
  974. var code = '_iot_brightness = ' + value_brightness + '\n';
  975. return code;
  976. };
  977. Blockly.Blocks['iot_led_matrix_xy'] = {
  978. init: function () {
  979. this.appendValueInput("x")
  980. .setCheck(null)
  981. .appendField(Blockly.Msg.x_led_matrix_width_iot_x);
  982. this.appendValueInput("y")
  983. .setCheck(null)
  984. .appendField(Blockly.Msg.x_led_matrix_width_iot_y);
  985. this.setInputsInline(true);
  986. this.setOutput(true, null);
  987. this.setColour(AILEDCOLOR);
  988. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_xy_TOOLTIP);
  989. this.setHelpUrl("");
  990. }
  991. };
  992. Blockly.Python['iot_led_matrix_xy'] = function (block) {
  993. var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
  994. var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC);
  995. // TODO: Assemble Python into code variable.
  996. var code = value_x + ',' + value_y;
  997. // TODO: Change ORDER_NONE to the correct strength.
  998. return [code, Blockly.Python.ORDER_NONE];
  999. };
  1000. Blockly.Blocks['iot_led_matrix_wh'] = {
  1001. init: function () {
  1002. this.appendValueInput("w")
  1003. .setCheck(null)
  1004. .appendField(Blockly.Msg.x_led_matrix_width_iot);
  1005. this.appendValueInput("h")
  1006. .setCheck(null)
  1007. .appendField(Blockly.Msg.x_led_matrix_height_iot);
  1008. this.setInputsInline(true);
  1009. this.setOutput(true, null);
  1010. this.setColour(AILEDCOLOR);
  1011. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_wh_TOOLTIP);
  1012. this.setHelpUrl("");
  1013. }
  1014. };
  1015. Blockly.Python['iot_led_matrix_wh'] = function (block) {
  1016. var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
  1017. var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
  1018. // TODO: Assemble Python into code variable.
  1019. var code = value_w + ',' + value_h;
  1020. // TODO: Change ORDER_NONE to the correct strength.
  1021. return [code, Blockly.Python.ORDER_NONE];
  1022. };
  1023. Blockly.Blocks['iot_led_matrix_color_picker'] = {
  1024. init: function () {
  1025. this.appendDummyInput()
  1026. .appendField(Blockly.Msg.x_led_matrix_color_iot)
  1027. .appendField(new Blockly.FieldColour("#ff0000"), "color");
  1028. this.setInputsInline(true);
  1029. this.setOutput(true, null);
  1030. this.setColour(AILEDCOLOR);
  1031. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Color_Picker_TOOLTIP);
  1032. this.setHelpUrl("");
  1033. }
  1034. };
  1035. Blockly.Python['iot_led_matrix_color_picker'] = function (block) {
  1036. var color = block.getFieldValue('color');
  1037. var d = 0,
  1038. e = 0,
  1039. f = 0;
  1040. try {
  1041. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1042. e = parseInt(color.substring(3, 5), 16),
  1043. f = parseInt(color.substring(5, 7), 16))
  1044. } catch (g) { }
  1045. // TODO: Assemble Python into code variable.
  1046. var code = d + ',' + e + ',' + f;
  1047. // TODO: Change ORDER_NONE to the correct strength.
  1048. return [code, Blockly.Python.ORDER_NONE];
  1049. };
  1050. Blockly.Blocks['iot_led_matrix_draw_pixel'] = {
  1051. init: function () {
  1052. this.appendDummyInput()
  1053. .appendField(new Blockly.FieldImage("blockly/media/led_draw-pixel.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
  1054. this.appendDummyInput()
  1055. .appendField(Blockly.Msg.x_led_matrix_draw_title_iot);
  1056. this.appendDummyInput()
  1057. .appendField(Blockly.Msg.x_led_matrix_draw_pixel_iot);
  1058. this.appendValueInput("color")
  1059. .setCheck(null)
  1060. .appendField(Blockly.Msg.x_led_matrix_parameter_color_iot);
  1061. this.appendValueInput("coordinate")
  1062. .setCheck(null)
  1063. .appendField(Blockly.Msg.x_led_matrix_parameter_coord_iot);
  1064. this.setInputsInline(false);
  1065. this.setPreviousStatement(true, null);
  1066. this.setNextStatement(true, null);
  1067. this.setColour(AILEDCOLOR);
  1068. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Draw_Pixel_TOOLTIP);
  1069. this.setHelpUrl("");
  1070. }
  1071. };
  1072. Blockly.Python['iot_led_matrix_draw_pixel'] = function (block) {
  1073. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  1074. var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
  1075. // TODO: Assemble Python into code variable.
  1076. // _6x6_led_matrix[(((1*6)//6)-1)*6+(1-1)] = (0, 0, 255)
  1077. var comma_location = value_coordinate.indexOf(",");
  1078. var last_bracket_location = value_coordinate.length;
  1079. var cord_x = value_coordinate.substr(1, comma_location - 1);
  1080. var cord_y_pre = value_coordinate.substr(comma_location + 1, last_bracket_location - 1);
  1081. var cord_y = cord_y_pre.substring(0, cord_y_pre.length - 1)
  1082. // console.log(value_coordinate + ": " + cord_x + ", " + cord_y);
  1083. var code = '_6x6_led_matrix[(((' + cord_y + '*6)//6)-1)*6+(' + cord_x + '-1)] = rgba_to_rgb_conversion' + value_color + '\n';
  1084. return code;
  1085. };
  1086. Blockly.Blocks['iot_led_matrix_draw_rectangle'] = {
  1087. init: function () {
  1088. this.appendDummyInput()
  1089. .appendField(new Blockly.FieldImage("blockly/media/led_draw-rect.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
  1090. this.appendDummyInput()
  1091. .appendField(Blockly.Msg.x_led_matrix_draw_title_iot);
  1092. this.appendDummyInput()
  1093. .appendField(Blockly.Msg.x_led_matrix_draw_rect_ai);
  1094. this.appendValueInput("color")
  1095. .setCheck(null)
  1096. .appendField(Blockly.Msg.x_led_matrix_parameter_color_ai);
  1097. this.appendValueInput("coordinate")
  1098. .setCheck(null)
  1099. .appendField(Blockly.Msg.x_led_matrix_parameter_coord_ai);
  1100. this.appendValueInput("size")
  1101. .setCheck(null)
  1102. .appendField(Blockly.Msg.x_led_matrix_parameter_size_ai);
  1103. this.setInputsInline(false);
  1104. this.setPreviousStatement(true, null);
  1105. this.setNextStatement(true, null);
  1106. this.setColour(AILEDCOLOR);
  1107. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Draw_Rectangle_TOOLTIP);
  1108. this.setHelpUrl("");
  1109. }
  1110. };
  1111. Blockly.Python['iot_led_matrix_draw_rectangle'] = function (block) {
  1112. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  1113. var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
  1114. var value_size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);
  1115. Blockly.Python.definitions_['iot_led_matrix_draw_rectangle_setup'] = '' +
  1116. 'def _iot_led_draw_rectangle(x, y, w, h, rgb):\n' +
  1117. ' for i in range(x, x+ w, 1):\n' +
  1118. ' for j in range(y, y+h, 1):\n' +
  1119. ' _6x6_led_matrix[(((j*6)//6)-1)*6+(i-1)] = (rgb)' +
  1120. '\n';
  1121. // TODO: Assemble Python into code variable.
  1122. var comma_location = value_coordinate.indexOf(",");
  1123. var last_bracket_location = value_coordinate.length;
  1124. var cord_x = value_coordinate.substr(1, comma_location - 1);
  1125. var cord_y_pre = value_coordinate.substr(comma_location + 1, last_bracket_location - 1);
  1126. var cord_y = cord_y_pre.substring(0, cord_y_pre.length - 1)
  1127. // console.log(value_coordinate + ": " + cord_x + ", " + cord_y);
  1128. var comma_location_size = value_size.indexOf(",");
  1129. var last_bracket_location_size = value_size.length;
  1130. var size_width = value_size.substr(1, comma_location_size - 1);
  1131. var size_height_pre = value_size.substr(comma_location_size + 1, last_bracket_location_size - 1);
  1132. var size_height = size_height_pre.substring(0, size_height_pre.length - 1)
  1133. // console.log("Size string length: " + last_bracket_location_size);
  1134. // console.log("Size is " + value_size + ": " + size_width + ", " + size_height);
  1135. var code = '_iot_led_draw_rectangle(' + cord_y + ',' + cord_x + ',' + size_width + ',' + size_height + ',rgba_to_rgb_conversion' + value_color + ')\n';
  1136. return code;
  1137. };
  1138. Blockly.Blocks['iot_led_matrix_show_above'] = {
  1139. init: function () {
  1140. this.appendDummyInput()
  1141. .appendField(Blockly.Msg.x_led_matrix_show_above_iot);
  1142. this.setInputsInline(false);
  1143. this.setPreviousStatement(true, null);
  1144. this.setNextStatement(true, null);
  1145. this.setColour(AILEDCOLOR);
  1146. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Show_Above_TOOLTIP);
  1147. this.setHelpUrl("");
  1148. }
  1149. };
  1150. Blockly.Python['iot_led_matrix_show_above'] = function (block) {
  1151. // TODO: Assemble Python into code variable.
  1152. var code = '_6x6_led_matrix.write()\n';
  1153. return code;
  1154. };
  1155. /*
  1156. _ _____ ____ ____ _ _
  1157. | | | ____| _ \ / ___|| |_ _ __(_)_ __
  1158. | | | _| | | | | \___ \| __| '__| | '_ \
  1159. | |___| |___| |_| | ___) | |_| | | | |_) |
  1160. |_____|_____|____/ |____/ \__|_| |_| .__/
  1161. |_|
  1162. */
  1163. var ESP32_LED_STRIP_COLOR = AILEDCOLOR;
  1164. Blockly.Blocks['esp32_main_controller_led_strip_setup'] = {
  1165. init: function () {
  1166. let types = getLocalStorage("type")
  1167. this.appendDummyInput()
  1168. .appendField(new Blockly.FieldImage("blockly/media/led_strip_setup.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1169. types == 1 ? this.appendDummyInput()
  1170. .appendField(new Blockly.FieldDropdown([
  1171. [Blockly.Msg.ledstrip_setup_text_ai, "ai"],
  1172. [Blockly.Msg.ledstrip_setup_text_iot, "iot"]
  1173. ]), "type")
  1174. .appendField(Blockly.Msg.ledstrip_setup_text)
  1175. .appendField(new Blockly.FieldVariable("STRIP"), "varitem")
  1176. .appendField(Blockly.Msg.ledstrip_setup) :
  1177. this.appendDummyInput()
  1178. .appendField(new Blockly.FieldDropdown([
  1179. [Blockly.Msg.ledstrip_setup_text_iot, "iot"],
  1180. [Blockly.Msg.ledstrip_setup_text_ai, "ai"]
  1181. ]), "type")
  1182. .appendField(Blockly.Msg.ledstrip_setup_text)
  1183. .appendField(new Blockly.FieldVariable("STRIP"), "varitem")
  1184. .appendField(Blockly.Msg.ledstrip_setup);
  1185. this.appendValueInput("io")
  1186. .setCheck(null)
  1187. .appendField(Blockly.Msg.ledstrip_set_gpio);
  1188. this.appendValueInput("count")
  1189. .setCheck(null)
  1190. .appendField(Blockly.Msg.ledstrip_set_total_led);
  1191. this.appendValueInput("brightness")
  1192. .setCheck(null)
  1193. .appendField(Blockly.Msg.ledstrip_set_brightness_setup);
  1194. this.setInputsInline(false);
  1195. this.setPreviousStatement(true, null);
  1196. this.setNextStatement(true, null);
  1197. this.setColour(ESP32_LED_STRIP_COLOR);
  1198. this.setHelpUrl("");
  1199. var thisBlock = this;
  1200. this.setTooltip(function () {
  1201. var mode = thisBlock.getFieldValue('type');
  1202. var TOOLTIPS = {
  1203. 'ai': Blockly.Msg.Esp32_Main_Controller_Led_Strip_Setup_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_ai),
  1204. 'iot': Blockly.Msg.Esp32_Main_Controller_Led_Strip_Setup_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_iot)
  1205. };
  1206. return TOOLTIPS[mode];
  1207. });
  1208. }
  1209. };
  1210. Blockly.Python['esp32_main_controller_led_strip_setup'] = function (block) {
  1211. var dropdown_type = block.getFieldValue('type');
  1212. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1213. var value_io = Blockly.Python.valueToCode(block, 'io', Blockly.Python.ORDER_ATOMIC);
  1214. var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
  1215. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  1216. Blockly.Python.definitions_['esp32_main_controller_led_strip_setup_def'] = '' +
  1217. '_ai_brightness = ' + value_brightness + '\n' +
  1218. '\n' +
  1219. 'def rgba_to_rgb_conversion(red, green, blue):\n' +
  1220. ' if _ai_brightness <= 255:\n' +
  1221. ' alpha = _ai_brightness / 255\n' +
  1222. ' elif _ai_brightness > 255:\n' +
  1223. ' alpha = 255 / 255\n' +
  1224. ' elif _ai_brightness < 0:\n' +
  1225. ' alpha = 0 / 255\n' +
  1226. ' final_red = int((1 - alpha) * 0 + alpha * red)\n' +
  1227. ' final_green = int((1 - alpha) * 0 + alpha * green)\n' +
  1228. ' final_blue = int((1 - alpha) * 0 + alpha * blue)\n' +
  1229. ' final = (final_red, final_green, final_blue)\n' +
  1230. ' return final\n' +
  1231. '';
  1232. if (dropdown_type == "iot") {
  1233. Blockly.Python.definitions_['led_strip_definition'] = 'import machine, neopixel\n';
  1234. Blockly.Python.addVariable(variable_name, variable_name + ' = neopixel.NeoPixel(machine.Pin(' + value_io + '), ' + value_count + ')', true);
  1235. var code = '\n';
  1236. } else if (dropdown_type == "ai") {
  1237. Blockly.Python.definitions_['led_strip_definition'] = 'from modules import ws2812\n';
  1238. Blockly.Python.addVariable(variable_name, '' +
  1239. variable_name + ' = ws2812(' + value_io + ',' + value_count + ')\n' +
  1240. '\n', true);
  1241. var code = '\n';
  1242. }
  1243. return code;
  1244. };
  1245. Blockly.Blocks['esp32_main_controller_led_strip_set_bright'] = {
  1246. init: function () {
  1247. this.appendValueInput("brightness")
  1248. .setCheck(null)
  1249. .appendField(Blockly.Msg.ledstrip_set_brightness_1);
  1250. this.appendDummyInput()
  1251. .appendField(Blockly.Msg.ledstrip_set_brightness_after);
  1252. this.setInputsInline(true);
  1253. this.setPreviousStatement(true, null);
  1254. this.setNextStatement(true, null);
  1255. this.setColour(AILEDCOLOR);
  1256. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Led_strip_set_bright_TOOLTIP);
  1257. this.setHelpUrl("");
  1258. }
  1259. };
  1260. Blockly.Python['esp32_main_controller_led_strip_set_bright'] = function (block) {
  1261. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  1262. // TODO: Assemble Python into code variable.
  1263. var code = '_ai_brightness = ' + value_brightness + '\n';
  1264. return code;
  1265. };
  1266. Blockly.Blocks['esp32_main_controller_led_strip_set'] = {
  1267. init: function () {
  1268. let types = getLocalStorage("type")
  1269. types == 1 ? this.appendDummyInput()
  1270. .appendField(new Blockly.FieldDropdown([
  1271. [Blockly.Msg.ledstrip_setup_text_ai, "ai"],
  1272. [Blockly.Msg.ledstrip_setup_text_iot, "iot"]
  1273. ]), "type")
  1274. .appendField(Blockly.Msg.ledstrip_set_light_up)
  1275. .appendField(new Blockly.FieldVariable("STRIP"), "varitem") :
  1276. this.appendDummyInput()
  1277. .appendField(new Blockly.FieldDropdown([
  1278. [Blockly.Msg.ledstrip_setup_text_iot, "iot"],
  1279. [Blockly.Msg.ledstrip_setup_text_ai, "ai"]
  1280. ]), "type")
  1281. .appendField(Blockly.Msg.ledstrip_set_light_up)
  1282. .appendField(new Blockly.FieldVariable("STRIP"), "varitem");
  1283. this.appendValueInput("location")
  1284. .setCheck(null)
  1285. .appendField(Blockly.Msg.ledstrip_set_no);
  1286. this.appendValueInput("color")
  1287. .setCheck(null)
  1288. .appendField(Blockly.Msg.ledstrip_set_color);
  1289. this.setInputsInline(false);
  1290. this.setPreviousStatement(true, null);
  1291. this.setNextStatement(true, null);
  1292. this.setColour(ESP32_LED_STRIP_COLOR);
  1293. this.setHelpUrl("");
  1294. var thisBlock = this;
  1295. this.setTooltip(function () {
  1296. var mode = thisBlock.getFieldValue('type');
  1297. var TOOLTIPS = {
  1298. 'iot': Blockly.Msg.Esp32_Main_Controller_Led_Strip_Set_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_iot),
  1299. 'ai': Blockly.Msg.Esp32_Main_Controller_Led_Strip_Set_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_ai)
  1300. };
  1301. return TOOLTIPS[mode];
  1302. });
  1303. }
  1304. };
  1305. Blockly.Python['esp32_main_controller_led_strip_set'] = function (block) {
  1306. var dropdown_type = block.getFieldValue('type');
  1307. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1308. var value_location = Blockly.Python.valueToCode(block, 'location', Blockly.Python.ORDER_ATOMIC);
  1309. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  1310. // TODO: Assemble Python into code variable.
  1311. if (dropdown_type == "iot") {
  1312. if (value_color.charAt(0) != '#') {
  1313. var code = '' +
  1314. variable_name + '[' + value_location + '-1] = rgba_to_rgb_conversion' + value_color + '\n';
  1315. } else if (value_color.charAt(0) == '#') {
  1316. var d = 0,
  1317. e = 0,
  1318. f = 0;
  1319. try {
  1320. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1321. e = parseInt(color.substring(3, 5), 16),
  1322. f = parseInt(color.substring(5, 7), 16))
  1323. } catch (g) { }
  1324. var code = '' +
  1325. variable_name + '[' + value_location + '-1] = rgba_to_rgb_conversion(' + d + ',' + e + ',' + f + ')\n';
  1326. }
  1327. } else if (dropdown_type == "ai") {
  1328. if (value_color.charAt(0) != '#') {
  1329. var code = '' +
  1330. '_a = ' + variable_name + '.set_led(' + value_location + '-1, rgba_to_rgb_conversion' + value_color + ')\n';
  1331. } else if (value_color.charAt(0) == '#') {
  1332. var d = 0,
  1333. e = 0,
  1334. f = 0;
  1335. try {
  1336. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1337. e = parseInt(color.substring(3, 5), 16),
  1338. f = parseInt(color.substring(5, 7), 16))
  1339. } catch (g) { }
  1340. var code = '' +
  1341. '_a = ' + variable_name + '.set_led(' + value_location + '-1,rgba_to_rgb_conversion(' + d + ',' + e + ',' + f + '))\n';
  1342. }
  1343. }
  1344. return code;
  1345. };
  1346. Blockly.Blocks['led_strip_show_above'] = {
  1347. init: function () {
  1348. let types = getLocalStorage("type")
  1349. types == 1 ? this.appendDummyInput()
  1350. .appendField(new Blockly.FieldDropdown([
  1351. [Blockly.Msg.ledstrip_setup_text_ai, "ai"],
  1352. [Blockly.Msg.ledstrip_setup_text_iot, "iot"]
  1353. ]), "type")
  1354. .appendField(Blockly.Msg.ledstrip_setup_text_show_above_set)
  1355. .appendField(new Blockly.FieldVariable("STRIP"), "varitem")
  1356. .appendField(Blockly.Msg.ledstrip_setup_text_show_above) :
  1357. this.appendDummyInput()
  1358. .appendField(new Blockly.FieldDropdown([
  1359. [Blockly.Msg.ledstrip_setup_text_iot, "iot"],
  1360. [Blockly.Msg.ledstrip_setup_text_ai, "ai"]
  1361. ]), "type")
  1362. .appendField(Blockly.Msg.ledstrip_setup_text_show_above_set)
  1363. .appendField(new Blockly.FieldVariable("STRIP"), "varitem")
  1364. .appendField(Blockly.Msg.ledstrip_setup_text_show_above);
  1365. this.setInputsInline(false);
  1366. this.setPreviousStatement(true, null);
  1367. this.setNextStatement(true, null);
  1368. this.setColour(AILEDCOLOR);
  1369. this.setHelpUrl("");
  1370. var thisBlock = this;
  1371. this.setTooltip(function () {
  1372. var mode = thisBlock.getFieldValue('type');
  1373. var TOOLTIPS = {
  1374. 'iot': Blockly.Msg.Led_Strip_Show_Above_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_iot),
  1375. 'ai': Blockly.Msg.Led_Strip_Show_Above_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_ai)
  1376. };
  1377. return TOOLTIPS[mode];
  1378. });
  1379. }
  1380. };
  1381. Blockly.Python['led_strip_show_above'] = function (block) {
  1382. var dropdown_type = block.getFieldValue('type');
  1383. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1384. // TODO: Assemble Python into code variable.
  1385. if (dropdown_type == "ai") {
  1386. var code = variable_name + '.display()\n';
  1387. } else if (dropdown_type == "iot") {
  1388. var code = variable_name + '.write()\n';
  1389. }
  1390. return code;
  1391. };
  1392. Blockly.Blocks['led_strip_color_rgb'] = {
  1393. init: function () {
  1394. this.appendDummyInput()
  1395. .appendField(Blockly.Msg.image_process_rgb_r);
  1396. this.appendValueInput("rgb_value_r")
  1397. .setCheck(null)
  1398. .appendField("");
  1399. this.appendDummyInput()
  1400. .appendField(Blockly.Msg.image_process_rgb_g);
  1401. this.appendValueInput("rgb_value_g")
  1402. .setCheck(null)
  1403. .appendField("");
  1404. this.appendDummyInput()
  1405. .appendField(Blockly.Msg.image_process_rgb_b);
  1406. this.appendValueInput("rgb_value_b")
  1407. .setCheck(null)
  1408. .appendField("");
  1409. this.setInputsInline(true);
  1410. this.setOutput(true, null);
  1411. this.setColour(AILEDCOLOR);
  1412. this.setTooltip(Blockly.Msg.Led_Strip_Color_Rgb_TOOLTIP);
  1413. this.setHelpUrl("");
  1414. }
  1415. };
  1416. Blockly.Python['led_strip_color_rgb'] = function (block) {
  1417. var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
  1418. var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
  1419. var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
  1420. // TODO: Assemble Python into code variable.
  1421. var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
  1422. // TODO: Change ORDER_NONE to the correct strength.
  1423. return [code, Blockly.Python.ORDER_NONE];
  1424. };
  1425. /*
  1426. ____ ____ _
  1427. / ___| __ _ _ __ ___ ___| _ \ __ _ __| |
  1428. | | _ / _` | '_ ` _ \ / _ \ |_) / _` |/ _` |
  1429. | |_| | (_| | | | | | | __/ __/ (_| | (_| |
  1430. \____|\__,_|_| |_| |_|\___|_| \__,_|\__,_|
  1431. */
  1432. var GAMEPAD_COLOR = "#183895";
  1433. Blockly.Blocks['x_iot_read_gamepad_button_pressed'] = {
  1434. init: function () {
  1435. this.appendDummyInput()
  1436. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_iot)
  1437. .appendField(new Blockly.FieldDropdown([
  1438. ["X", "x"],
  1439. ["Y", "y"],
  1440. ["A", "a"],
  1441. ["B", "b"],
  1442. ["L", "l"],
  1443. ["R", "r"]
  1444. ]), "type")
  1445. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_pressed_iot);
  1446. this.setInputsInline(true);
  1447. this.setOutput(true, null);
  1448. this.setColour(GAMEPAD_COLOR);
  1449. var thisBlock = this;
  1450. this.setTooltip(function () {
  1451. var mode = thisBlock.getFieldValue('type');
  1452. var TOOLTIPS = {
  1453. 'x': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "X"),
  1454. 'y': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "Y"),
  1455. 'a': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "A"),
  1456. 'b': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "B"),
  1457. 'l': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "L"),
  1458. 'r': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "R")
  1459. };
  1460. return TOOLTIPS[mode];
  1461. });
  1462. this.setHelpUrl("");
  1463. }
  1464. };
  1465. Blockly.Python['x_iot_read_gamepad_button_pressed'] = function (block) {
  1466. var dropdown_type = block.getFieldValue('type');
  1467. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  1468. 'from machine import ADC, Pin\n' +
  1469. 'import time\n' +
  1470. 'from machine import I2C\n' +
  1471. 'from ads1x15 import ADS1115\n' +
  1472. '\n';
  1473. Blockly.Python.definitions_['gamepad_button_def_iot'] = '' +
  1474. '_px = Pin(36, Pin.IN,Pin.PULL_UP)\n' +
  1475. '_py = Pin(39, Pin.IN,Pin.PULL_UP)\n' +
  1476. '_pa = Pin(4, Pin.IN,Pin.PULL_UP)\n' +
  1477. '_pb = Pin(2, Pin.IN,Pin.PULL_UP)\n' +
  1478. '_pl = Pin(17, Pin.IN,Pin.PULL_UP)\n' +
  1479. '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' +
  1480. '\n';
  1481. // TODO: Assemble Python into code variable.
  1482. var code = '_p' + dropdown_type + '.value() == 1';
  1483. // TODO: Change ORDER_NONE to the correct strength.
  1484. return [code, Blockly.Python.ORDER_NONE];
  1485. };
  1486. Blockly.Blocks['x_iot_read_gamepad_button_released'] = {
  1487. init: function () {
  1488. this.appendDummyInput()
  1489. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_iot)
  1490. .appendField(new Blockly.FieldDropdown([
  1491. ["X", "x"],
  1492. ["Y", "y"],
  1493. ["A", "a"],
  1494. ["B", "b"],
  1495. ["L", "l"],
  1496. ["R", "r"]
  1497. ]), "type")
  1498. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_released_iot);
  1499. this.setInputsInline(true);
  1500. this.setOutput(true, null);
  1501. this.setColour(GAMEPAD_COLOR);
  1502. var thisBlock = this;
  1503. this.setTooltip(function () {
  1504. var mode = thisBlock.getFieldValue('type');
  1505. var TOOLTIPS = {
  1506. 'x': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "X"),
  1507. 'y': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "Y"),
  1508. 'a': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "A"),
  1509. 'b': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "B"),
  1510. 'l': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "L"),
  1511. 'r': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "R")
  1512. };
  1513. return TOOLTIPS[mode];
  1514. });
  1515. this.setHelpUrl("");
  1516. }
  1517. };
  1518. Blockly.Python['x_iot_read_gamepad_button_released'] = function (block) {
  1519. var dropdown_type = block.getFieldValue('type');
  1520. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  1521. 'from machine import ADC, Pin\n' +
  1522. 'import time\n' +
  1523. 'from machine import I2C\n' +
  1524. 'from ads1x15 import ADS1115\n' +
  1525. '\n';
  1526. Blockly.Python.definitions_['gamepad_button_def_iot'] = '' +
  1527. '_px = Pin(36, Pin.IN,Pin.PULL_UP)\n' +
  1528. '_py = Pin(39, Pin.IN,Pin.PULL_UP)\n' +
  1529. '_pa = Pin(4, Pin.IN,Pin.PULL_UP)\n' +
  1530. '_pb = Pin(2, Pin.IN,Pin.PULL_UP)\n' +
  1531. '_pl = Pin(17, Pin.IN,Pin.PULL_UP)\n' +
  1532. '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' +
  1533. '\n';
  1534. // TODO: Assemble Python into code variable.
  1535. var code = '_p' + dropdown_type + '.value() == 0';
  1536. // TODO: Change ORDER_NONE to the correct strength.
  1537. return [code, Blockly.Python.ORDER_NONE];
  1538. };
  1539. Blockly.Blocks['x_iot_read_gamepad_button_x-r'] = {
  1540. init: function () {
  1541. this.appendDummyInput()
  1542. .appendField(Blockly.Msg.x_gamepad_read_button_status_first_text_iot)
  1543. .appendField(new Blockly.FieldDropdown([
  1544. ["X", "x"],
  1545. ["Y", "y"],
  1546. ["A", "a"],
  1547. ["B", "b"],
  1548. ["L", "l"],
  1549. ["R", "r"]
  1550. ]), "type")
  1551. .appendField(Blockly.Msg.x_gamepad_read_button_status_after_text_iot);
  1552. this.setInputsInline(true);
  1553. this.setOutput(true, null);
  1554. this.setColour(GAMEPAD_COLOR);
  1555. var thisBlock = this;
  1556. this.setTooltip(function () {
  1557. var mode = thisBlock.getFieldValue('type');
  1558. var TOOLTIPS = {
  1559. 'x': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "X"),
  1560. 'y': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "Y"),
  1561. 'a': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "A"),
  1562. 'b': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "B"),
  1563. 'l': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "L"),
  1564. 'r': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "R")
  1565. };
  1566. return TOOLTIPS[mode];
  1567. });
  1568. this.setHelpUrl("");
  1569. }
  1570. };
  1571. Blockly.Python['x_iot_read_gamepad_button_x-r'] = function (block) {
  1572. var dropdown_type = block.getFieldValue('type');
  1573. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  1574. 'from machine import ADC, Pin\n' +
  1575. 'import time\n' +
  1576. 'from machine import I2C\n' +
  1577. 'from ads1x15 import ADS1115\n' +
  1578. '\n';
  1579. Blockly.Python.definitions_['gamepad_button_def_iot'] = '' +
  1580. '_px = Pin(36, Pin.IN,Pin.PULL_UP)\n' +
  1581. '_py = Pin(39, Pin.IN,Pin.PULL_UP)\n' +
  1582. '_pa = Pin(4, Pin.IN,Pin.PULL_UP)\n' +
  1583. '_pb = Pin(2, Pin.IN,Pin.PULL_UP)\n' +
  1584. '_pl = Pin(17, Pin.IN,Pin.PULL_UP)\n' +
  1585. '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' +
  1586. '\n';
  1587. // TODO: Assemble Python into code variable.
  1588. var code = '_p' + dropdown_type + '.value()';
  1589. // TODO: Change ORDER_NONE to the correct strength.
  1590. return [code, Blockly.Python.ORDER_NONE];
  1591. };
  1592. Blockly.Blocks['x_iot_read_gamepad_joystick'] = {
  1593. init: function () {
  1594. this.appendDummyInput()
  1595. .appendField(Blockly.Msg.x_gamepad_read_joystick_read_iot)
  1596. .appendField(new Blockly.FieldDropdown([
  1597. [Blockly.Msg.x_gamepad_read_joystick_read_iot_x, "X"],
  1598. [Blockly.Msg.x_gamepad_read_joystick_read_iot_y, "Y"]
  1599. ]), "type")
  1600. .appendField(Blockly.Msg.x_gamepad_read_joystick_read_after_iot);
  1601. this.setInputsInline(true);
  1602. this.setOutput(true, null);
  1603. this.setColour(GAMEPAD_COLOR);
  1604. var thisBlock = this;
  1605. this.setTooltip(function () {
  1606. var mode = thisBlock.getFieldValue('type');
  1607. var TOOLTIPS = {
  1608. 'X': Blockly.Msg.x_iot_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_x),
  1609. 'Y': Blockly.Msg.x_iot_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_y)
  1610. };
  1611. return TOOLTIPS[mode];
  1612. });
  1613. this.setHelpUrl("");
  1614. }
  1615. };
  1616. Blockly.Python['x_iot_read_gamepad_joystick'] = function (block) {
  1617. var dropdown_type = block.getFieldValue('type');
  1618. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  1619. 'from machine import ADC, Pin\n' +
  1620. 'import time\n' +
  1621. 'from machine import I2C\n' +
  1622. 'from ads1x15 import ADS1115\n' +
  1623. '\n';
  1624. Blockly.Python.definitions_['gamepad_joystick_def_iot'] = '' +
  1625. '_adc_i2c = I2C(scl=Pin(22), sda=Pin(21))\n' +
  1626. '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
  1627. '\n';
  1628. Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' +
  1629. 'def map_number_scale(val):\n' +
  1630. ' return int(((val - 0) / (26400-0)) * (1023-0) + 0)\n' +
  1631. '\n' +
  1632. 'def _read_direction():\n' +
  1633. ' _adcX = map_number_scale(_joystick_adc.read(channel1=1))\n' +
  1634. ' _adcY = map_number_scale(_joystick_adc.read(channel1=0))\n' +
  1635. ' if (_adcY > 38) and (_adcY < 968) and (_adcX > 38) and (_adcX < 968):\n' +
  1636. ' return "still"\n' +
  1637. ' elif _adcX > 968:\n' +
  1638. ' return "left"\n' +
  1639. ' elif _adcX < 38:\n' +
  1640. ' return "right"\n' +
  1641. ' elif _adcY > 968:\n' +
  1642. ' return "top"\n' +
  1643. ' elif _adcY < 38:\n' +
  1644. ' return "bottom"\n' +
  1645. '\n';
  1646. // TODO: Assemble Python into code variable.
  1647. if (dropdown_type == "X") {
  1648. var code = 'map_number_scale(_joystick_adc.read(channel1=1))';
  1649. } else if (dropdown_type == "Y") {
  1650. var code = 'map_number_scale(_joystick_adc.read(channel1=0))';
  1651. }
  1652. // TODO: Change ORDER_NONE to the correct strength.
  1653. return [code, Blockly.Python.ORDER_NONE];
  1654. };
  1655. Blockly.Blocks['x_iot_read_gamepad_joystick_check'] = {
  1656. init: function () {
  1657. this.appendDummyInput()
  1658. .appendField(Blockly.Msg.x_gamepad_read_joystick_when_iot)
  1659. .appendField(new Blockly.FieldDropdown([
  1660. [Blockly.Msg.x_gamepad_read_joystick_when_top_iot, "top"],
  1661. [Blockly.Msg.x_gamepad_read_joystick_when_bottom_iot, "bottom"],
  1662. [Blockly.Msg.x_gamepad_read_joystick_when_left_iot, "left"],
  1663. [Blockly.Msg.x_gamepad_read_joystick_when_right_iot, "right"],
  1664. [Blockly.Msg.x_gamepad_read_joystick_when_not_iot, "still"],
  1665. ]), "type");
  1666. this.setInputsInline(true);
  1667. this.setOutput(true, null);
  1668. this.setColour(GAMEPAD_COLOR);
  1669. var thisBlock = this;
  1670. this.setTooltip(function () {
  1671. var mode = thisBlock.getFieldValue('type');
  1672. var TOOLTIPS = {
  1673. 'top': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_top_iot),
  1674. 'bottom': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_bottom_iot),
  1675. 'left': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_left_iot),
  1676. 'right': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_right_iot),
  1677. 'still': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_not_iot)
  1678. };
  1679. return TOOLTIPS[mode];
  1680. });
  1681. this.setHelpUrl("");
  1682. }
  1683. };
  1684. Blockly.Python['x_iot_read_gamepad_joystick_check'] = function (block) {
  1685. var dropdown_type = block.getFieldValue('type');
  1686. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  1687. 'from machine import ADC, Pin\n' +
  1688. 'import time\n' +
  1689. 'from machine import I2C\n' +
  1690. 'from ads1x15 import ADS1115\n' +
  1691. '\n';
  1692. Blockly.Python.definitions_['gamepad_joystick_def_iot'] = '' +
  1693. '_adc_i2c = I2C(scl=Pin(22), sda=Pin(21))\n' +
  1694. '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
  1695. '\n';
  1696. Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' +
  1697. 'def map_number_scale(val):\n' +
  1698. ' return int(((val - 0) / (26400-0)) * (1023-0) + 0)\n' +
  1699. '\n' +
  1700. 'def _read_direction():\n' +
  1701. ' _adcX = map_number_scale(_joystick_adc.read(channel1=1))\n' +
  1702. ' _adcY = map_number_scale(_joystick_adc.read(channel1=0))\n' +
  1703. ' if (_adcY > 38) and (_adcY < 968) and (_adcX > 38) and (_adcX < 968):\n' +
  1704. ' return "still"\n' +
  1705. ' elif _adcX > 968:\n' +
  1706. ' return "left"\n' +
  1707. ' elif _adcX < 38:\n' +
  1708. ' return "right"\n' +
  1709. ' elif _adcY > 968:\n' +
  1710. ' return "top"\n' +
  1711. ' elif _adcY < 38:\n' +
  1712. ' return "bottom"\n' +
  1713. '\n';
  1714. // TODO: Assemble Python into code variable.
  1715. var code = '_read_direction() == "' + dropdown_type + '"';
  1716. // TODO: Change ORDER_NONE to the correct strength.
  1717. return [code, Blockly.Python.ORDER_NONE];
  1718. };
  1719. Blockly.Blocks['x_ai_read_gamepad_button_pressed'] = {
  1720. init: function () {
  1721. this.appendDummyInput()
  1722. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_ai)
  1723. .appendField(new Blockly.FieldDropdown([
  1724. ["X", "x"],
  1725. ["Y", "y"],
  1726. ["A", "a"],
  1727. ["B", "b"],
  1728. ["L", "l"],
  1729. ["R", "r"]
  1730. ]), "type")
  1731. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_pressed_ai);
  1732. this.setInputsInline(true);
  1733. this.setOutput(true, null);
  1734. this.setColour(GAMEPAD_COLOR);
  1735. var thisBlock = this;
  1736. this.setTooltip(function () {
  1737. var mode = thisBlock.getFieldValue('type');
  1738. var TOOLTIPS = {
  1739. 'x': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "X"),
  1740. 'y': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "Y"),
  1741. 'a': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "A"),
  1742. 'b': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "B"),
  1743. 'l': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "L"),
  1744. 'r': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "R")
  1745. };
  1746. return TOOLTIPS[mode];
  1747. });
  1748. this.setHelpUrl("");
  1749. }
  1750. };
  1751. Blockly.Python['x_ai_read_gamepad_button_pressed'] = function (block) {
  1752. var dropdown_type = block.getFieldValue('type');
  1753. Blockly.Python.definitions_['gamepad_all_def'] = '' +
  1754. 'from fpioa_manager import *\n' +
  1755. 'from Maix import FPIOA, GPIO\n' +
  1756. '\n';
  1757. Blockly.Python.definitions_['gamepad_button_def'] = '' +
  1758. '_gp_side_button = [13, 14]\n' +
  1759. '_gp_right_button = [22, 21, 15, 17]\n' +
  1760. 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' +
  1761. 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' +
  1762. 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' +
  1763. 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' +
  1764. 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' +
  1765. 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' +
  1766. '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' +
  1767. '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' +
  1768. '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' +
  1769. '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' +
  1770. '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' +
  1771. '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' +
  1772. '\n';
  1773. // TODO: Assemble Python into code variable.
  1774. var code = '_gp_button_' + dropdown_type + '.value() == 1';
  1775. // TODO: Change ORDER_NONE to the correct strength.
  1776. return [code, Blockly.Python.ORDER_NONE];
  1777. };
  1778. Blockly.Blocks['x_ai_read_gamepad_button_released'] = {
  1779. init: function () {
  1780. this.appendDummyInput()
  1781. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_ai)
  1782. .appendField(new Blockly.FieldDropdown([
  1783. ["X", "x"],
  1784. ["Y", "y"],
  1785. ["A", "a"],
  1786. ["B", "b"],
  1787. ["L", "l"],
  1788. ["R", "r"]
  1789. ]), "type")
  1790. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_released_ai);
  1791. this.setInputsInline(true);
  1792. this.setOutput(true, null);
  1793. this.setColour(GAMEPAD_COLOR);
  1794. var thisBlock = this;
  1795. this.setTooltip(function () {
  1796. var mode = thisBlock.getFieldValue('type');
  1797. var TOOLTIPS = {
  1798. 'x': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "X"),
  1799. 'y': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "Y"),
  1800. 'a': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "A"),
  1801. 'b': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "B"),
  1802. 'l': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "L"),
  1803. 'r': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "R")
  1804. };
  1805. return TOOLTIPS[mode];
  1806. });
  1807. this.setHelpUrl("");
  1808. }
  1809. };
  1810. Blockly.Python['x_ai_read_gamepad_button_released'] = function (block) {
  1811. var dropdown_type = block.getFieldValue('type');
  1812. Blockly.Python.definitions_['gamepad_all_def'] = '' +
  1813. 'from fpioa_manager import *\n' +
  1814. 'from Maix import FPIOA, GPIO\n' +
  1815. '\n';
  1816. Blockly.Python.definitions_['gamepad_button_def'] = '' +
  1817. '_gp_side_button = [13, 14]\n' +
  1818. '_gp_right_button = [15, 17, 22, 21]\n' +
  1819. 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' +
  1820. 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' +
  1821. 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' +
  1822. 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' +
  1823. 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' +
  1824. 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' +
  1825. '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' +
  1826. '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' +
  1827. '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' +
  1828. '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' +
  1829. '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' +
  1830. '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' +
  1831. '\n';
  1832. // TODO: Assemble Python into code variable.
  1833. var code = '_gp_button_' + dropdown_type + '.value() == 0';
  1834. // TODO: Change ORDER_NONE to the correct strength.
  1835. return [code, Blockly.Python.ORDER_NONE];
  1836. };
  1837. Blockly.Blocks['x_ai_read_gamepad_button_x-r'] = {
  1838. init: function () {
  1839. this.appendDummyInput()
  1840. .appendField(Blockly.Msg.x_gamepad_read_button_status_first_text_ai)
  1841. .appendField(new Blockly.FieldDropdown([
  1842. ["X", "x"],
  1843. ["Y", "y"],
  1844. ["A", "a"],
  1845. ["B", "b"],
  1846. ["L", "l"],
  1847. ["R", "r"]
  1848. ]), "type")
  1849. .appendField(Blockly.Msg.x_gamepad_read_button_status_after_text_ai);
  1850. this.setInputsInline(true);
  1851. this.setOutput(true, null);
  1852. this.setColour(GAMEPAD_COLOR);
  1853. var thisBlock = this;
  1854. this.setTooltip(function () {
  1855. var mode = thisBlock.getFieldValue('type');
  1856. var TOOLTIPS = {
  1857. 'x': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "X"),
  1858. 'y': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "Y"),
  1859. 'a': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "A"),
  1860. 'b': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "B"),
  1861. 'l': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "L"),
  1862. 'r': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "R")
  1863. };
  1864. return TOOLTIPS[mode];
  1865. });
  1866. this.setHelpUrl("");
  1867. }
  1868. };
  1869. Blockly.Python['x_ai_read_gamepad_button_x-r'] = function (block) {
  1870. var dropdown_type = block.getFieldValue('type');
  1871. Blockly.Python.definitions_['gamepad_all_def'] = '' +
  1872. 'from fpioa_manager import *\n' +
  1873. 'from Maix import FPIOA, GPIO\n' +
  1874. '\n';
  1875. Blockly.Python.definitions_['gamepad_button_def'] = '' +
  1876. '_gp_side_button = [13, 14]\n' +
  1877. '_gp_right_button = [15, 17, 22, 21]\n' +
  1878. 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' +
  1879. 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' +
  1880. 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' +
  1881. 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' +
  1882. 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' +
  1883. 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' +
  1884. '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' +
  1885. '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' +
  1886. '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' +
  1887. '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' +
  1888. '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' +
  1889. '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' +
  1890. '\n';
  1891. // TODO: Assemble Python into code variable.
  1892. var code = '_gp_button_' + dropdown_type + '.value()';
  1893. // TODO: Change ORDER_NONE to the correct strength.
  1894. return [code, Blockly.Python.ORDER_NONE];
  1895. };
  1896. Blockly.Blocks['x_ai_read_gamepad_joystick'] = {
  1897. init: function () {
  1898. this.appendDummyInput()
  1899. .appendField(Blockly.Msg.x_gamepad_read_joystick_read_ai)
  1900. .appendField(new Blockly.FieldDropdown([
  1901. [Blockly.Msg.x_gamepad_read_joystick_read_iot_x, "0"],
  1902. [Blockly.Msg.x_gamepad_read_joystick_read_iot_y, "1"]
  1903. ]), "type")
  1904. .appendField(Blockly.Msg.x_gamepad_read_joystick_read_after_ai);
  1905. this.setInputsInline(true);
  1906. this.setOutput(true, null);
  1907. this.setColour(GAMEPAD_COLOR);
  1908. var thisBlock = this;
  1909. this.setTooltip(function () {
  1910. var mode = thisBlock.getFieldValue('type');
  1911. var TOOLTIPS = {
  1912. '0': Blockly.Msg.x_ai_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_x),
  1913. '1': Blockly.Msg.x_ai_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_y)
  1914. };
  1915. return TOOLTIPS[mode];
  1916. });
  1917. this.setHelpUrl("");
  1918. }
  1919. };
  1920. Blockly.Python['x_ai_read_gamepad_joystick'] = function (block) {
  1921. var dropdown_type = block.getFieldValue('type');
  1922. Blockly.Python.definitions_['gamepad_joystick_def'] = '' +
  1923. 'from machine import I2C\n' +
  1924. 'from ads1x15 import ADS1115\n' +
  1925. '\n' +
  1926. '_adc_i2c = I2C(I2C.I2C0, freq=100000, scl=30, sda=29)\n' +
  1927. '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
  1928. '\n';
  1929. // TODO: Assemble Python into code variable.
  1930. var code = 'int(_joystick_adc.read(channel1=' + dropdown_type + ')/25.75)';
  1931. // TODO: Change ORDER_NONE to the correct strength.
  1932. return [code, Blockly.Python.ORDER_NONE];
  1933. };
  1934. Blockly.Blocks['x_ai_read_gamepad_joystick_check'] = {
  1935. init: function () {
  1936. this.appendDummyInput()
  1937. .appendField(Blockly.Msg.x_gamepad_read_joystick_when_ai)
  1938. .appendField(new Blockly.FieldDropdown([
  1939. [Blockly.Msg.x_gamepad_read_joystick_when_top_ai, "top"],
  1940. [Blockly.Msg.x_gamepad_read_joystick_when_bottom_ai, "bottom"],
  1941. [Blockly.Msg.x_gamepad_read_joystick_when_left_ai, "left"],
  1942. [Blockly.Msg.x_gamepad_read_joystick_when_right_ai, "right"],
  1943. [Blockly.Msg.x_gamepad_read_joystick_when_not_ai, "still"],
  1944. ]), "type");
  1945. this.setInputsInline(true);
  1946. this.setOutput(true, null);
  1947. this.setColour(GAMEPAD_COLOR);
  1948. var thisBlock = this;
  1949. this.setTooltip(function () {
  1950. var mode = thisBlock.getFieldValue('type');
  1951. var TOOLTIPS = {
  1952. 'top': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_top_ai),
  1953. 'bottom': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_bottom_ai),
  1954. 'left': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_left_ai),
  1955. 'right': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_right_ai),
  1956. 'still': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_not_ai)
  1957. };
  1958. return TOOLTIPS[mode];
  1959. });
  1960. this.setHelpUrl("");
  1961. }
  1962. };
  1963. Blockly.Python['x_ai_read_gamepad_joystick_check'] = function (block) {
  1964. var dropdown_type = block.getFieldValue('type');
  1965. Blockly.Python.definitions_['gamepad_joystick_def'] = '' +
  1966. 'from machine import I2C\n' +
  1967. 'from ads1x15 import ADS1115\n' +
  1968. '\n' +
  1969. '_adc_i2c = I2C(I2C.I2C0, freq=100000, scl=30, sda=29)\n' +
  1970. '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
  1971. '\n';
  1972. // TODO: Assemble Python into code variable.
  1973. '\n';
  1974. Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' +
  1975. 'def _read_direction():\n' +
  1976. ' if (int(_joystick_adc.read(channel1=0)/25.75) > 400) and (int(_joystick_adc.read(channel1=0)/25.75) < 600) and (int(_joystick_adc.read(channel1=1)/25.75) > 400) and (int(_joystick_adc.read(channel1=1)/25.75) < 600):\n' +
  1977. ' return "still" \n' +
  1978. ' elif int(_joystick_adc.read(channel1=1)/25.75) > 600:\n' +
  1979. ' return "left"\n' +
  1980. ' elif int(_joystick_adc.read(channel1=1)/25.75) < 400:\n' +
  1981. ' return "right"\n' +
  1982. ' elif int(_joystick_adc.read(channel1=0)/25.75) > 600:\n' +
  1983. ' return "top"\n' +
  1984. ' elif int(_joystick_adc.read(channel1=0)/25.75) < 400:\n' +
  1985. ' return "bottom"\n' +
  1986. '\n';
  1987. // TODO: Assemble Python into code variable.
  1988. var code = '_read_direction() == "' + dropdown_type + '"';
  1989. // TODO: Change ORDER_NONE to the correct strength.
  1990. return [code, Blockly.Python.ORDER_NONE];
  1991. };
  1992. /*
  1993. __ ___ _____ _____
  1994. /\ \ \/ __\ \_ \___/__ \
  1995. / \/ /__\//_____ / /\/ _ \ / /\/
  1996. / /\ / \/ \_____/\/ /_| (_) / /
  1997. \_\ \/\_____/ \____/ \___/\/
  1998. Blockly.Python.addVariable('', '', true);
  1999. Blockly.Python.definitions_['definition'] = '';
  2000. Blockly.Python.addSetup("setup", '');
  2001. */
  2002. var ESP32_NBIOT_COLOR = "#629978";
  2003. Blockly.Blocks['esp32_nbiot_setup'] = {
  2004. init: function () {
  2005. let type = getLocalStorage("type")
  2006. this.appendDummyInput()
  2007. .appendField(new Blockly.FieldImage("blockly/media/nbiot_esp32_header.png", 110, 40, { alt: "*", flipRtl: "FALSE" }));
  2008. type == 1 ? this.appendDummyInput()
  2009. .appendField(new Blockly.FieldDropdown([
  2010. [Blockly.Msg.nbiot_module_which_module_ai, "ai"],
  2011. [Blockly.Msg.nbiot_module_which_module_iot, "iot"]
  2012. ]), "module_type") : this.appendDummyInput()
  2013. .appendField(new Blockly.FieldDropdown([
  2014. [Blockly.Msg.nbiot_module_which_module_iot, "iot"],
  2015. [Blockly.Msg.nbiot_module_which_module_ai, "ai"]
  2016. ]), "module_type");
  2017. this.appendDummyInput()
  2018. .appendField(Blockly.Msg.nbiot_module_init);
  2019. this.setPreviousStatement(true, null);
  2020. this.setNextStatement(true, null);
  2021. this.setColour(ESP32_NBIOT_COLOR);
  2022. var thisBlock = this;
  2023. this.setTooltip(function () {
  2024. var mode = thisBlock.getFieldValue('module_type');
  2025. var TOOLTIPS = {
  2026. 'ai': Blockly.Msg.esp32_nbiot_setup_TOOLTIP.replace('%1', Blockly.Msg.nbiot_module_which_module_ai),
  2027. 'iot': Blockly.Msg.esp32_nbiot_setup_TOOLTIP.replace('%1', Blockly.Msg.nbiot_module_which_module_iot)
  2028. };
  2029. return TOOLTIPS[mode];
  2030. });
  2031. this.setHelpUrl("");
  2032. }
  2033. };
  2034. Blockly.Python['esp32_nbiot_setup'] = function (block) {
  2035. // TODO: Assemble Python into code variable.
  2036. var dropdown_module_type = block.getFieldValue('module_type');
  2037. if (dropdown_module_type == "iot") {
  2038. Blockly.Python.addVariable('_nbiot_serial_read', '_nbiot_serial_read = ""', true);
  2039. Blockly.Python.addVariable('_nbiot_readings', '_nbiot_readings = ""', true);
  2040. Blockly.Python.addVariable('_nbiot_power_on_status', '_nbiot_power_on_status = False', true);
  2041. Blockly.Python.addVariable('_nbiot_internet_status', '_nbiot_internet_status = False', true);
  2042. Blockly.Python.addVariable('_nbiot_internet_ip_address', '_nbiot_internet_ip_address = ""', true);
  2043. Blockly.Python.definitions_['import_definition'] = '' +
  2044. 'from machine import UART\n' +
  2045. 'from machine import Timer\n' +
  2046. 'import ubinascii as binascii\n' +
  2047. 'import time\n' +
  2048. '\n' +
  2049. '_nbiot_serial = UART(2,rx=17, tx=16, baudrate=115200, timeout=10)\n' +
  2050. '\n' +
  2051. '_nbiot_serial_timer = Timer(1)\n' +
  2052. '_nbiot_serial_timer.init(period=50, mode=Timer.PERIODIC, callback=lambda t: _read_nbiot_serial(_nbiot_serial))\n' +
  2053. '\n' +
  2054. 'def _read_nbiot_serial(uart):\n' +
  2055. ' global _nbiot_readings, _nbiot_power_on_status, _nbiot_internet_status, _nbiot_internet_ip_address\n' +
  2056. ' if _nbiot_serial.any():\n' +
  2057. ' _nbiot_serial_read = _nbiot_serial.readline().decode("utf-8")\n' +
  2058. ' _nbiot_serial_read = _nbiot_serial_read.strip()\n' +
  2059. ' _nbiot_readings = _nbiot_serial_read.strip()\n' +
  2060. '\n' +
  2061. ' if _nbiot_serial_read == "+CPIN: READY":\n' +
  2062. ' _nbiot_power_on_status = True\n' +
  2063. ' print("NB-IoT Module Initiated!\\n")\n' +
  2064. ' _nbiot_serial.write(b\'AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n\')\n' +
  2065. '\n' +
  2066. ' if "+IP" in _nbiot_serial_read:\n' +
  2067. ' _nbiot_internet_status = True\n' +
  2068. ' _nbiot_internet_ip_address = _nbiot_serial_read[5:len(_nbiot_serial_read)]\n' +
  2069. '';
  2070. var code = '';
  2071. } else if (dropdown_module_type == "ai") {
  2072. Blockly.Python.addVariable('_nbiot_serial_read', '_nbiot_serial_read = ""', true);
  2073. Blockly.Python.addVariable('_nbiot_readings', '_nbiot_readings = ""', true);
  2074. Blockly.Python.addVariable('_nbiot_power_on_status', '_nbiot_power_on_status = False', true);
  2075. Blockly.Python.addVariable('_nbiot_internet_status', '_nbiot_internet_status = False', true);
  2076. Blockly.Python.addVariable('_nbiot_internet_ip_address', '_nbiot_internet_ip_address = ""', true);
  2077. Blockly.Python.definitions_['import_definition'] = '' +
  2078. 'import machine, time\n' +
  2079. 'from fpioa_manager import fm\n' +
  2080. 'from machine import Timer\n' +
  2081. '\n' +
  2082. 'fm.register(14,fm.fpioa.UART2_TX)\n' +
  2083. 'fm.register(13,fm.fpioa.UART2_RX)\n' +
  2084. '\n' +
  2085. '_nbiot_serial = machine.UART(machine.UART.UART2,115200, 8, None, 1, timeout=50)\n' +
  2086. '\n' +
  2087. 'def _on_timer(timer):\n' +
  2088. ' _read_nbiot_serial(_nbiot_serial)\n' +
  2089. '\n' +
  2090. '_periodic_tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=1, callback=_on_timer, arg=_on_timer)\n' +
  2091. '_periodic_tim.start()\n' +
  2092. '\n' +
  2093. 'def _read_nbiot_serial(uart):\n' +
  2094. ' global _nbiot_readings, _nbiot_power_on_status, _nbiot_internet_status, _nbiot_internet_ip_address\n' +
  2095. ' if _nbiot_serial.any():\n' +
  2096. ' _nbiot_serial_read = _nbiot_serial.readline().decode("utf-8")\n' +
  2097. ' print(_nbiot_serial_read)\n' +
  2098. ' _nbiot_serial_read = _nbiot_serial_read.strip()\n' +
  2099. ' _nbiot_readings = _nbiot_serial_read.strip()\n' +
  2100. '\n' +
  2101. ' if _nbiot_serial_read == "+CPIN: READY":\n' +
  2102. ' _nbiot_power_on_status = True\n' +
  2103. ' print("NB-IoT Module Initiated!\\n")\n' +
  2104. ' _nbiot_serial.write(b\'AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n\')\n' +
  2105. '\n' +
  2106. ' if "+IP" in _nbiot_serial_read:\n' +
  2107. ' _nbiot_internet_status = True\n' +
  2108. ' _nbiot_internet_ip_address = _nbiot_serial_read[5:len(_nbiot_serial_read)]\n' +
  2109. ' print("NB-IoT is Online! Public IP is: " + str(_nbiot_internet_ip_address))\n' +
  2110. '\n' +
  2111. '_nbiot_serial_read = ""\n' +
  2112. '_nbiot_readings = ""\n' +
  2113. '_nbiot_power_on_status = False\n' +
  2114. '_nbiot_internet_status = False\n' +
  2115. '_nbiot_internet_ip_address = ""\n' +
  2116. '';
  2117. var code = '';
  2118. }
  2119. return code;
  2120. };
  2121. Blockly.Blocks['esp32_nbiot_get_power_status'] = {
  2122. init: function () {
  2123. this.appendDummyInput()
  2124. .appendField(Blockly.Msg.nbiot_module_power);
  2125. this.setOutput(true, null);
  2126. this.setColour(ESP32_NBIOT_COLOR);
  2127. this.setTooltip(Blockly.Msg.esp32_nbiot_get_power_status_TOOLTIP);
  2128. this.setHelpUrl("");
  2129. }
  2130. };
  2131. Blockly.Python['esp32_nbiot_get_power_status'] = function (block) {
  2132. // TODO: Assemble Python into code variable.
  2133. var code = '_nbiot_power_on_status';
  2134. // TODO: Change ORDER_NONE to the correct strength.
  2135. return [code, Blockly.Python.ORDER_NONE];
  2136. };
  2137. Blockly.Blocks['esp32_nbiot_get_network_status'] = {
  2138. init: function () {
  2139. this.appendDummyInput()
  2140. .appendField(Blockly.Msg.nbiot_module_connectivity);
  2141. this.setOutput(true, null);
  2142. this.setColour(ESP32_NBIOT_COLOR);
  2143. this.setTooltip(Blockly.Msg.esp32_nbiot_get_network_status_TOOLTIP);
  2144. this.setHelpUrl("");
  2145. }
  2146. };
  2147. Blockly.Python['esp32_nbiot_get_network_status'] = function (block) {
  2148. // TODO: Assemble Python into code variable.
  2149. var code = '_nbiot_internet_status';
  2150. // TODO: Change ORDER_NONE to the correct strength.
  2151. return [code, Blockly.Python.ORDER_NONE];
  2152. };
  2153. Blockly.Blocks['esp32_nbiot_get_network_ip'] = {
  2154. init: function () {
  2155. this.appendDummyInput()
  2156. .appendField(Blockly.Msg.nbiot_module_ip);
  2157. this.setOutput(true, null);
  2158. this.setColour(ESP32_NBIOT_COLOR);
  2159. this.setTooltip(Blockly.Msg.esp32_nbiot_get_network_ip_TOOLTIP);
  2160. this.setHelpUrl("");
  2161. }
  2162. };
  2163. Blockly.Python['esp32_nbiot_get_network_ip'] = function (block) {
  2164. // TODO: Assemble Python into code variable.
  2165. var code = '_nbiot_internet_ip_address';
  2166. // TODO: Change ORDER_NONE to the correct strength.
  2167. return [code, Blockly.Python.ORDER_NONE];
  2168. };
  2169. Blockly.Blocks['esp32_nbiot_onenet_init'] = {
  2170. init: function () {
  2171. this.appendDummyInput()
  2172. .appendField(new Blockly.FieldImage("blockly/media/nbiot_onenet.png", 200, 60, { alt: "*", flipRtl: "FALSE" }));
  2173. this.appendDummyInput()
  2174. .appendField(Blockly.Msg.nbiot_onenet_title);
  2175. this.appendValueInput("product_id")
  2176. .setCheck(null)
  2177. .appendField(Blockly.Msg.nbiot_onenet_connect_productid);
  2178. this.appendValueInput("device_id")
  2179. .setCheck(null)
  2180. .appendField(Blockly.Msg.nbiot_onenet_connect_deviceid);
  2181. this.appendValueInput("device_apikey")
  2182. .setCheck(null)
  2183. .appendField(Blockly.Msg.nbiot_onenet_connect_device_api);
  2184. this.setPreviousStatement(true, null);
  2185. this.setNextStatement(true, null);
  2186. this.setColour(ONENET_BLOCK_COLOR);
  2187. this.setTooltip(Blockly.Msg.esp32_nbiot_onenet_init_TOOLTIP);
  2188. this.setHelpUrl("");
  2189. }
  2190. };
  2191. Blockly.Python['esp32_nbiot_onenet_init'] = function (block) {
  2192. var value_product_id = Blockly.Python.valueToCode(block, 'product_id', Blockly.Python.ORDER_ATOMIC);
  2193. var value_device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
  2194. var value_device_apikey = Blockly.Python.valueToCode(block, 'device_apikey', Blockly.Python.ORDER_ATOMIC);
  2195. // TODO: Assemble Python into code variable.
  2196. Blockly.Python.addVariable('_nbiot_onenet_product_id', '', true);
  2197. Blockly.Python.addVariable('_nbiot_onenet_device_id', '', true);
  2198. Blockly.Python.addVariable('_nbiot_onent_device_apikey', '', true);
  2199. Blockly.Python.addVariable('_nbiot_onenet_connection_status', '', true);
  2200. Blockly.Python.addVariable('_nbiot_onenet_read_anything', '', true);
  2201. Blockly.Python.addVariable('_nbiot_onenet_init_state', '', true);
  2202. Blockly.Python.addSetup("setup", '' +
  2203. '_nbiot_onenet_init_state = False\n' +
  2204. '_nbiot_onenet_connection_status = False\n' +
  2205. '_nbiot_onenet_read_anything = False\n' +
  2206. '_nbiot_onenet_product_id = ' + value_product_id + '\n' +
  2207. '_nbiot_onenet_device_id = ' + value_device_id + '\n' +
  2208. '_nbiot_onent_device_apikey = ' + value_device_apikey + '\n' +
  2209. '\n' +
  2210. 'def _nbiot_send_onenet_data(data):\n' +
  2211. ' _nbiot_send_message = data\n' +
  2212. ' _nbiot_send_data_content = binascii.hexlify(_nbiot_send_message).decode("utf-8")\n' +
  2213. ' _nbiot_send_header = ",0300" + str(hex(len(_nbiot_send_message)).split(\'x\')[-1])\n' +
  2214. ' _nbiot_send_at_content = "AT+MQTTPUB=$dp,0,1,0," + str(len(_nbiot_send_message)+len(_nbiot_send_header[1:7].replace("0",""))) + ",0300" + str(hex(len(_nbiot_send_message)).split(\'x\')[-1]) + str(_nbiot_send_data_content)\n' +
  2215. ' return _nbiot_send_at_content\n' +
  2216. '\n' +
  2217. 'def _nbiot_read_onenet_data(raw_response):\n' +
  2218. ' _nbiot_receive = raw_response\n' +
  2219. ' if "+MQTTPUBLISH" not in raw_response:\n' +
  2220. ' return ""\n' +
  2221. ' elif "+MQTTPUBLISH" in raw_response:\n' +
  2222. ' _nbiot_occur = -1\n' +
  2223. ' for i in range(0, 6): _nbiot_occur = _nbiot_receive.find(",", _nbiot_occur + 1)\n' +
  2224. ' return _nbiot_receive[_nbiot_occur+1:len(_nbiot_receive)]\n' +
  2225. '\n' +
  2226. 'def _read_nbiot_onenet(_incoming_data):\n' +
  2227. ' global _nbiot_onenet_connection_status, _nbiot_onenet_read_anything\n' +
  2228. '\n' +
  2229. ' if "+MQTTOPEN: OK" in _incoming_data:\n' +
  2230. ' _nbiot_onenet_connection_status = True\n' +
  2231. ' # print("OneNET Connected!")\n' +
  2232. ' if "+MQTTOPEN: FAIL" in _incoming_data:\n' +
  2233. ' _nbiot_onenet_connection_status = False\n' +
  2234. ' # print("OneNET Lost Connection!")\n' +
  2235. '\n' +
  2236. ' if "+MQTTPUBLISH" in _incoming_data:\n' +
  2237. ' _nbiot_onenet_read_anything = True\n' +
  2238. ' else:\n' +
  2239. ' _nbiot_onenet_read_anything = False\n' +
  2240. '\n' +
  2241. ' if "+MQTTDISC" in _incoming_data:\n' +
  2242. ' _nbiot_onenet_read_anything = False\n' +
  2243. ' _nbiot_onenet_configuration = "AT+MQTTCFG=183.230.40.39,6002,"+_nbiot_onenet_device_id+",100,"+_nbiot_onenet_product_id+","+_nbiot_onent_device_apikey+",1,0\\r\\n"\n' +
  2244. ' print("Enabling OneNET Connection...")\n' +
  2245. ' _nbiot_serial.write(_nbiot_onenet_configuration.encode())\n' +
  2246. ' time.sleep_ms(100)\n' +
  2247. ' _nbiot_serial.write(b\'AT+MQTTOPEN=1,1,0\\r\\n\')\n' +
  2248. '\n' +
  2249. 'def _init_onenet_connection():\n' +
  2250. ' _nbiot_onenet_configuration = "AT+MQTTCFG=183.230.40.39,6002,"+_nbiot_onenet_device_id+",100,"+_nbiot_onenet_product_id+","+_nbiot_onent_device_apikey+",1,0\\r\\n"\n' +
  2251. ' print("Enabling OneNET Connection...")\n' +
  2252. ' _nbiot_serial.write(_nbiot_onenet_configuration.encode())\n' +
  2253. ' time.sleep_ms(100)\n' +
  2254. ' _nbiot_serial.write(b\'AT+MQTTOPEN=1,1,0\\r\\n\')\n' +
  2255. ' time.sleep_ms(100)\n' +
  2256. '');
  2257. var code = '' +
  2258. 'if (_nbiot_internet_status == True) and (_nbiot_onenet_init_state == False) and (_nbiot_onenet_connection_status == False):\n' +
  2259. ' _init_onenet_connection()\n' +
  2260. ' _nbiot_onenet_init_state = True\n' +
  2261. '\n' +
  2262. '_read_nbiot_onenet(_nbiot_readings)\n' +
  2263. '';
  2264. return code;
  2265. };
  2266. Blockly.Blocks['esp32_nbiot_onenet_connection_status'] = {
  2267. init: function () {
  2268. this.appendDummyInput()
  2269. .appendField(Blockly.Msg.nbiot_onenet_connectivity);
  2270. this.setOutput(true, null);
  2271. this.setColour(ONENET_BLOCK_COLOR);
  2272. this.setTooltip(Blockly.Msg.esp32_nbiot_onenet_connection_status_TOOLTIP);
  2273. this.setHelpUrl("");
  2274. }
  2275. };
  2276. Blockly.Python['esp32_nbiot_onenet_connection_status'] = function (block) {
  2277. // TODO: Assemble Python into code variable.
  2278. var code = '_nbiot_onenet_connection_status';
  2279. // TODO: Change ORDER_NONE to the correct strength.
  2280. return [code, Blockly.Python.ORDER_NONE];
  2281. };
  2282. Blockly.Blocks['iot_service_nbiot_onenet'] = {
  2283. init: function () {
  2284. this.appendDummyInput()
  2285. .appendField(new Blockly.FieldImage("blockly/media/nbiot_onenet_send.png", 210, 60, { alt: "*", flipRtl: "FALSE" }));
  2286. this.appendDummyInput()
  2287. .appendField(Blockly.Msg.nbiot_onenet_send_title);
  2288. /*this.appendValueInput("first_input")
  2289. .setCheck(null)
  2290. .appendField("Field 0");*/
  2291. this.itemCount_ = 1;
  2292. this.updateShape_();
  2293. this.setMutator(new Blockly.Mutator(['iot_service_nbiot_onenet_create_with_item']));
  2294. this.setColour(ONENET_BLOCK_COLOR);
  2295. this.setPreviousStatement(true, null);
  2296. this.setNextStatement(true, null);
  2297. this.setTooltip(Blockly.Msg.iot_service_nbiot_onenet_TOOLTIP);
  2298. this.setHelpUrl("");
  2299. },
  2300. mutationToDom: function () {
  2301. var container = document.createElement('mutation');
  2302. container.setAttribute('items', this.itemCount_);
  2303. return container;
  2304. },
  2305. domToMutation: function (xmlElement) {
  2306. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  2307. this.updateShape_();
  2308. },
  2309. decompose: function (workspace) {
  2310. var containerBlock = workspace.newBlock('iot_service_nbiot_onenet_create_with_container');
  2311. containerBlock.initSvg();
  2312. var connection = containerBlock.getInput('STACK').connection;
  2313. for (var i = 0; i < this.itemCount_; i++) {
  2314. var itemBlock = workspace.newBlock('iot_service_nbiot_onenet_create_with_item');
  2315. itemBlock.initSvg();
  2316. connection.connect(itemBlock.previousConnection);
  2317. connection = itemBlock.nextConnection;
  2318. }
  2319. return containerBlock;
  2320. },
  2321. compose: function (containerBlock) {
  2322. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2323. // Count number of inputs.
  2324. var connections = [];
  2325. while (itemBlock) {
  2326. connections.push(itemBlock.valueConnection_);
  2327. itemBlock = itemBlock.nextConnection &&
  2328. itemBlock.nextConnection.targetBlock();
  2329. }
  2330. // Disconnect any children that don't belong.
  2331. for (var i = 0; i < this.itemCount_; i++) {
  2332. var connection = this.getInput('ADD' + i).connection.targetConnection;
  2333. if (connection && connections.indexOf(connection) == -1) {
  2334. connection.disconnect();
  2335. }
  2336. }
  2337. this.itemCount_ = connections.length;
  2338. this.updateShape_();
  2339. // Reconnect any child blocks.
  2340. for (var i = 0; i < this.itemCount_; i++) {
  2341. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  2342. }
  2343. },
  2344. saveConnections: function (containerBlock) {
  2345. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2346. var i = 0;
  2347. while (itemBlock) {
  2348. var input = this.getInput('ADD' + i);
  2349. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  2350. i++;
  2351. itemBlock = itemBlock.nextConnection &&
  2352. itemBlock.nextConnection.targetBlock();
  2353. }
  2354. },
  2355. updateShape_: function () {
  2356. for (var i = 0; i < this.itemCount_; i++) {
  2357. if (!this.getInput('ADD' + i)) {
  2358. var input = this.appendValueInput('ADD' + i);
  2359. input.appendField(Blockly.Msg.nbiot_onenet_send_property)
  2360. .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
  2361. //input.appendField("資料 " + (i + 1) + ":");
  2362. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  2363. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  2364. }
  2365. }
  2366. while (this.getInput('ADD' + i)) {
  2367. this.removeInput('ADD' + i);
  2368. i++;
  2369. }
  2370. },
  2371. };
  2372. Blockly.Blocks['iot_service_nbiot_onenet_create_with_container'] = {
  2373. init: function () {
  2374. this.setColour(ONENET_BLOCK_COLOR);
  2375. this.appendDummyInput()
  2376. .appendField("Items");
  2377. this.appendStatementInput('STACK');
  2378. this.setTooltip('');
  2379. this.contextMenu = false;
  2380. }
  2381. };
  2382. Blockly.Blocks['iot_service_nbiot_onenet_create_with_item'] = {
  2383. init: function () {
  2384. this.setColour(ONENET_BLOCK_COLOR);
  2385. this.appendDummyInput()
  2386. .appendField("Field");
  2387. this.setPreviousStatement(true);
  2388. this.setNextStatement(true);
  2389. this.setTooltip('');
  2390. this.contextMenu = false;
  2391. }
  2392. };
  2393. Blockly.Python['iot_service_nbiot_onenet'] = function (block) {
  2394. // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
  2395. var item_field = '',
  2396. item_value = '';
  2397. var nbiot_onenet_data = "'{";
  2398. for (var n = 0; n < this.itemCount_; n++) {
  2399. item_field = block.getFieldValue("field" + n);
  2400. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  2401. nbiot_onenet_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
  2402. }
  2403. var nbiot_onenet_data_final = nbiot_onenet_data + "}'";
  2404. var code = '' +
  2405. '_data_to_be_send = ' + nbiot_onenet_data_final.slice(0, -3) + '}\\r\\n\'' + '\n' +
  2406. '_data_add_rn = _nbiot_send_onenet_data(_data_to_be_send) + "\\r\\n"\n' +
  2407. '_data_for_at = _data_add_rn.encode()\n' +
  2408. '_nbiot_serial.write(_data_for_at)\n' +
  2409. // '_nbiot_serial.write(_nbiot_send_onenet_data(_data_to_be_send).encode())\n' +
  2410. '\n' +
  2411. '';
  2412. return code;
  2413. };
  2414. Blockly.Blocks['iot_service_nbiot_onenet_read'] = {
  2415. init: function () {
  2416. this.appendDummyInput()
  2417. .appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg);
  2418. this.appendValueInput("message")
  2419. .setCheck(null);
  2420. this.appendDummyInput()
  2421. .appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg_when);
  2422. this.appendStatementInput("exec")
  2423. .setCheck(null)
  2424. .appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg_exec);
  2425. this.setPreviousStatement(true, null);
  2426. this.setNextStatement(true, null);
  2427. this.setColour(ONENET_BLOCK_COLOR);
  2428. this.setTooltip(Blockly.Msg.iot_service_nbiot_onenet_read_TOOLTIP);
  2429. this.setHelpUrl("");
  2430. }
  2431. };
  2432. Blockly.Python['iot_service_nbiot_onenet_read'] = function (block) {
  2433. var value_message = Blockly.Python.valueToCode(block, 'message', Blockly.Python.ORDER_ATOMIC);
  2434. var statements_exec = Blockly.Python.statementToCode(block, 'exec');
  2435. // TODO: Assemble Python into code variable.
  2436. var code = 'if (_nbiot_read_onenet_data(_nbiot_readings) == ' + value_message + '):\n' +
  2437. statements_exec +
  2438. '\n';
  2439. return code;
  2440. };
  2441. Blockly.Blocks['esp32_nbiot_onenet_read_value'] = {
  2442. init: function () {
  2443. this.appendDummyInput()
  2444. .appendField(Blockly.Msg.nbiot_onenet_receive_command);
  2445. this.setOutput(true, null);
  2446. this.setColour(ONENET_BLOCK_COLOR);
  2447. this.setTooltip(Blockly.Msg.esp32_nbiot_onenet_read_value_TOOLTIP);
  2448. this.setHelpUrl("");
  2449. }
  2450. };
  2451. Blockly.Python['esp32_nbiot_onenet_read_value'] = function (block) {
  2452. // TODO: Assemble Python into code variable.
  2453. var code = '_nbiot_read_onenet_data(_nbiot_readings)';
  2454. // TODO: Change ORDER_NONE to the correct strength.
  2455. return [code, Blockly.Python.ORDER_NONE];
  2456. };
  2457. Blockly.Blocks['iot_service_nbiot_cococloud_read'] = {
  2458. init: function () {
  2459. this.appendDummyInput()
  2460. .appendField(new Blockly.FieldImage("blockly/media/nbiot_cococloud_get.png", 260, 60, { alt: "*", flipRtl: "FALSE" }));
  2461. this.appendDummyInput()
  2462. .appendField(Blockly.Msg.nbiot_cococloud_get_event_title);
  2463. this.appendDummyInput()
  2464. .appendField(Blockly.Msg.nbiot_cococloud_get_event_api)
  2465. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "t_api");
  2466. this.setPreviousStatement(true, null);
  2467. this.setNextStatement(true, null);
  2468. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2469. this.setTooltip("");
  2470. this.setHelpUrl("");
  2471. }
  2472. };
  2473. Blockly.Python['iot_service_nbiot_cococloud_read'] = function (block) {
  2474. var api = block.getFieldValue('t_api');
  2475. // TODO: Assemble Python into code variable.
  2476. var code = '\n';
  2477. return code;
  2478. };
  2479. Blockly.Blocks['iot_service_nbiot_cococloud_read_data'] = {
  2480. init: function () {
  2481. this.appendDummyInput()
  2482. .appendField(Blockly.Msg.nbiot_cococloud_get_property);
  2483. this.appendValueInput("property")
  2484. .setCheck(null);
  2485. this.appendDummyInput()
  2486. .appendField(Blockly.Msg.nbiot_cococloud_get_data);
  2487. this.setInputsInline(true);
  2488. this.setOutput(true, null);
  2489. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2490. this.setTooltip("");
  2491. this.setHelpUrl("");
  2492. }
  2493. };
  2494. Blockly.Python['iot_service_nbiot_cococloud_read_data'] = function (block) {
  2495. var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC);
  2496. // TODO: Assemble Python into code variable.
  2497. var code = '\n';
  2498. // TODO: Change ORDER_NONE to the correct strength.
  2499. return [code, Blockly.Python.ORDER_NONE];
  2500. };
  2501. Blockly.Blocks['iot_service_nbiot_cococloud'] = {
  2502. init: function () {
  2503. this.appendDummyInput()
  2504. .appendField(new Blockly.FieldImage("blockly/media/nbiot_cococloud_send.png", 260, 60, { alt: "*", flipRtl: "FALSE" }));
  2505. this.appendDummyInput()
  2506. .appendField(Blockly.Msg.nbiot_cococloud_send_title);
  2507. this.appendDummyInput()
  2508. .appendField(Blockly.Msg.nbiot_cococloud_send_api)
  2509. .appendField(new Blockly.FieldTextInput("ENTET_YOUT_EVENT_API_KEY"), "t_api");
  2510. /*this.appendValueInput("first_input")
  2511. .setCheck(null)
  2512. .appendField("Field 0");*/
  2513. this.itemCount_ = 1;
  2514. this.updateShape_();
  2515. this.setMutator(new Blockly.Mutator(['iot_service_nbiot_cococloud_create_with_item']));
  2516. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2517. this.setPreviousStatement(true, null);
  2518. this.setNextStatement(true, null);
  2519. this.setTooltip(Blockly.Msg.iot_service_nbiot_cococloud_TOOLTIP);
  2520. this.setHelpUrl("");
  2521. },
  2522. mutationToDom: function () {
  2523. var container = document.createElement('mutation');
  2524. container.setAttribute('items', this.itemCount_);
  2525. return container;
  2526. },
  2527. domToMutation: function (xmlElement) {
  2528. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  2529. this.updateShape_();
  2530. },
  2531. decompose: function (workspace) {
  2532. var containerBlock = workspace.newBlock('iot_service_nbiot_cococloud_create_with_container');
  2533. containerBlock.initSvg();
  2534. var connection = containerBlock.getInput('STACK').connection;
  2535. for (var i = 0; i < this.itemCount_; i++) {
  2536. var itemBlock = workspace.newBlock('iot_service_nbiot_cococloud_create_with_item');
  2537. itemBlock.initSvg();
  2538. connection.connect(itemBlock.previousConnection);
  2539. connection = itemBlock.nextConnection;
  2540. }
  2541. return containerBlock;
  2542. },
  2543. compose: function (containerBlock) {
  2544. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2545. // Count number of inputs.
  2546. var connections = [];
  2547. while (itemBlock) {
  2548. connections.push(itemBlock.valueConnection_);
  2549. itemBlock = itemBlock.nextConnection &&
  2550. itemBlock.nextConnection.targetBlock();
  2551. }
  2552. // Disconnect any children that don't belong.
  2553. for (var i = 0; i < this.itemCount_; i++) {
  2554. var connection = this.getInput('ADD' + i).connection.targetConnection;
  2555. if (connection && connections.indexOf(connection) == -1) {
  2556. connection.disconnect();
  2557. }
  2558. }
  2559. this.itemCount_ = connections.length;
  2560. this.updateShape_();
  2561. // Reconnect any child blocks.
  2562. for (var i = 0; i < this.itemCount_; i++) {
  2563. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  2564. }
  2565. },
  2566. saveConnections: function (containerBlock) {
  2567. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2568. var i = 0;
  2569. while (itemBlock) {
  2570. var input = this.getInput('ADD' + i);
  2571. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  2572. i++;
  2573. itemBlock = itemBlock.nextConnection &&
  2574. itemBlock.nextConnection.targetBlock();
  2575. }
  2576. },
  2577. updateShape_: function () {
  2578. for (var i = 0; i < this.itemCount_; i++) {
  2579. if (!this.getInput('ADD' + i)) {
  2580. var input = this.appendValueInput('ADD' + i);
  2581. input.appendField(Blockly.Msg.nbiot_cococloud_send_property)
  2582. .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
  2583. //input.appendField("資料 " + (i + 1) + ":");
  2584. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  2585. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  2586. }
  2587. }
  2588. while (this.getInput('ADD' + i)) {
  2589. this.removeInput('ADD' + i);
  2590. i++;
  2591. }
  2592. },
  2593. };
  2594. Blockly.Blocks['iot_service_nbiot_cococloud_create_with_container'] = {
  2595. init: function () {
  2596. this.setColour(ESP32_NBIOT_COLOR);
  2597. this.appendDummyInput()
  2598. .appendField("Items");
  2599. this.appendStatementInput('STACK');
  2600. this.setTooltip('');
  2601. this.contextMenu = false;
  2602. }
  2603. };
  2604. Blockly.Blocks['iot_service_nbiot_cococloud_create_with_item'] = {
  2605. init: function () {
  2606. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2607. this.appendDummyInput()
  2608. .appendField("Field");
  2609. this.setPreviousStatement(true);
  2610. this.setNextStatement(true);
  2611. this.setTooltip('');
  2612. this.contextMenu = false;
  2613. }
  2614. };
  2615. Blockly.Python['iot_service_nbiot_cococloud'] = function (block) {
  2616. var api = block.getFieldValue('t_api');
  2617. Blockly.Python.addVariable('_nbiot_cococloud_send_init_state', '', true);
  2618. Blockly.Python.addVariable('_nbiot_cococloud_json_content', '', true);
  2619. Blockly.Python.definitions_['import'] = '' +
  2620. '_nbiot_cococloud_send_init_state = False\n' +
  2621. '\n' +
  2622. 'def _nbiot_cococloud_send(data):\n' +
  2623. ' _nbiot_cococloud_send_data = \'AT+HTTPCONTENT=0,\"\'+ data + \'\"\\r\\n\'\n' +
  2624. ' print(_nbiot_cococloud_send_data)\n' +
  2625. ' _nbiot_serial.write(b\'AT+HTTPCLOSE=0\\r\\n\')\n' +
  2626. ' time.sleep_ms(500)\n' +
  2627. ' _nbiot_serial.write(b\'AT+HTTPCREATE=\"http://api.cocorobo.cn/\"\\r\\n\')\n' +
  2628. ' time.sleep_ms(500)\n' +
  2629. ' _nbiot_serial.write(b\'AT+HTTPHEADER=0,\"Content-Type: application/json\\\\r\\\\n\"\\r\\n\')\n' +
  2630. ' time.sleep_ms(500)\n' +
  2631. ' _nbiot_serial.write(_nbiot_cococloud_send_data.encode())\n' +
  2632. ' time.sleep_ms(500)\n' +
  2633. ' _nbiot_serial.write(b\'AT+HTTPSEND=0,1,\"/iot/data/eventAPIKey/' + api + '\"\\r\\n\')\n' +
  2634. ' time.sleep_ms(500)\n' +
  2635. '';
  2636. var item_field = '',
  2637. item_value = '';
  2638. var nbiot_cococloud_data = "'{";
  2639. for (var n = 0; n < this.itemCount_; n++) {
  2640. item_field = block.getFieldValue("field" + n);
  2641. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  2642. nbiot_cococloud_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
  2643. }
  2644. var nbiot_cococloud_data_final = nbiot_cococloud_data + "}'";
  2645. var code = '' +
  2646. '_nbiot_cococloud_json_content = ' + nbiot_cococloud_data_final.slice(0, -3) + '}\'' + '\n' +
  2647. '_nbiot_cococloud_send(_nbiot_cococloud_json_content)\n' +
  2648. '';
  2649. return code;
  2650. };
  2651. Blockly.Blocks['3rd_party_sensor_hcsr04'] = {
  2652. init: function () {
  2653. let types = getLocalStorage("type")
  2654. this.appendDummyInput()
  2655. .appendField(new Blockly.FieldImage("blockly/media/ultrasonic_setup_header.png", 120, 40, { alt: "*", flipRtl: "FALSE" }));
  2656. types == 1 ? this.appendDummyInput()
  2657. .appendField(new Blockly.FieldDropdown([
  2658. [Blockly.Msg.third_party_hcsr04_ai, "ai"],
  2659. [Blockly.Msg.third_party_hcsr04_iot, "iot"]
  2660. ]), "type")
  2661. .appendField(Blockly.Msg.third_party_hcsr04_setup_title)
  2662. .appendField(new Blockly.FieldDropdown([
  2663. ["1", "1"],
  2664. ["2", "2"],
  2665. ["3", "3"]
  2666. ]), "index") : this.appendDummyInput()
  2667. .appendField(new Blockly.FieldDropdown([
  2668. [Blockly.Msg.third_party_hcsr04_iot, "iot"],
  2669. [Blockly.Msg.third_party_hcsr04_ai, "ai"]
  2670. ]), "type")
  2671. .appendField(Blockly.Msg.third_party_hcsr04_setup_title)
  2672. .appendField(new Blockly.FieldDropdown([
  2673. ["1", "1"],
  2674. ["2", "2"],
  2675. ["3", "3"]
  2676. ]), "index");
  2677. this.appendDummyInput()
  2678. .appendField(Blockly.Msg.third_party_hcsr04_trig_pin)
  2679. .appendField(new Blockly.FieldNumber(2, 0, 40, 1), "trig")
  2680. .appendField(Blockly.Msg.third_party_hcsr04_echo_pin)
  2681. .appendField(new Blockly.FieldNumber(3, 0, 40, 1), "echo");
  2682. this.setPreviousStatement(true, null);
  2683. this.setNextStatement(true, null);
  2684. this.setColour("#d72bd5");
  2685. this.setHelpUrl("");
  2686. var thisBlock = this;
  2687. this.setTooltip(function () {
  2688. var mode = thisBlock.getFieldValue('type');
  2689. var TOOLTIPS = {
  2690. 'ai': Blockly.Msg.Trd_Party_Sensor_Hcsr04_TOOLTIP.replace('%1', Blockly.Msg.third_party_hcsr04_ai),
  2691. 'iot': Blockly.Msg.Trd_Party_Sensor_Hcsr04_TOOLTIP.replace('%1', Blockly.Msg.third_party_hcsr04_iot)
  2692. };
  2693. return TOOLTIPS[mode];
  2694. });
  2695. }
  2696. };
  2697. Blockly.Python['3rd_party_sensor_hcsr04'] = function (block) {
  2698. var dropdown_type = block.getFieldValue('type');
  2699. var dropdown_index = block.getFieldValue('index');
  2700. var number_trig = block.getFieldValue('trig');
  2701. var number_echo = block.getFieldValue('echo');
  2702. // TODO: Assemble Python into code variable.
  2703. if (dropdown_type == "iot") {
  2704. Blockly.Python.definitions_['import_hcsr04_def_' + number_trig + number_echo] = '' +
  2705. 'from HCSR04 import HCSR04\n' +
  2706. 'from machine import Pin\n' +
  2707. 'import time\n' +
  2708. '\n' +
  2709. '_ultrasonic_sensor_trig_' + number_trig + ' = Pin(' + number_trig + ',Pin.OUT)\n' +
  2710. '_ultrasonic_sensor_echo_' + number_echo + ' = Pin(' + number_echo + ',Pin.IN)\n' +
  2711. '_ultrasonic_sensor_read_' + dropdown_index + ' = HCSR04(_ultrasonic_sensor_trig_' + number_trig + ',_ultrasonic_sensor_echo_' + number_echo + ')\n' +
  2712. '\n';
  2713. var code = '';
  2714. } else if (dropdown_type == "ai") {
  2715. Blockly.Python.definitions_['import_hcsr04_def_' + number_trig + number_echo] = '' +
  2716. 'from HCSR04 import HCSR04\n' +
  2717. 'from fpioa_manager import *\n' +
  2718. 'from Maix import GPIO\n' +
  2719. 'import time\n' +
  2720. '\n' +
  2721. 'fm.register(' + number_trig + ',fm.fpioa.GPIOHS' + number_trig + ')\n' +
  2722. 'fm.register(' + number_echo + ',fm.fpioa.GPIOHS' + number_echo + ')\n' +
  2723. '\n' +
  2724. '_ultrasonic_sensor_trig_' + number_trig + ' = GPIO(GPIO.GPIOHS' + number_trig + ', GPIO.OUT)\n' +
  2725. '_ultrasonic_sensor_echo_' + number_echo + ' = GPIO(GPIO.GPIOHS' + number_echo + ', GPIO.IN)\n' +
  2726. '_ultrasonic_sensor_read_' + dropdown_index + ' = HCSR04(_ultrasonic_sensor_trig_' + number_trig + ',_ultrasonic_sensor_echo_' + number_echo + ')\n' +
  2727. '\n';
  2728. var code = '';
  2729. }
  2730. return code;
  2731. };
  2732. Blockly.Blocks['3rd_party_sensor_hcsr04_read'] = {
  2733. init: function () {
  2734. this.appendDummyInput()
  2735. .appendField(Blockly.Msg.third_party_hcsr04_read_text)
  2736. .appendField(new Blockly.FieldDropdown([
  2737. ["1", "1"],
  2738. ["2", "2"],
  2739. ["3", "3"]
  2740. ]), "index")
  2741. .appendField(Blockly.Msg.third_party_hcsr04_read_text_after);
  2742. this.setOutput(true, null);
  2743. this.setColour("#d72bd5");
  2744. this.setTooltip(Blockly.Msg.Trd_Party_Sensor_Hcsr04_Read_TOOLTIP);
  2745. this.setHelpUrl("");
  2746. }
  2747. };
  2748. Blockly.Python['3rd_party_sensor_hcsr04_read'] = function (block) {
  2749. var dropdown_index = block.getFieldValue('index');
  2750. // TODO: Assemble Python into code variable.
  2751. var code = '_ultrasonic_sensor_read_' + dropdown_index + '.getDistance()';
  2752. // TODO: Change ORDER_NONE to the correct strength.
  2753. return [code, Blockly.Python.ORDER_NONE];
  2754. };
  2755. Blockly.Blocks['Pedometer_Init'] = {
  2756. init: function () {
  2757. this.appendDummyInput()
  2758. .appendField(Blockly.Msg.Pedometer_Init);
  2759. this.appendValueInput("time")
  2760. .setCheck(null)
  2761. .appendField(Blockly.Msg.Pedometer_Init_time);
  2762. this.appendValueInput("sensitivity")
  2763. .setCheck(null)
  2764. .appendField(Blockly.Msg.Pedometer_Init_sensitivity);
  2765. this.setInputsInline(false);
  2766. this.setPreviousStatement(true, null);
  2767. this.setNextStatement(true, null);
  2768. this.setColour("#d42b03");
  2769. this.setTooltip(Blockly.Msg.Pedometer_Init_TOOLTIP);
  2770. this.setHelpUrl("");
  2771. }
  2772. };
  2773. Blockly.Python['Pedometer_Init'] = function (block) {
  2774. var time = Blockly.Python.valueToCode(block, 'time', Blockly.Python.ORDER_ATOMIC);
  2775. var sensitivity = Blockly.Python.valueToCode(block, 'sensitivity', Blockly.Python.ORDER_ATOMIC);
  2776. Blockly.Python.definitions_['Pedometer_Init'] = `from machine import I2C, Pin
  2777. import math
  2778. import mpu6050
  2779. #申明mpu6050传感器IIC通信引脚
  2780. _SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))
  2781. threshold_time=`+ time + ` #时间筛选阈值
  2782. threshold_hill=`+ sensitivity + ` #波峰波谷差值阈值
  2783. xval=[None]*100 #初始X轴加速度原始数据采集列表
  2784. yval=[None]*100 #初始Y轴加速度原始数据采集列表
  2785. zval=[None]*100 #初始Z轴加速度原始数据采集列表
  2786. xavg=0 #X轴方向初始加速度平均值
  2787. yavg=0 #Y轴方向初始加速度平均值
  2788. zavg=0 #Z轴方向初始加速度平均值
  2789. xaccl=[None]*100 #X轴加速度实时采集列表
  2790. yaccl=[None]*100 #Y轴加速度实时采集列表
  2791. zaccl=[None]*100 #Z轴加速度实时采集列表
  2792. totvect=0 #三个方向总加速度矢量值
  2793. totave=[0.0]*2 #最大和最小加速度矢量值平均值
  2794. peak=0 #波峰数据
  2795. trough=0 #波谷数据
  2796. data=[0]*15 #实时采集数据滚动递进列表
  2797. steps=0 #行走步伐数
  2798. flag0=0 #步数提取标志位
  2799. flag1=0 #波峰提取标志位
  2800. flag2=0 #波谷提取标志位
  2801. hill=0 #波峰波谷差值
  2802. starttime=0 #波峰时间点
  2803. endtime=0 #波谷时间点
  2804. lasttime=0 #波峰波谷间隔时间
  2805. _MPU = mpu6050.accel(_SENSOR_MPU_PINS)
  2806. _MPU_SHAKING_OFFSET = _MPU.get_values()['AcZ']
  2807. _MPU_ACCL_X = _MPU.get_values()['AcX']
  2808. _MPU_ACCL_Y = _MPU.get_values()['AcY']
  2809. _MPU_ACCL_Z = _MPU.get_values()['AcZ']
  2810. _MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE('X')
  2811. _MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE('Y')
  2812. _MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE('Z')
  2813. #获取三个方向加速度函数
  2814. def mpu6050_get_accel(type):
  2815. _MPU = mpu6050.accel(_SENSOR_MPU_PINS)
  2816. _MPU_SHAKING_OFFSET = _MPU.get_values()['AcZ']
  2817. _MPU_ACCL_X = _MPU.get_values()['AcX']
  2818. _MPU_ACCL_Y = _MPU.get_values()['AcY']
  2819. _MPU_ACCL_Z = _MPU.get_values()['AcZ']
  2820. _MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE('X')
  2821. _MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE('Y')
  2822. _MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE('Z')
  2823. if type == 'X':
  2824. return _MPU_ACCL_X
  2825. if type == 'Y':
  2826. return _MPU_ACCL_Y
  2827. if type == 'Z':
  2828. return _MPU_ACCL_Z
  2829. #初始化校准函数
  2830. def calibration():
  2831. sum0=0 #x轴加速度累计值
  2832. sum1=0 #y轴加速度累计值
  2833. sum2=0 #z轴加速度累计值
  2834. for i in range(100):
  2835. xval[i]=mpu6050_get_accel('X')-345
  2836. sum0=xval[i]+sum0
  2837. time.sleep_ms(100)
  2838. xavg=sum0/100
  2839. #print(xavg)
  2840. for i in range(100):
  2841. yval[i]=mpu6050_get_accel('Y')-346
  2842. sum1=yval[i]+sum1
  2843. time.sleep_ms(100)
  2844. yavg=sum1/100
  2845. #print(yavg)
  2846. for i in range(100):
  2847. zval[i]=mpu6050_get_accel('Z')-416
  2848. sum2=zval[i]+sum2
  2849. time.sleep_ms(100)
  2850. zavg=sum2/100
  2851. #print(zavg)
  2852. def getPedometer():
  2853. global q, a, xaccl, yaccl, zaccl, totvect, totave, data, peak, flag0, flag1, flag2, starttime, lasttime, endtime, hill, trough, threshold_hill, hill, threshold_time, steps
  2854. #for a in range(100):
  2855. xaccl=mpu6050_get_accel('X')-345
  2856. time.sleep_ms(1)
  2857. yaccl=mpu6050_get_accel('Y')-346
  2858. time.sleep_ms(1)
  2859. zaccl=mpu6050_get_accel('Z')-416
  2860. time.sleep_ms(1)
  2861. #计算X、Y、Z轴三个方向加速度值的平方根来计算总加速度矢量
  2862. totvect=math.sqrt(((xaccl-xavg)*(xaccl-xavg))+((yaccl-yavg)*(yaccl-yavg))+((zaccl-zavg)*(zaccl-zavg)))
  2863. totave[0]=totave[1]
  2864. totave[1]=(totvect+totave[0])/2
  2865. #数据轮替
  2866. data[2]=data[1]
  2867. data[1]=data[0]
  2868. data[0]=totave[1]
  2869. #检测是否到达一个波峰,如果是,将波峰数据采集标志位置1,并开始计时
  2870. if ((data[1]>data[0])&(data[1]>data[2])&(flag1==0)):
  2871. peak=data[1]
  2872. flag1=1
  2873. starttime=time.ticks_ms()
  2874. #检测是否到达一个波谷,如果是,将波谷数据采集标志位置1,并读取当前时间
  2875. if ((data[1]<data[0])&(data[1]<data[2])&(flag2==0)):
  2876. trough=data[1]
  2877. flag2=1
  2878. endtime=time.ticks_ms()
  2879. #如果采集到一组波峰波谷数据
  2880. if((flag1==1)&(flag2==1)):
  2881. flag1=0 #将波峰数据采集标志位置0
  2882. flag2=0 #将波谷数据采集标指位置1
  2883. hill=peak-trough #计算波峰波谷差值
  2884. lasttime=endtime-starttime #计算波峰波谷间隔时间
  2885. if(hill>threshold_hill)&(lasttime>threshold_time): #如果波峰波谷差值大于threshold_hill且间隔时间大于threshold_time
  2886. steps=steps+1 #步数加一
  2887. flag0=1 #将步数数据提取标志位置1
  2888. lasttime=0 #将波峰波谷间隔时间清零
  2889. hill=0 #将波峰波谷差值清零
  2890. return steps
  2891. time.sleep_ms(20)
  2892. `;
  2893. var code = "";
  2894. return code;
  2895. };
  2896. Blockly.Blocks['Pedometer_Run'] = {
  2897. init: function () {
  2898. this.appendDummyInput()
  2899. .appendField(Blockly.Msg.Pedometer_Run);
  2900. this.setInputsInline(false);
  2901. this.setPreviousStatement(true, null);
  2902. this.setNextStatement(true, null);
  2903. this.setColour("#d42b03");
  2904. this.setTooltip(Blockly.Msg.Pedometer_Run_TOOLTIP);
  2905. this.setHelpUrl("");
  2906. }
  2907. };
  2908. Blockly.Python['Pedometer_Run'] = function (block) {
  2909. var code = `
  2910. calibration()
  2911. `;
  2912. return code;
  2913. };
  2914. Blockly.Blocks['Pedometer_Get'] = {
  2915. init: function () {
  2916. this.appendDummyInput()
  2917. .appendField(Blockly.Msg.Pedometer_Get);
  2918. this.setInputsInline(true);
  2919. this.setOutput(true, null);
  2920. this.setColour("#d42b03");
  2921. this.setTooltip(Blockly.Msg.Pedometer_Get_TOOLTIP);
  2922. this.setHelpUrl("");
  2923. }
  2924. };
  2925. Blockly.Python['Pedometer_Get'] = function (block) {
  2926. var code = `getPedometer()`;
  2927. return [code, Blockly.Python.ORDER_NONE];
  2928. };