extension_module.js 189 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675
  1. // Serial
  2. Blockly.Blocks['serial_write_data_create_with_container'] = {
  3. init: function () {
  4. this.setColour("#22b845");
  5. this.appendDummyInput()
  6. .appendField(Blockly.Msg.serialcomm_write_item);
  7. this.appendStatementInput('STACK');
  8. this.setTooltip('');
  9. this.contextMenu = false;
  10. }
  11. };
  12. Blockly.Blocks['serial_write_data_create_with_item'] = {
  13. init: function () {
  14. this.setColour("#22b845");
  15. this.appendDummyInput()
  16. .appendField(Blockly.Msg.serialcomm_write_data);
  17. this.setPreviousStatement(true);
  18. this.setNextStatement(true);
  19. this.setTooltip('');
  20. this.contextMenu = false;
  21. }
  22. };
  23. Blockly.Blocks['serial_write_data'] = {
  24. init: function () {
  25. this.appendDummyInput()
  26. .appendField(new Blockly.FieldImage("blockly/media/uart_serial_send_header.png", 110, 35, { alt: "*", flipRtl: "FALSE" }));
  27. this.appendDummyInput()
  28. .appendField(Blockly.Msg.serialcomm_write_title_text);
  29. this.appendDummyInput()
  30. .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
  31. .appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps")
  32. .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
  33. this.appendValueInput("ADD1")
  34. .setCheck(null)
  35. // .appendField("Field 0");
  36. this.itemCount_ = 1;
  37. this.setColour("#22b845");
  38. this.setPreviousStatement(true, null);
  39. this.setNextStatement(true, null);
  40. var thisBlock = this;
  41. this.setTooltip(function () {
  42. var mode = thisBlock.getFieldValue('type');
  43. var TOOLTIPS = {
  44. 'iot': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
  45. 'ai': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
  46. };
  47. return TOOLTIPS[mode];
  48. });
  49. this.setHelpUrl("");
  50. },
  51. };
  52. Blockly.Python['serial_write_data'] = function (block) {
  53. // var api = block.getFieldValue('t_api');
  54. var number_uart_bps = block.getFieldValue('uart_bps');
  55. var ai_uart_data = Blockly.Python.valueToCode(this, "ADD1", Blockly.Python.ORDER_NONE);
  56. Blockly.Python.definitions_['v831_import_serial'] = `import serial
  57. SERIAL = serial.Serial("/dev/ttyS1",${number_uart_bps})
  58. `
  59. var code = `uart_data = bytes(str(${ai_uart_data})+"\\n","utf-8")
  60. SERIAL.write(uart_data)
  61. `
  62. return code;
  63. };
  64. Blockly.Blocks['serial_write_data_coco'] = {
  65. init: function () {
  66. this.appendDummyInput()
  67. .appendField(new Blockly.FieldImage("blockly/media/cocorobo.png", 65, 65, { alt: "*", flipRtl: "FALSE" }));
  68. // this.appendDummyInput()
  69. // .appendField(Blockly.Msg.serialcomm_write_title_text_coco);
  70. this.appendDummyInput()
  71. .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
  72. .appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps");
  73. this.appendDummyInput()
  74. .appendField(Blockly.Msg.serial_send_data_on_send_data_cocorobo);
  75. /*this.appendValueInput("first_input")
  76. .setCheck(null)
  77. .appendField("Field 0");*/
  78. this.itemCount_ = 1;
  79. this.updateShape_();
  80. this.setMutator(new Blockly.Mutator(['serial_write_data_create_with_item']));
  81. this.setColour("#22b845");
  82. this.setPreviousStatement(true, null);
  83. this.setNextStatement(true, null);
  84. var thisBlock = this;
  85. this.setTooltip(function () {
  86. var mode = thisBlock.getFieldValue('type');
  87. var TOOLTIPS = {
  88. 'iot': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
  89. 'ai': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
  90. };
  91. return TOOLTIPS[mode];
  92. });
  93. this.setHelpUrl("");
  94. },
  95. mutationToDom: function () {
  96. var container = document.createElement('mutation');
  97. container.setAttribute('items', this.itemCount_);
  98. return container;
  99. },
  100. domToMutation: function (xmlElement) {
  101. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  102. this.updateShape_();
  103. },
  104. decompose: function (workspace) {
  105. var containerBlock = workspace.newBlock('serial_write_data_create_with_container');
  106. containerBlock.initSvg();
  107. var connection = containerBlock.getInput('STACK').connection;
  108. for (var i = 0; i < this.itemCount_; i++) {
  109. var itemBlock = workspace.newBlock('serial_write_data_create_with_item');
  110. itemBlock.initSvg();
  111. connection.connect(itemBlock.previousConnection);
  112. connection = itemBlock.nextConnection;
  113. }
  114. return containerBlock;
  115. },
  116. compose: function (containerBlock) {
  117. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  118. // Count number of inputs.
  119. var connections = [];
  120. while (itemBlock) {
  121. connections.push(itemBlock.valueConnection_);
  122. itemBlock = itemBlock.nextConnection &&
  123. itemBlock.nextConnection.targetBlock();
  124. }
  125. // Disconnect any children that don't belong.
  126. for (var i = 0; i < this.itemCount_; i++) {
  127. var connection = this.getInput('ADD' + i).connection.targetConnection;
  128. if (connection && connections.indexOf(connection) == -1) {
  129. connection.disconnect();
  130. }
  131. }
  132. this.itemCount_ = connections.length;
  133. this.updateShape_();
  134. // Reconnect any child blocks.
  135. for (var i = 0; i < this.itemCount_; i++) {
  136. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  137. }
  138. },
  139. saveConnections: function (containerBlock) {
  140. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  141. var i = 0;
  142. while (itemBlock) {
  143. var input = this.getInput('ADD' + i);
  144. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  145. i++;
  146. itemBlock = itemBlock.nextConnection &&
  147. itemBlock.nextConnection.targetBlock();
  148. }
  149. },
  150. updateShape_: function () {
  151. for (var i = 0; i < this.itemCount_; i++) {
  152. if (!this.getInput('ADD' + i)) {
  153. var input = this.appendValueInput('ADD' + i);
  154. input.appendField(Blockly.Msg.serialcomm_write_item_first + i + Blockly.Msg.serialcomm_write_item_last);
  155. //input.appendField("資料 " + (i + 1) + ":");
  156. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  157. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  158. }
  159. }
  160. while (this.getInput('ADD' + i)) {
  161. this.removeInput('ADD' + i);
  162. i++;
  163. }
  164. },
  165. };
  166. Blockly.Python['serial_write_data_coco'] = function (block) {
  167. // var api = block.getFieldValue('t_api');
  168. var number_uart_bps = block.getFieldValue('uart_bps');
  169. var item_value = '';
  170. var ai_uart_data = "";
  171. for (var n = 0; n < this.itemCount_; n++) {
  172. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  173. ai_uart_data += 'str(' + item_value + ') + \"|\" + ';
  174. }
  175. var ai_uart_data_final = ai_uart_data + "";
  176. Blockly.Python.definitions_['v831_import_serial'] = `import serial
  177. SERIAL = serial.Serial("/dev/ttyS1",${number_uart_bps})
  178. `
  179. var code = `uart_data = bytes("SOF|"+${ai_uart_data_final.slice(0, -8)}+"|\\r\\n","utf-8")
  180. SERIAL.write(uart_data)
  181. `
  182. return code;
  183. };
  184. Blockly.Blocks['serial_read_data_setup'] = {
  185. init: function () {
  186. let types = getLocalStorage("type")
  187. this.appendDummyInput()
  188. .appendField(new Blockly.FieldImage("blockly/media/uart_serial_read_header.png", 110, 35, { alt: "*", flipRtl: "FALSE" }));
  189. this.appendDummyInput()
  190. .appendField(Blockly.Msg.serialcomm_read_setup_title_text + Blockly.Msg.serialcomm_read_setup_title_text_1);
  191. this.appendDummyInput()
  192. .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
  193. .appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps")
  194. .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
  195. this.setColour("#22b845");
  196. this.setPreviousStatement(true, null);
  197. this.setNextStatement(true, null);
  198. var thisBlock = this;
  199. this.setTooltip(function () {
  200. var mode = thisBlock.getFieldValue('type');
  201. var TOOLTIPS = {
  202. 'iot': Blockly.Msg.Serial_Read_Data_Setup_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
  203. 'ai': Blockly.Msg.Serial_Read_Data_Setup_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
  204. };
  205. return TOOLTIPS[mode];
  206. });
  207. this.setHelpUrl("");
  208. }
  209. };
  210. Blockly.Python['serial_read_data_setup'] = function (block) {
  211. var number_uart_bps = block.getFieldValue('uart_bps');
  212. // TODO: Assemble Python into code variable.
  213. Blockly.Python.definitions_['v831_import_serial'] = `import serial
  214. SERIAL = serial.Serial("/dev/ttyS1",${number_uart_bps})
  215. `;
  216. Blockly.Python.definitions_['_read_serial_data'] = `def _read_serial_data(read_data,split, index):
  217. if read_data != None:
  218. read_str = ""
  219. try:
  220. read_str = str(read_data.decode("utf-8")).split(split)[index]
  221. except:
  222. read_str = str(read_data).split(split)[index]
  223. return read_str
  224. `
  225. var code = "";
  226. return code;
  227. };
  228. Blockly.Blocks['serial_read_data_setup_arduino'] = {
  229. init: function () {
  230. this.appendDummyInput()
  231. .appendField(new Blockly.FieldImage("blockly/media/uart_serial_read_header.png", 110, 35, { alt: "*", flipRtl: "FALSE" }));
  232. this.appendDummyInput()
  233. .appendField(Blockly.Msg.serialcomm_read_setup_title_text_arduino);
  234. this.appendDummyInput()
  235. .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
  236. .appendField(new Blockly.FieldNumber(9600, 0, 500000, 1), "uart_bps")
  237. .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
  238. this.setColour("#22b845");
  239. this.setPreviousStatement(true, null);
  240. this.setNextStatement(true, null);
  241. var thisBlock = this;
  242. this.setTooltip(function () {
  243. var mode = thisBlock.getFieldValue('type');
  244. var TOOLTIPS = {
  245. 'iot': Blockly.Msg.Serial_Read_Data_Setup_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
  246. 'ai': Blockly.Msg.Serial_Read_Data_Setup_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
  247. };
  248. return TOOLTIPS[mode];
  249. });
  250. this.setHelpUrl("");
  251. }
  252. };
  253. Blockly.Python['serial_read_data_setup_arduino'] = function (block) {
  254. var number_uart_bps = block.getFieldValue('uart_bps');
  255. // TODO: Assemble Python into code variable.
  256. Blockly.Python.definitions_["v831_import_os"] = `import os`
  257. Blockly.Python.definitions_['v831_import_serial_ardino_read'] = `import serial
  258. SERIALPATH = "/dev/ttyUSB0" if os.path.exists("/dev/ttyUSB0") else "/dev/ttyACM0"
  259. SERIAL = serial.Serial(SERIALPATH,${number_uart_bps})
  260. `;
  261. var code = "";
  262. return code;
  263. };
  264. Blockly.Blocks['serial_read_data_all'] = {
  265. init: function () {
  266. this.appendDummyInput()
  267. .appendField(Blockly.Msg.Serial_Read_Data_all)
  268. // .appendField(new Blockly.FieldDropdown([
  269. // ["serial_data", "serial_data"]
  270. // ]), "TYPE");
  271. this.setOutput(true, null);
  272. this.setColour("#22b845");
  273. this.setTooltip(Blockly.Msg.Serial_Read_Data_all);
  274. this.setHelpUrl("");
  275. }
  276. }
  277. Blockly.Python['serial_read_data_all'] = function (block) {
  278. var code = `SERIAL.readline().decode("UTF-8","ignore").strip()`;
  279. return [code, Blockly.Python.ORDER_NONE];
  280. };
  281. Blockly.Blocks['serial_read_data_all_usb'] = {
  282. init: function () {
  283. this.appendDummyInput()
  284. .appendField(Blockly.Msg.Serial_Read_Data_all_usb)
  285. // .appendField(new Blockly.FieldDropdown([
  286. // ["serial_data", "serial_data"]
  287. // ]), "TYPE");
  288. this.setOutput(true, null);
  289. this.setColour("#22b845");
  290. this.setTooltip(Blockly.Msg.Serial_Read_Data_all);
  291. this.setHelpUrl("");
  292. }
  293. }
  294. Blockly.Python['serial_read_data_all_usb'] = function (block) {
  295. var code = `SERIAL.readline().decode("UTF-8","ignore").strip()`;
  296. return [code, Blockly.Python.ORDER_NONE];
  297. };
  298. Blockly.Blocks['serial_read_data'] = {
  299. init: function () {
  300. this.appendDummyInput()
  301. .appendField(Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_GET_TEXT)
  302. .appendField(new Blockly.FieldDropdown([
  303. ["0", "0"],
  304. ["1", "1"],
  305. ["2", "2"],
  306. ["3", "3"],
  307. ["4", "4"],
  308. ["5", "5"],
  309. ["6", "6"],
  310. ["7", "7"],
  311. ["8", "8"],
  312. ["9", "9"],
  313. ["10", "10"],
  314. ["11", "11"],
  315. ]), "INDEX")
  316. .appendField(Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_TYPE_TEXT)
  317. // .appendField(new Blockly.FieldDropdown([
  318. // [Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_TYPE_STRING, "0"],
  319. // [Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_TYPE_INTEGER, "1"],
  320. // [Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_TYPE_FLOAT, "2"],
  321. // ]), "TYPE");
  322. this.setOutput(true, null);
  323. this.setColour("#22b845");
  324. this.setTooltip(Blockly.Msg.Serial_Read_Data_TOOLTIP);
  325. this.setHelpUrl("");
  326. }
  327. }
  328. Blockly.Python['serial_read_data'] = function (block) {
  329. var index = parseInt(block.getFieldValue("INDEX")) + 1;
  330. // var type = block.getFieldValue("TYPE")
  331. // var type_1 = block.getFieldValue("TYPE_1")
  332. Blockly.Python.definitions_['_read_serial_data'] = `def _read_serial_data(read_data,split, index):
  333. if read_data != None:
  334. read_str = ""
  335. try:
  336. read_str = str(read_data.decode("utf-8")).split(split)[index]
  337. except:
  338. read_str = str(read_data).split(split)[index]
  339. return read_str
  340. `
  341. var code = `_read_serial_data(SERIAL.readline().decode("UTF-8","ignore").strip(),"|",${index})`;
  342. return [code, Blockly.Python.ORDER_NONE];
  343. }
  344. Blockly.Blocks['serial_send_data_to_microbit'] = {
  345. init: function () {
  346. this.appendDummyInput()
  347. .appendField(new Blockly.FieldImage("blockly/media/microbit_data_send_header.png", 90, 70, { alt: "*", flipRtl: "FALSE" }));
  348. this.appendDummyInput()
  349. .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
  350. .appendField("115200")
  351. .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
  352. this.appendValueInput("NAME")
  353. // .appendField(Blockly.Msg.serial_send_data_on)
  354. .appendField(Blockly.Msg.serial_send_data_on_send_data);
  355. this.setInputsInline(false);
  356. this.setColour("#22b845");
  357. this.setPreviousStatement(true, null);
  358. this.setNextStatement(true, null);
  359. this.setHelpUrl("");
  360. this.setTooltip(Blockly.Msg.serial_send_data_on_send_data);
  361. }
  362. };
  363. Blockly.Python['serial_send_data_to_microbit'] = function (block) {
  364. var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC) || "";
  365. // TODO: Assemble Python into code variable.
  366. Blockly.Python.definitions_["831_import_serial"] = `import serial
  367. SERIAL = serial.Serial("/dev/ttyS1",115200)`
  368. var code = `uart_data = bytes(${value_name}+"\\n","utf-8")
  369. SERIAL.write(uart_data)
  370. `;
  371. return code
  372. };
  373. Blockly.Blocks['serial_send_data_to_arduino'] = {
  374. init: function () {
  375. this.appendDummyInput()
  376. .appendField(new Blockly.FieldImage("blockly/media/arduino.png", 90, 70, { alt: "*", flipRtl: "FALSE" }));
  377. this.appendDummyInput()
  378. .appendField(Blockly.Msg.serialcomm_write_title_text_arduino);
  379. this.appendDummyInput()
  380. .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
  381. .appendField(new Blockly.FieldNumber(9600, 0, 500000, 1), "uart_bps")
  382. .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
  383. this.appendValueInput("NAME")
  384. // .appendField(Blockly.Msg.serial_send_data_on)
  385. .appendField(Blockly.Msg.serial_send_data_on_send_data_control_panel_arduino);
  386. this.setInputsInline(false);
  387. this.setColour("#22b845");
  388. this.setPreviousStatement(true, null);
  389. this.setNextStatement(true, null);
  390. this.setHelpUrl("");
  391. this.setTooltip(Blockly.Msg.serial_send_data_on_send_data);
  392. }
  393. };
  394. Blockly.Python['serial_send_data_to_arduino'] = function (block) {
  395. var number_uart_bps = block.getFieldValue('uart_bps');
  396. var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC) || "";
  397. // TODO: Assemble Python into code variable.
  398. Blockly.Python.definitions_["v831_import_os"] = `import os`
  399. Blockly.Python.definitions_["v831_import_serial_ardino_read"] = `import serial
  400. SERIALPATH = "/dev/ttyUSB0" if os.path.exists("/dev/ttyUSB0") else "/dev/ttyACM0"
  401. SERIAL = serial.Serial(SERIALPATH,${number_uart_bps})`
  402. var code = `uart_data = bytes(${value_name}+"\\n","utf-8")
  403. SERIAL.write(uart_data)
  404. `;
  405. return code
  406. };
  407. /*
  408. ____
  409. / ___| ___ _ ____ _____
  410. \___ \ / _ \ '__\ \ / / _ \
  411. ___) | __/ | \ V / (_) |
  412. |____/ \___|_| \_/ \___/
  413. */
  414. var SERVO_BLOCK_COLOR = "#5bb2d6";
  415. var ai_servo_timer = [
  416. "Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM",
  417. "Timer.TIMER0, Timer.CHANNEL1, mode=Timer.MODE_PWM",
  418. "Timer.TIMER0, Timer.CHANNEL2, mode=Timer.MODE_PWM",
  419. "Timer.TIMER0, Timer.CHANNEL3, mode=Timer.MODE_PWM",
  420. "Timer.TIMER1, Timer.CHANNEL0, mode=Timer.MODE_PWM",
  421. "Timer.TIMER1, Timer.CHANNEL1, mode=Timer.MODE_PWM",
  422. "Timer.TIMER1, Timer.CHANNEL2, mode=Timer.MODE_PWM",
  423. "Timer.TIMER1, Timer.CHANNEL3, mode=Timer.MODE_PWM",
  424. "Timer.TIMER2, Timer.CHANNEL0, mode=Timer.MODE_PWM",
  425. "Timer.TIMER2, Timer.CHANNEL1, mode=Timer.MODE_PWM",
  426. "Timer.TIMER2, Timer.CHANNEL2, mode=Timer.MODE_PWM",
  427. "Timer.TIMER2, Timer.CHANNEL3, mode=Timer.MODE_PWM",
  428. "Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM"
  429. // "Timer.TIMER0, Timer.CHANNEL1, mode=Timer.MODE_PWM"
  430. ];
  431. var ai_servo_pin = [
  432. // '0',
  433. '1', '2', '3', '13', '14', '15', '17', '21', '22', '23', '24', '29', '30']
  434. Blockly.Blocks['extension_servo_setup_on_ai'] = {
  435. init: function () {
  436. this.appendDummyInput()
  437. .appendField(new Blockly.FieldImage("blockly/media/esp32_servo_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  438. this.appendDummyInput()
  439. .appendField(Blockly.Msg.servo_setup_ai);
  440. this.setInputsInline(false);
  441. this.setPreviousStatement(true, null);
  442. this.setNextStatement(true, null);
  443. this.setColour(servoCloor);
  444. this.setTooltip(Blockly.Msg.extension_servo_setup_on_ai_TOOLTIP);
  445. this.setHelpUrl("");
  446. }
  447. };
  448. Blockly.Python['extension_servo_setup_on_ai'] = function (block) {
  449. Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
  450. Blockly.Python.definitions_['v831_import_time'] = `import time`
  451. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  452. sys.path.append("/root/")`
  453. Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
  454. // Blockly.Python.definitions_["v831_set_servo_S1_S2"] = `S1= multiFuncGpio(0,1)
  455. // S2= multiFuncGpio(1,1)`
  456. var code = '';
  457. return code;
  458. };
  459. Blockly.Blocks['extension_servo_write_on_ai'] = {
  460. init: function () {
  461. this.appendDummyInput()
  462. .appendField(Blockly.Msg.servo_set_gpio_ai)
  463. .appendField(new Blockly.FieldDropdown([
  464. ["S1", "S1"],
  465. ["S2", "S2"],
  466. ["P0", "P0"],
  467. ["P1", "P1"],
  468. ["P2", "P2"],
  469. ["P3", "P3"]
  470. ]), "gpio");
  471. this.appendValueInput("degree")
  472. .setCheck(null)
  473. .appendField(Blockly.Msg.servo_rotate_to_ai);
  474. this.appendDummyInput()
  475. .appendField(Blockly.Msg.servo_degree_ai);
  476. this.setInputsInline(true);
  477. this.setPreviousStatement(true, null);
  478. this.setNextStatement(true, null);
  479. this.setColour(servoCloor);
  480. this.setTooltip(Blockly.Msg.extension_servo_write_on_ai_TOOLTIP);
  481. this.setHelpUrl("");
  482. }
  483. };
  484. let extension_servo_write_on_ai_list = []
  485. Blockly.Python['extension_servo_write_on_ai'] = function (block) {
  486. var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC);
  487. var value_gpio = block.getFieldValue('gpio');
  488. Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
  489. Blockly.Python.definitions_['v831_import_time'] = `import time`
  490. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  491. sys.path.append("/root/")`
  492. let code = ""
  493. if (value_gpio == "S1" || value_gpio == "S2") {
  494. Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
  495. const allBlocks = getBlocksByTypeName("extension_servo_write_on_ai")
  496. let onedegree = allBlocks[0].innerText
  497. let degree = 0
  498. let degree1 = 0
  499. try {
  500. if (value_gpio == "S1") {
  501. degree = onedegree.indexOf("S1") == 0 ? allBlocks[0].children[1].innerText : allBlocks[Math.floor((onedegree.indexOf("S1")) / 4)].children[1].innerText
  502. } else {
  503. degree1 = onedegree.indexOf("S2") == 0 ? allBlocks[0].children[1].innerText : allBlocks[Math.floor((onedegree.indexOf("S2")) / 4)].children[1].innerText
  504. }
  505. } catch (e) {
  506. console.log("error", e)
  507. }
  508. Blockly.Python.definitions_["v831_servo_init" + value_gpio] = `${value_gpio}= multiFuncGpio(${value_gpio[1] - 1},1)
  509. ${value_gpio}.servoCtrl(${allBlocks.length > 1 ? value_gpio == "S1" ? degree : degree1 : value_degree})`
  510. code = `${value_gpio}.servoCtrl(${value_degree})\n`
  511. } else {
  512. Blockly.Python.definitions_['v831_import_CocoPi_extServo'] = `from CocoPi import extServo`;
  513. Blockly.Python.definitions_["v831_servo_init_" + value_gpio] = `${value_gpio} = extServo(${value_gpio[1]})`
  514. code = `${value_gpio}.position(${value_degree})\n`
  515. }
  516. return code;
  517. };
  518. let servoCloor = "#386dc8"
  519. var iot_servo_pin = [
  520. '4', '12', '13', '14', '15', '16', '17', '21', '22', '25', '27']
  521. Blockly.Blocks['esp32_main_controller_servo_setup'] = {
  522. init: function () {
  523. this.appendDummyInput()
  524. .appendField(new Blockly.FieldImage("blockly/media/esp32_servo_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  525. this.appendDummyInput()
  526. .appendField(Blockly.Msg.servo_setup);
  527. this.setPreviousStatement(true, null);
  528. this.setNextStatement(true, null);
  529. this.setColour(servoCloor);
  530. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Servo_Setup_TOOLTIP);
  531. this.setHelpUrl("");
  532. }
  533. };
  534. Blockly.Python['esp32_main_controller_servo_setup'] = function (block) {
  535. // TODO: Assemble Python into code variable.
  536. Blockly.Python.definitions_['definition_servo_setup'] = '' +
  537. 'from machine import Pin, PWM\n' +
  538. 'import time\n' +
  539. '';
  540. var code = '\n';
  541. return code;
  542. };
  543. Blockly.Blocks['esp32_main_controller_servo_set'] = {
  544. init: function () {
  545. this.appendDummyInput()
  546. .appendField(Blockly.Msg.servo_set_gpio)
  547. .appendField(new Blockly.FieldDropdown([
  548. ["4", "0"],
  549. ["12", "1"],
  550. ["13", "2"],
  551. ["14", "3"],
  552. ["15", "4"],
  553. ["16", "5"],
  554. ["17", "6"],
  555. ["21", "7"],
  556. ["22", "8"],
  557. ["25", "9"],
  558. ["27", "10"]
  559. ]), "gpio");
  560. this.appendDummyInput()
  561. .appendField(Blockly.Msg.servo_rotate_to);
  562. this.appendValueInput("degree")
  563. .setCheck(null);
  564. this.appendDummyInput()
  565. .appendField(Blockly.Msg.servo_degree);
  566. this.setInputsInline(true);
  567. this.setPreviousStatement(true, null);
  568. this.setNextStatement(true, null);
  569. this.setColour(servoCloor);
  570. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Servo_Set_TOOLTIP);
  571. this.setHelpUrl("");
  572. }
  573. };
  574. Blockly.Python['esp32_main_controller_servo_set'] = function (block) {
  575. var value_gpio = block.getFieldValue('gpio');
  576. var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC);
  577. // TODO: Assemble Python into code variable.
  578. Blockly.Python.addVariable('S' + iot_servo_pin[value_gpio], 'S' + iot_servo_pin[value_gpio] + ' = PWM(Pin(' + iot_servo_pin[value_gpio] + '), freq=50, duty=0)', true);
  579. Blockly.Python.definitions_['definition_servo_set'] = '' +
  580. 'def Servo(servo,angle):\n' +
  581. ' angle = angle - 90\n' +
  582. ' servo.freq(50)\n' +
  583. ' servo.duty(int(((angle+90)*2/180+0.5)/20*1023))\n' +
  584. ' time.sleep_ms(22)\n' +
  585. '';
  586. var code = '' +
  587. 'Servo(S' + iot_servo_pin[value_gpio] + ',' + value_degree + ')\n' +
  588. '';
  589. return code;
  590. };
  591. /*
  592. __ __ _
  593. | \/ | ___ | |_ ___ _ __
  594. | |\/| |/ _ \| __/ _ \| '__|
  595. | | | | (_) | || (_) | |
  596. |_| |_|\___/ \__\___/|_|
  597. */
  598. var MOTOR_BLOCK_COLOR = "#0000FF";
  599. Blockly.Blocks['esp32_motor_setup'] = {
  600. init: function () {
  601. this.appendDummyInput()
  602. .appendField(new Blockly.FieldImage("blockly/media/motor_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  603. this.appendDummyInput()
  604. .appendField(Blockly.Msg.x_motor_set_motor)
  605. this.setInputsInline(false);
  606. this.setPreviousStatement(true, null);
  607. this.setNextStatement(true, null);
  608. this.setColour(MOTOR_BLOCK_COLOR);
  609. this.setTooltip(Blockly.Msg.Esp32_Motor_Setup_TOOLTIP);
  610. this.setHelpUrl("");
  611. }
  612. };
  613. Blockly.Python['esp32_motor_setup'] = function (block) {
  614. // TODO: Assemble Python into code variable.
  615. Blockly.Python.definitions_['esp32_controller_set_motor'] = '' +
  616. 'import pca9685, machine, motor\n' +
  617. '\n' +
  618. '_iot_motor_i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))\n' +
  619. '_iot_motor = motor.DCMotors(_iot_motor_i2c)';
  620. var code = '';
  621. return code;
  622. };
  623. Blockly.Blocks['esp32_motor_run'] = {
  624. init: function () {
  625. this.appendDummyInput()
  626. .appendField(Blockly.Msg.x_motor_set_motor_turn)
  627. .appendField(new Blockly.FieldDropdown([
  628. ["A", "0"],
  629. ["B", "1"],
  630. ["C", "3"],
  631. ["D", "2"]
  632. ]), "motor_type")
  633. .appendField(Blockly.Msg.x_motor_set_motor_speed);
  634. this.appendValueInput("speed")
  635. .setCheck(null);
  636. this.appendDummyInput()
  637. .appendField(Blockly.Msg.x_motor_exec)
  638. // .appendField(new Blockly.FieldDropdown([
  639. // [Blockly.Msg.x_motor_cw, "pos"],
  640. // [Blockly.Msg.x_motor_acw, "neg"]
  641. // ]), "direction")
  642. .appendField(Blockly.Msg.x_motor_set_motor_turns);
  643. this.setInputsInline(true);
  644. this.setPreviousStatement(true, null);
  645. this.setNextStatement(true, null);
  646. this.setColour(MOTOR_BLOCK_COLOR);
  647. this.setTooltip(Blockly.Msg.Esp32_Motor_Run_TOOLTIP);
  648. this.setHelpUrl("");
  649. }
  650. };
  651. Blockly.Python['esp32_motor_run'] = function (block) {
  652. var dropdown_motor_type = block.getFieldValue('motor_type');
  653. var value_speed = Blockly.Python.valueToCode(block, 'speed', Blockly.Python.ORDER_ATOMIC);
  654. // var dropdown_direction = block.getFieldValue('direction');
  655. var code;
  656. if (value_speed.indexOf("-") == -1) {
  657. code = '_iot_motor.speed(' + dropdown_motor_type + ', (' + value_speed + '*16))\n';
  658. } else {
  659. code = '_iot_motor.speed(' + dropdown_motor_type + ', -(abs' + value_speed + '*16))\n';
  660. }
  661. return code;
  662. };
  663. Blockly.Blocks['ai_motor_setup'] = {
  664. init: function () {
  665. this.appendDummyInput()
  666. .appendField(new Blockly.FieldImage("blockly/media/motor_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  667. this.appendDummyInput()
  668. .appendField(Blockly.Msg.x_motor_set_motor_ai)
  669. this.setInputsInline(false);
  670. this.setPreviousStatement(true, null);
  671. this.setNextStatement(true, null);
  672. this.setColour(servoCloor);
  673. this.setTooltip(Blockly.Msg.ai_motor_setup_TOOLTIP);
  674. this.setHelpUrl("");
  675. }
  676. };
  677. Blockly.Python['ai_motor_setup'] = function (block) {
  678. // TODO: Assemble Python into code variable.
  679. Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
  680. Blockly.Python.definitions_['v831_import_time'] = `import time`
  681. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  682. sys.path.append("/root/")`
  683. Blockly.Python.definitions_['v831_import_CocoPi_stm8s_init'] = `from CocoPi import stm8s
  684. iic_slaver=stm8s()
  685. iic_slaver.clear()
  686. del iic_slaver`;
  687. Blockly.Python.definitions_['v831_import_CocoPi_stm8s'] = `from CocoPi import dcMotor`;
  688. // Blockly.Python.definitions_["v831_motor_init"] = `M1=dcMotor(1)
  689. // M2=dcMotor(2)`
  690. var code = '';
  691. return code;
  692. };
  693. Blockly.Blocks['ai_motor_run'] = {
  694. init: function () {
  695. this.appendDummyInput()
  696. .appendField(Blockly.Msg.x_motor_set_motor_turn_ai)
  697. .appendField(new Blockly.FieldDropdown([
  698. ["M1", "M1"],
  699. ["M2", "M2"],
  700. ["M3", "C"],
  701. ["M4", "D"],
  702. ["M5", "E"],
  703. ["M6", "F"]
  704. ]), "motor_type")
  705. .appendField(Blockly.Msg.x_motor_set_motor_speed_ai);
  706. this.appendValueInput("speed")
  707. .setCheck(null);
  708. this.appendDummyInput()
  709. .appendField(Blockly.Msg.x_motor_exec_ai)
  710. .appendField(new Blockly.FieldDropdown([
  711. [Blockly.Msg.x_motor_cw_ai, "pos"],
  712. [Blockly.Msg.x_motor_acw_ai, "neg"]
  713. ]), "direction")
  714. .appendField(Blockly.Msg.x_motor_set_motor_turns_ai);
  715. this.setInputsInline(true);
  716. this.setPreviousStatement(true, null);
  717. this.setNextStatement(true, null);
  718. this.setColour(servoCloor);
  719. this.setTooltip(Blockly.Msg.ai_motor_run_TOOLTIP);
  720. this.setHelpUrl("");
  721. }
  722. };
  723. Blockly.Python['ai_motor_run'] = function (block) {
  724. var dropdown_motor_type = block.getFieldValue('motor_type');
  725. var value_speed = Blockly.Python.valueToCode(block, 'speed', Blockly.Python.ORDER_ATOMIC);
  726. var dropdown_direction = block.getFieldValue('direction');
  727. // TODO: Assemble Python into code variable.
  728. Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
  729. Blockly.Python.definitions_['v831_import_time'] = `import time`
  730. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  731. sys.path.append("/root/")`
  732. var code = ''
  733. if (dropdown_motor_type == "M1" || dropdown_motor_type == "M2") {
  734. Blockly.Python.definitions_['v831_import_CocoPi_stm8s_init'] = `from CocoPi import stm8s
  735. iic_slaver=stm8s()
  736. iic_slaver.clear()
  737. del iic_slaver`;
  738. Blockly.Python.definitions_['v831_import_CocoPi_stm8s'] = `from CocoPi import dcMotor`;
  739. Blockly.Python.definitions_["v831dcMotorCtrlFun" + dropdown_motor_type] = `${dropdown_motor_type} = dcMotor(${dropdown_motor_type.slice(1, 2)})`
  740. if (dropdown_direction == 'pos') {
  741. code = `${dropdown_motor_type}.dcMotorCtrl(1,${value_speed})\n`
  742. }
  743. else {
  744. code = `${dropdown_motor_type}.dcMotorCtrl(0,${value_speed})\n`
  745. }
  746. } else {
  747. Blockly.Python.definitions_['v831_import_CocoPi_extDcMotor'] = `from CocoPi import extDcMotor`;
  748. Blockly.Python.definitions_["v831dcMotorCtrlFun" + dropdown_motor_type] = `${dropdown_motor_type} = extDcMotor("${dropdown_motor_type}")`
  749. if (dropdown_direction == 'pos') {
  750. code = `${dropdown_motor_type}.speedControl(${value_speed})\n`
  751. }
  752. else {
  753. code = `${dropdown_motor_type}.speedControl(-${value_speed})\n`
  754. }
  755. }
  756. return code;
  757. };
  758. // ai_motor_stop
  759. Blockly.Blocks['ai_motor_stop'] = {
  760. init: function () {
  761. this.appendDummyInput()
  762. .appendField(Blockly.Msg.x_motor_set_motor_turn_ai)
  763. .appendField(new Blockly.FieldDropdown([
  764. ["M1", "M1"],
  765. ["M2", "M2"],
  766. ["M3", "C"],
  767. ["M4", "D"],
  768. ["M5", "E"],
  769. ["M6", "F"]
  770. ]), "motor_type")
  771. .appendField(Blockly.Msg.x_motor_set_motor_speed_stop);
  772. this.setInputsInline(true);
  773. this.setPreviousStatement(true, null);
  774. this.setNextStatement(true, null);
  775. this.setColour(servoCloor);
  776. this.setTooltip(Blockly.Msg.x_motor_set_motor_speed_stop);
  777. this.setHelpUrl("");
  778. }
  779. };
  780. Blockly.Python['ai_motor_stop'] = function (block) {
  781. var dropdown_motor_type = block.getFieldValue('motor_type');
  782. // TODO: Assemble Python into code variable.
  783. Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
  784. Blockly.Python.definitions_['v831_import_time'] = `import time`
  785. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  786. sys.path.append("/root/")`
  787. var code = ''
  788. if (dropdown_motor_type == "M1" || dropdown_motor_type == "M2") {
  789. Blockly.Python.definitions_['v831_import_CocoPi_stm8s_init'] = `from CocoPi import stm8s
  790. iic_slaver=stm8s()
  791. iic_slaver.clear()
  792. del iic_slaver`;
  793. Blockly.Python.definitions_['v831_import_CocoPi_stm8s'] = `from CocoPi import dcMotor`;
  794. Blockly.Python.definitions_["v831dcMotorCtrlFun" + dropdown_motor_type] = `${dropdown_motor_type} = dcMotor(${dropdown_motor_type.slice(1, 2)})`
  795. code = `${dropdown_motor_type}.dcMotorCtrl(0,0)\n`
  796. } else {
  797. Blockly.Python.definitions_['v831_import_CocoPi_extDcMotor'] = `from CocoPi import extDcMotor`;
  798. Blockly.Python.definitions_["v831dcMotorCtrlFun" + dropdown_motor_type] = `${dropdown_motor_type} = extDcMotor("${dropdown_motor_type}")`
  799. code = `${dropdown_motor_type}.speedControl(0)\n`
  800. }
  801. return code;
  802. };
  803. /*
  804. _ _____ ____ __ __ _ _
  805. | | | ____| _ \ | \/ | __ _| |_ _ __(_)_ __
  806. | | | _| | | | | | |\/| |/ _` | __| '__| \ \/ /
  807. | |___| |___| |_| | | | | | (_| | |_| | | |> <
  808. |_____|_____|____/ |_| |_|\__,_|\__|_| |_/_/\_\
  809. */
  810. let AILEDCOLOR = "#e8795b"
  811. Blockly.Blocks['ai_led_matrix_setup'] = {
  812. init: function () {
  813. this.appendDummyInput()
  814. .appendField(new Blockly.FieldImage("blockly/media/led_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  815. this.appendDummyInput()
  816. .appendField(Blockly.Msg.x_led_matrix_setup_ai);
  817. this.appendDummyInput()
  818. .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_1)
  819. .appendField(new Blockly.FieldNumber(50, 0, 255, 1), "brightness")
  820. .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_range);
  821. this.setInputsInline(false);
  822. this.setPreviousStatement(true, null);
  823. this.setNextStatement(true, null);
  824. this.setColour(AILEDCOLOR);
  825. this.setTooltip(Blockly.Msg.ai_led_matrix_setup_TOOLTIP);
  826. this.setHelpUrl("");
  827. }
  828. };
  829. Blockly.Python['ai_led_matrix_setup'] = function (block) {
  830. var number_brightness = block.getFieldValue('brightness');
  831. Blockly.Python.definitions_['ai_controller_led_setup'] = '' +
  832. 'from modules import ws2812\n' +
  833. '\n' +
  834. '_ai_led_matrix_total = 36\n' +
  835. '_ai_led_matrix = ws2812(24,_ai_led_matrix_total)\n' +
  836. '_ai_brightness = ' + number_brightness + '\n' +
  837. '\n' +
  838. 'def rgba_to_rgb_conversion(red, green, blue):\n' +
  839. ' if _ai_brightness <= 255:\n' +
  840. ' alpha = _ai_brightness / 255\n' +
  841. ' elif _ai_brightness > 255:\n' +
  842. ' alpha = 255 / 255\n' +
  843. ' elif _ai_brightness < 0:\n' +
  844. ' alpha = 0 / 255\n' +
  845. ' final_red = int((1 - alpha) * 0 + alpha * red)\n' +
  846. ' final_green = int((1 - alpha) * 0 + alpha * green)\n' +
  847. ' final_blue = int((1 - alpha) * 0 + alpha * blue)\n' +
  848. ' final = (final_red, final_green, final_blue)\n' +
  849. ' return final\n' +
  850. '';
  851. // TODO: Assemble Python into code variable.
  852. var code = '';
  853. return code;
  854. };
  855. Blockly.Blocks['ai_led_matrix_set_bright'] = {
  856. init: function () {
  857. this.appendValueInput("brightness")
  858. .setCheck(null)
  859. .appendField(Blockly.Msg.x_led_matrix_width_ai_set_brightness_1);
  860. this.appendDummyInput()
  861. .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_range);
  862. this.setInputsInline(true);
  863. this.setPreviousStatement(true, null);
  864. this.setNextStatement(true, null);
  865. this.setColour(AILEDCOLOR);
  866. this.setTooltip(Blockly.Msg.ai_led_matrix_set_bright_TOOLTIP);
  867. this.setHelpUrl("");
  868. }
  869. };
  870. Blockly.Python['ai_led_matrix_set_bright'] = function (block) {
  871. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  872. // TODO: Assemble Python into code variable.
  873. var code = '_ai_brightness = ' + value_brightness + '\n';
  874. return code;
  875. };
  876. Blockly.Blocks['ai_led_matrix_xy'] = {
  877. init: function () {
  878. this.appendValueInput("x")
  879. .setCheck(null)
  880. .appendField(Blockly.Msg.x_led_matrix_width_ai_x);
  881. this.appendValueInput("y")
  882. .setCheck(null)
  883. .appendField(Blockly.Msg.x_led_matrix_width_ai_y);
  884. this.setInputsInline(true);
  885. this.setOutput(true, null);
  886. this.setColour(AILEDCOLOR);
  887. this.setTooltip(Blockly.Msg.ai_led_matrix_xy_TOOLTIP);
  888. this.setHelpUrl("");
  889. }
  890. };
  891. Blockly.Python['ai_led_matrix_xy'] = function (block) {
  892. var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
  893. var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC);
  894. // TODO: Assemble Python into code variable.
  895. var code = value_x + ',' + value_y;
  896. // TODO: Change ORDER_NONE to the correct strength.
  897. return [code, Blockly.Python.ORDER_NONE];
  898. };
  899. Blockly.Blocks['ai_led_matrix_wh'] = {
  900. init: function () {
  901. this.appendValueInput("w")
  902. .setCheck(null)
  903. .appendField(Blockly.Msg.x_led_matrix_width_ai);
  904. this.appendValueInput("h")
  905. .setCheck(null)
  906. .appendField(Blockly.Msg.x_led_matrix_height_ai);
  907. this.setInputsInline(true);
  908. this.setOutput(true, null);
  909. this.setColour(AILEDCOLOR);
  910. this.setTooltip(Blockly.Msg.ai_led_matrix_wh_TOOLTIP);
  911. this.setHelpUrl("");
  912. }
  913. };
  914. Blockly.Python['ai_led_matrix_wh'] = function (block) {
  915. var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
  916. var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
  917. // TODO: Assemble Python into code variable.
  918. var code = value_w + ',' + value_h;
  919. // TODO: Change ORDER_NONE to the correct strength.
  920. return [code, Blockly.Python.ORDER_NONE];
  921. };
  922. Blockly.Blocks['ai_led_matrix_color_picker'] = {
  923. init: function () {
  924. this.appendDummyInput()
  925. .appendField(Blockly.Msg.x_led_matrix_color_ai)
  926. .appendField(new Blockly.FieldColour("#ff0000"), "color");
  927. this.setInputsInline(true);
  928. this.setOutput(true, null);
  929. this.setColour(AILEDCOLOR);
  930. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Color_Picker_TOOLTIP);
  931. this.setHelpUrl("");
  932. }
  933. };
  934. Blockly.Python['ai_led_matrix_color_picker'] = function (block) {
  935. var color = block.getFieldValue('color');
  936. var d = 0,
  937. e = 0,
  938. f = 0;
  939. try {
  940. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  941. e = parseInt(color.substring(3, 5), 16),
  942. f = parseInt(color.substring(5, 7), 16))
  943. } catch (g) { }
  944. // TODO: Assemble Python into code variable.
  945. var code = d + ',' + e + ',' + f;
  946. // TODO: Change ORDER_NONE to the correct strength.
  947. return [code, Blockly.Python.ORDER_NONE];
  948. };
  949. Blockly.Blocks['ai_led_matrix_draw_pixel'] = {
  950. init: function () {
  951. this.appendDummyInput()
  952. .appendField(new Blockly.FieldImage("blockly/media/led_draw-pixel.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
  953. this.appendDummyInput()
  954. .appendField(Blockly.Msg.x_led_matrix_draw_title_ai);
  955. this.appendDummyInput()
  956. .appendField(Blockly.Msg.x_led_matrix_draw_pixel_ai);
  957. this.appendValueInput("color")
  958. .setCheck(null)
  959. .appendField(Blockly.Msg.x_led_matrix_parameter_color_ai);
  960. this.appendValueInput("coordinate")
  961. .setCheck(null)
  962. .appendField(Blockly.Msg.x_led_matrix_parameter_coord_ai);
  963. this.setInputsInline(false);
  964. this.setPreviousStatement(true, null);
  965. this.setNextStatement(true, null);
  966. this.setColour(AILEDCOLOR);
  967. this.setTooltip(Blockly.Msg.ai_led_matrix_draw_pixel_TOOLTIP);
  968. this.setHelpUrl("");
  969. }
  970. };
  971. Blockly.Python['ai_led_matrix_draw_pixel'] = function (block) {
  972. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  973. var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
  974. // TODO: Assemble Python into code variable.
  975. var code = 'ai_pixel_value_coordinate = ' + value_coordinate + '\n' +
  976. '_a = _ai_led_matrix.set_led((((ai_pixel_value_coordinate[1]*6)//6)-1)*6+(ai_pixel_value_coordinate[0]-1),rgba_to_rgb_conversion' + value_color + ')\n';
  977. return code;
  978. };
  979. Blockly.Blocks['ai_led_matrix_draw_rectangle'] = {
  980. init: function () {
  981. this.appendDummyInput()
  982. .appendField(new Blockly.FieldImage("blockly/media/led_draw-rect.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
  983. this.appendDummyInput()
  984. .appendField(Blockly.Msg.x_led_matrix_draw_title_ai);
  985. this.appendDummyInput()
  986. .appendField(Blockly.Msg.x_led_matrix_draw_rect_ai);
  987. this.appendValueInput("color")
  988. .setCheck(null)
  989. .appendField(Blockly.Msg.x_led_matrix_parameter_color_ai);
  990. this.appendValueInput("coordinate")
  991. .setCheck(null)
  992. .appendField(Blockly.Msg.x_led_matrix_parameter_coord_ai);
  993. this.appendValueInput("size")
  994. .setCheck(null)
  995. .appendField(Blockly.Msg.x_led_matrix_parameter_size_ai);
  996. this.setInputsInline(false);
  997. this.setPreviousStatement(true, null);
  998. this.setNextStatement(true, null);
  999. this.setColour(AILEDCOLOR);
  1000. this.setTooltip(Blockly.Msg.ai_led_matrix_draw_rectangle_TOOLTIP);
  1001. this.setHelpUrl("");
  1002. }
  1003. };
  1004. Blockly.Python['ai_led_matrix_draw_rectangle'] = function (block) {
  1005. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  1006. var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
  1007. var value_size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);
  1008. Blockly.Python.codeFunctions_['ai_led_matrix_draw_rectangle_setup'] = '' +
  1009. 'def _ai_led_draw_rectangle(x, y, w, h, rgb):\n' +
  1010. ' for i in range(x, x+ w, 1):\n' +
  1011. ' for j in range(y, y+h, 1):\n' +
  1012. ' _a = _ai_led_matrix.set_led((((j*6)//6)-1)*6+(i-1),(rgb))\n' +
  1013. '\n';
  1014. // TODO: Assemble Python into code variable.
  1015. // var comma_location = value_coordinate.indexOf(",");
  1016. // var last_bracket_location = value_coordinate.length;
  1017. // var cord_x = value_coordinate.substr(1, comma_location - 1);
  1018. // var cord_y_pre = value_coordinate.substr(comma_location + 1, last_bracket_location - 1);
  1019. // var cord_y = cord_y_pre.substring(0, cord_y_pre.length - 1)
  1020. // // console.log(value_coordinate + ": " + cord_x + ", " + cord_y);
  1021. // var comma_location_size = value_size.indexOf(",");
  1022. // var last_bracket_location_size = value_size.length;
  1023. // var size_width = value_size.substr(1, comma_location_size - 1);
  1024. // var size_height_pre = value_size.substr(comma_location_size + 1, last_bracket_location_size - 1);
  1025. // var size_height = size_height_pre.substring(0, size_height_pre.length - 1)
  1026. // console.log("Size string length: " + last_bracket_location_size);
  1027. // console.log("Size is " + value_size + ": " + size_width + ", " + size_height);
  1028. var code = 'ai_rectangle_value_coordinate = ' + value_coordinate + '\n' +
  1029. 'ai_rectangle_value_size = ' + value_size + '\n' +
  1030. '_ai_led_draw_rectangle(ai_rectangle_value_coordinate[0], ai_rectangle_value_coordinate[1], ai_rectangle_value_size[0], ai_rectangle_value_size[1], rgba_to_rgb_conversion' + value_color + ')\n';
  1031. return code;
  1032. };
  1033. Blockly.Blocks['ai_led_pattern_6x6'] = {
  1034. init: function () {
  1035. this.appendDummyInput()
  1036. .appendField(
  1037. new Blockly.FieldImage("./../blockly/media/pattern.png", 25, 25, "15"))
  1038. .appendField(Blockly.Msg.LED_DRAW + Blockly.Msg.LED_PATTERN);
  1039. this.appendDummyInput()
  1040. .appendField(" ")
  1041. .appendField((new Blockly.FieldColour("#000000")), "6_1")
  1042. .appendField((new Blockly.FieldColour("#000000")), "6_2")
  1043. .appendField((new Blockly.FieldColour("#000000")), "6_3")
  1044. .appendField((new Blockly.FieldColour("#000000")), "6_4")
  1045. .appendField((new Blockly.FieldColour("#000000")), "6_5")
  1046. .appendField((new Blockly.FieldColour("#000000")), "6_6")
  1047. .appendField(" ");
  1048. this.appendDummyInput()
  1049. .appendField(" ")
  1050. .appendField((new Blockly.FieldColour("#000000")), "5_1")
  1051. .appendField((new Blockly.FieldColour("#000000")), "5_2")
  1052. .appendField((new Blockly.FieldColour("#000000")), "5_3")
  1053. .appendField((new Blockly.FieldColour("#000000")), "5_4")
  1054. .appendField((new Blockly.FieldColour("#000000")), "5_5")
  1055. .appendField((new Blockly.FieldColour("#000000")), "5_6")
  1056. .appendField(" ");
  1057. this.appendDummyInput()
  1058. .appendField(" ")
  1059. .appendField((new Blockly.FieldColour("#000000")), "4_1")
  1060. .appendField((new Blockly.FieldColour("#000000")), "4_2")
  1061. .appendField((new Blockly.FieldColour("#000000")), "4_3")
  1062. .appendField((new Blockly.FieldColour("#000000")), "4_4")
  1063. .appendField((new Blockly.FieldColour("#000000")), "4_5")
  1064. .appendField((new Blockly.FieldColour("#000000")), "4_6")
  1065. .appendField(" ");
  1066. this.appendDummyInput()
  1067. .appendField(" ")
  1068. .appendField((new Blockly.FieldColour("#000000")), "3_1")
  1069. .appendField((new Blockly.FieldColour("#000000")), "3_2")
  1070. .appendField((new Blockly.FieldColour("#000000")), "3_3")
  1071. .appendField((new Blockly.FieldColour("#000000")), "3_4")
  1072. .appendField((new Blockly.FieldColour("#000000")), "3_5")
  1073. .appendField((new Blockly.FieldColour("#000000")), "3_6")
  1074. .appendField(" ");
  1075. this.appendDummyInput()
  1076. .appendField(" ")
  1077. .appendField((new Blockly.FieldColour("#000000")), "2_1")
  1078. .appendField((new Blockly.FieldColour("#000000")), "2_2")
  1079. .appendField((new Blockly.FieldColour("#000000")), "2_3")
  1080. .appendField((new Blockly.FieldColour("#000000")), "2_4")
  1081. .appendField((new Blockly.FieldColour("#000000")), "2_5")
  1082. .appendField((new Blockly.FieldColour("#000000")), "2_6")
  1083. .appendField(" ");
  1084. this.appendDummyInput()
  1085. .appendField(" ")
  1086. .appendField((new Blockly.FieldColour("#000000")), "1_1")
  1087. .appendField((new Blockly.FieldColour("#000000")), "1_2")
  1088. .appendField((new Blockly.FieldColour("#000000")), "1_3")
  1089. .appendField((new Blockly.FieldColour("#000000")), "1_4")
  1090. .appendField((new Blockly.FieldColour("#000000")), "1_5")
  1091. .appendField((new Blockly.FieldColour("#000000")), "1_6")
  1092. .appendField(" ");
  1093. this.setPreviousStatement(true);
  1094. this.setNextStatement(true);
  1095. this.setColour(AILEDCOLOR);
  1096. this.setTooltip('');
  1097. this.setHelpUrl('');
  1098. }
  1099. };
  1100. var ai_led_pattern_6x6_length = 0;
  1101. Blockly.Python['ai_led_pattern_6x6'] = function (block) {
  1102. // TODO: Assemble Python into code variable.
  1103. var rgb = [];
  1104. ai_led_pattern_6x6_length = ai_led_pattern_6x6_length + 1;
  1105. for (var i = 6; i >= 1; i--) {
  1106. for (var j = 1; j < 7; j++) {
  1107. var color = block.getFieldValue(i + '_' + j);
  1108. var d = 0,
  1109. e = 0,
  1110. f = 0;
  1111. try {
  1112. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1113. e = parseInt(color.substring(3, 5), 16),
  1114. f = parseInt(color.substring(5, 7), 16))
  1115. } catch (g) { }
  1116. rgb[(parseInt((i * 6) / 6) - 1) * 6 + (j - 1)] = '(rgba_to_rgb_conversion(' + d + ',' + e + ',' + f + '))';
  1117. }
  1118. }
  1119. Blockly.Python.codeFunctions_['ai_led_pattern_6x6_setup'] = '' +
  1120. 'def _ai_led_draw_picture(rgb):\n' +
  1121. ' for i in range(6, 0, -1):\n' +
  1122. ' for j in range(1, 7):\n' +
  1123. ' _a = _ai_led_matrix.set_led((((i*6)//6)-1)*6+(j-1),(rgb[(((i*6)//6)-1)*6+(j-1)]))\n' +
  1124. '\n';
  1125. Blockly.Python.codeFunctions_['ai_led_pattern_6x6_' + ai_led_pattern_6x6_length + '_setup'] = '' +
  1126. 'ai_led_pattern_6x6_' + ai_led_pattern_6x6_length + ' = [' + rgb + ']' +
  1127. '\n';
  1128. var code = '_ai_led_draw_picture(ai_led_pattern_6x6_' + ai_led_pattern_6x6_length + ')\n';
  1129. return code;
  1130. };
  1131. Blockly.Blocks['ai_led_matrix_show_above'] = {
  1132. init: function () {
  1133. this.appendDummyInput()
  1134. .appendField(Blockly.Msg.x_led_matrix_show_above_ai);
  1135. this.setInputsInline(false);
  1136. this.setPreviousStatement(true, null);
  1137. this.setNextStatement(true, null);
  1138. this.setColour(AILEDCOLOR);
  1139. this.setTooltip(Blockly.Msg.ai_led_matrix_show_above_TOOLTIP);
  1140. this.setHelpUrl("");
  1141. }
  1142. };
  1143. Blockly.Python['ai_led_matrix_show_above'] = function (block) {
  1144. // TODO: Assemble Python into code variable.
  1145. var code = '_ai_led_matrix.display()\n';
  1146. return code;
  1147. };
  1148. Blockly.Blocks['ai_led_matrix_clear_screen'] = {
  1149. init: function () {
  1150. this.appendDummyInput()
  1151. .appendField(Blockly.Msg.x_led_matrix_clear_all_ai);
  1152. this.setInputsInline(false);
  1153. this.setPreviousStatement(true, null);
  1154. this.setNextStatement(true, null);
  1155. this.setColour(AILEDCOLOR);
  1156. this.setTooltip(Blockly.Msg.ai_led_matrix_clear_screen_TOOLTIP);
  1157. this.setHelpUrl("");
  1158. }
  1159. };
  1160. Blockly.Python['ai_led_matrix_clear_screen'] = function (block) {
  1161. // TODO: Assemble Python into code variable.
  1162. Blockly.Python.definitions_['ai_led_matrix_clear_screen_setup'] = '' +
  1163. 'def _ai_led_draw_rectangle(x, y, w, h, rgb):\n' +
  1164. ' for i in range(x, x+ w, 1):\n' +
  1165. ' for j in range(y, y+h, 1):\n' +
  1166. ' _a = _ai_led_matrix.set_led((((i*6)//6)-1)*6+(j-1),(rgb))\n' +
  1167. '';
  1168. var code = '' +
  1169. '_ai_led_draw_rectangle(1, 1, 6, 6, rgba_to_rgb_conversion(0, 0, 0))\n' +
  1170. '_ai_led_matrix.display()\n' +
  1171. '';
  1172. return code;
  1173. };
  1174. Blockly.Blocks['ai_led_matrix_rgb_value_input'] = {
  1175. init: function () {
  1176. this.appendDummyInput()
  1177. .appendField(Blockly.Msg.image_process_rgb_r);
  1178. this.appendValueInput("rgb_value_r")
  1179. .setCheck(null)
  1180. .appendField("");
  1181. this.appendDummyInput()
  1182. .appendField(Blockly.Msg.image_process_rgb_g);
  1183. this.appendValueInput("rgb_value_g")
  1184. .setCheck(null)
  1185. .appendField("");
  1186. this.appendDummyInput()
  1187. .appendField(Blockly.Msg.image_process_rgb_b);
  1188. this.appendValueInput("rgb_value_b")
  1189. .setCheck(null)
  1190. .appendField("");
  1191. this.setInputsInline(true);
  1192. this.setOutput(true, null);
  1193. this.setColour(AILEDCOLOR);
  1194. this.setTooltip(Blockly.Msg.ai_led_matrix_rgb_value_input_TOOLTIP);
  1195. this.setHelpUrl("");
  1196. }
  1197. };
  1198. Blockly.Python['ai_led_matrix_rgb_value_input'] = function (block) {
  1199. var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
  1200. var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
  1201. var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
  1202. // TODO: Assemble Python into code variable.
  1203. var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
  1204. // TODO: Change ORDER_NONE to the correct strength.
  1205. return [code, Blockly.Python.ORDER_NONE];
  1206. };
  1207. Blockly.Blocks['iot_led_matrix_rgb_value_input'] = {
  1208. init: function () {
  1209. this.appendDummyInput()
  1210. .appendField(Blockly.Msg.image_process_rgb_r);
  1211. this.appendValueInput("rgb_value_r")
  1212. .setCheck(null)
  1213. .appendField("");
  1214. this.appendDummyInput()
  1215. .appendField(Blockly.Msg.image_process_rgb_g);
  1216. this.appendValueInput("rgb_value_g")
  1217. .setCheck(null)
  1218. .appendField("");
  1219. this.appendDummyInput()
  1220. .appendField(Blockly.Msg.image_process_rgb_b);
  1221. this.appendValueInput("rgb_value_b")
  1222. .setCheck(null)
  1223. .appendField("");
  1224. this.setInputsInline(true);
  1225. this.setOutput(true, null);
  1226. this.setColour(AILEDCOLOR);
  1227. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Rgb_Value_Input_TOOLTIP);
  1228. this.setHelpUrl("");
  1229. }
  1230. };
  1231. Blockly.Python['iot_led_matrix_rgb_value_input'] = function (block) {
  1232. var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
  1233. var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
  1234. var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
  1235. // TODO: Assemble Python into code variable.
  1236. var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
  1237. // TODO: Change ORDER_NONE to the correct strength.
  1238. return [code, Blockly.Python.ORDER_NONE];
  1239. };
  1240. Blockly.Blocks['iot_led_matrix_setup'] = {
  1241. init: function () {
  1242. this.appendDummyInput()
  1243. .appendField(new Blockly.FieldImage("blockly/media/led_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
  1244. this.appendDummyInput()
  1245. .appendField(Blockly.Msg.x_led_matrix_setup_iot);
  1246. this.appendDummyInput()
  1247. .appendField(Blockly.Msg.x_led_matrix_setup_iot_io)
  1248. .appendField(new Blockly.FieldDropdown([
  1249. ["25", "25"],
  1250. ["15", "15"]
  1251. ]), "io");
  1252. this.appendDummyInput()
  1253. .appendField(Blockly.Msg.x_led_matrix_setup_iot_set_bright_1)
  1254. .appendField(new Blockly.FieldNumber(50, 0, 255, 1), "brightness")
  1255. .appendField(Blockly.Msg.x_led_matrix_setup_iot_set_bright_range);
  1256. this.setInputsInline(false);
  1257. this.setPreviousStatement(true, null);
  1258. this.setNextStatement(true, null);
  1259. this.setColour(AILEDCOLOR);
  1260. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Setup_TOOLTIP);
  1261. this.setHelpUrl("");
  1262. }
  1263. };
  1264. Blockly.Python['iot_led_matrix_setup'] = function (block) {
  1265. var number_brightness = block.getFieldValue('brightness');
  1266. var dropdown_io = block.getFieldValue('io');
  1267. Blockly.Python.definitions_['iot_controller_led_setup'] = '' +
  1268. 'import machine, neopixel, time\n' +
  1269. '_6x6_led_matrix = neopixel.NeoPixel(machine.Pin(' + dropdown_io + '), 36)\n' +
  1270. '_iot_brightness = ' + number_brightness + '\n' +
  1271. '\n' +
  1272. 'def rgba_to_rgb_conversion(red, green, blue):\n' +
  1273. ' if _iot_brightness <= 255:\n' +
  1274. ' alpha = _iot_brightness / 255\n' +
  1275. ' elif _iot_brightness > 255:\n' +
  1276. ' alpha = 255 / 255\n' +
  1277. ' elif _iot_brightness < 0:\n' +
  1278. ' alpha = 0 / 255\n' +
  1279. ' final_red = int((1 - alpha) * 0 + alpha * red)\n' +
  1280. ' final_green = int((1 - alpha) * 0 + alpha * green)\n' +
  1281. ' final_blue = int((1 - alpha) * 0 + alpha * blue)\n' +
  1282. ' final = (final_red, final_green, final_blue)\n' +
  1283. ' return final\n' +
  1284. '\n';
  1285. // TODO: Assemble Python into code variable.
  1286. var code = '';
  1287. return code;
  1288. };
  1289. Blockly.Blocks['iot_led_matrix_set_bright'] = {
  1290. init: function () {
  1291. this.appendValueInput("brightness")
  1292. .setCheck(null)
  1293. .appendField(Blockly.Msg.x_led_matrix_width_ai_set_brightness_1);
  1294. this.appendDummyInput()
  1295. .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_range);
  1296. this.setInputsInline(true);
  1297. this.setPreviousStatement(true, null);
  1298. this.setNextStatement(true, null);
  1299. this.setColour(AILEDCOLOR);
  1300. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Set_Bright_TOOLTIP);
  1301. this.setHelpUrl("");
  1302. }
  1303. };
  1304. Blockly.Python['iot_led_matrix_set_bright'] = function (block) {
  1305. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  1306. // TODO: Assemble Python into code variable.
  1307. var code = '_iot_brightness = ' + value_brightness + '\n';
  1308. return code;
  1309. };
  1310. Blockly.Blocks['iot_led_matrix_xy'] = {
  1311. init: function () {
  1312. this.appendValueInput("x")
  1313. .setCheck(null)
  1314. .appendField(Blockly.Msg.x_led_matrix_width_iot_x);
  1315. this.appendValueInput("y")
  1316. .setCheck(null)
  1317. .appendField(Blockly.Msg.x_led_matrix_width_iot_y);
  1318. this.setInputsInline(true);
  1319. this.setOutput(true, null);
  1320. this.setColour(AILEDCOLOR);
  1321. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_xy_TOOLTIP);
  1322. this.setHelpUrl("");
  1323. }
  1324. };
  1325. Blockly.Python['iot_led_matrix_xy'] = function (block) {
  1326. var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
  1327. var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC);
  1328. // TODO: Assemble Python into code variable.
  1329. var code = value_x + ',' + value_y;
  1330. // TODO: Change ORDER_NONE to the correct strength.
  1331. return [code, Blockly.Python.ORDER_NONE];
  1332. };
  1333. Blockly.Blocks['iot_led_matrix_wh'] = {
  1334. init: function () {
  1335. this.appendValueInput("w")
  1336. .setCheck(null)
  1337. .appendField(Blockly.Msg.x_led_matrix_width_iot);
  1338. this.appendValueInput("h")
  1339. .setCheck(null)
  1340. .appendField(Blockly.Msg.x_led_matrix_height_iot);
  1341. this.setInputsInline(true);
  1342. this.setOutput(true, null);
  1343. this.setColour(AILEDCOLOR);
  1344. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_wh_TOOLTIP);
  1345. this.setHelpUrl("");
  1346. }
  1347. };
  1348. Blockly.Python['iot_led_matrix_wh'] = function (block) {
  1349. var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
  1350. var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
  1351. // TODO: Assemble Python into code variable.
  1352. var code = value_w + ',' + value_h;
  1353. // TODO: Change ORDER_NONE to the correct strength.
  1354. return [code, Blockly.Python.ORDER_NONE];
  1355. };
  1356. Blockly.Blocks['iot_led_matrix_color_picker'] = {
  1357. init: function () {
  1358. this.appendDummyInput()
  1359. .appendField(Blockly.Msg.x_led_matrix_color_iot)
  1360. .appendField(new Blockly.FieldColour("#ff0000"), "color");
  1361. this.setInputsInline(true);
  1362. this.setOutput(true, null);
  1363. this.setColour(AILEDCOLOR);
  1364. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Color_Picker_TOOLTIP);
  1365. this.setHelpUrl("");
  1366. }
  1367. };
  1368. Blockly.Python['iot_led_matrix_color_picker'] = function (block) {
  1369. var color = block.getFieldValue('color');
  1370. var d = 0,
  1371. e = 0,
  1372. f = 0;
  1373. try {
  1374. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1375. e = parseInt(color.substring(3, 5), 16),
  1376. f = parseInt(color.substring(5, 7), 16))
  1377. } catch (g) { }
  1378. // TODO: Assemble Python into code variable.
  1379. var code = d + ',' + e + ',' + f;
  1380. // TODO: Change ORDER_NONE to the correct strength.
  1381. return [code, Blockly.Python.ORDER_MEMBER];
  1382. };
  1383. Blockly.Blocks['iot_led_matrix_draw_pixel'] = {
  1384. init: function () {
  1385. this.appendDummyInput()
  1386. .appendField(new Blockly.FieldImage("blockly/media/led_draw-pixel.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
  1387. this.appendDummyInput()
  1388. .appendField(Blockly.Msg.x_led_matrix_draw_title_iot);
  1389. this.appendDummyInput()
  1390. .appendField(Blockly.Msg.x_led_matrix_draw_pixel_iot);
  1391. this.appendValueInput("color")
  1392. .setCheck(null)
  1393. .appendField(Blockly.Msg.x_led_matrix_parameter_color_iot);
  1394. this.appendValueInput("coordinate")
  1395. .setCheck(null)
  1396. .appendField(Blockly.Msg.x_led_matrix_parameter_coord_iot);
  1397. this.setInputsInline(false);
  1398. this.setPreviousStatement(true, null);
  1399. this.setNextStatement(true, null);
  1400. this.setColour(AILEDCOLOR);
  1401. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Draw_Pixel_TOOLTIP);
  1402. this.setHelpUrl("");
  1403. }
  1404. };
  1405. Blockly.Python['iot_led_matrix_draw_pixel'] = function (block) {
  1406. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  1407. var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
  1408. // TODO: Assemble Python into code variable.
  1409. // _6x6_led_matrix[(((1*6)//6)-1)*6+(1-1)] = (0, 0, 255)
  1410. var code = 'iot_pixel_value_coordinate = ' + value_coordinate + '\n' +
  1411. '_6x6_led_matrix[(((iot_pixel_value_coordinate[1]*6)//6)-1)*6+(iot_pixel_value_coordinate[0]-1)] = rgba_to_rgb_conversion' + value_color + '\n';
  1412. return code;
  1413. };
  1414. Blockly.Blocks['iot_led_matrix_draw_rectangle'] = {
  1415. init: function () {
  1416. this.appendDummyInput()
  1417. .appendField(new Blockly.FieldImage("blockly/media/led_draw-rect.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
  1418. this.appendDummyInput()
  1419. .appendField(Blockly.Msg.x_led_matrix_draw_title_iot);
  1420. this.appendDummyInput()
  1421. .appendField(Blockly.Msg.x_led_matrix_draw_rect_ai);
  1422. this.appendValueInput("color")
  1423. .setCheck(null)
  1424. .appendField(Blockly.Msg.x_led_matrix_parameter_color_ai);
  1425. this.appendValueInput("coordinate")
  1426. .setCheck(null)
  1427. .appendField(Blockly.Msg.x_led_matrix_parameter_coord_ai);
  1428. this.appendValueInput("size")
  1429. .setCheck(null)
  1430. .appendField(Blockly.Msg.x_led_matrix_parameter_size_ai);
  1431. this.setInputsInline(false);
  1432. this.setPreviousStatement(true, null);
  1433. this.setNextStatement(true, null);
  1434. this.setColour(AILEDCOLOR);
  1435. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Draw_Rectangle_TOOLTIP);
  1436. this.setHelpUrl("");
  1437. }
  1438. };
  1439. Blockly.Python['iot_led_matrix_draw_rectangle'] = function (block) {
  1440. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  1441. var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
  1442. var value_size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);
  1443. Blockly.Python.definitions_['iot_led_matrix_draw_rectangle_setup'] = '' +
  1444. 'def _iot_led_draw_rectangle(x, y, w, h, rgb):\n' +
  1445. ' for i in range(x, x+ w, 1):\n' +
  1446. ' for j in range(y, y+h, 1):\n' +
  1447. ' _6x6_led_matrix[(((j*6)//6)-1)*6+(i-1)] = (rgb)' +
  1448. '\n';
  1449. // TODO: Assemble Python into code variable.
  1450. // var comma_location = value_coordinate.indexOf(",");
  1451. // var last_bracket_location = value_coordinate.length;
  1452. // var cord_x = value_coordinate.substr(1, comma_location - 1);
  1453. // var cord_y_pre = value_coordinate.substr(comma_location + 1, last_bracket_location - 1);
  1454. // var cord_y = cord_y_pre.substring(0, cord_y_pre.length - 1)
  1455. // // console.log(value_coordinate + ": " + cord_x + ", " + cord_y);
  1456. // var comma_location_size = value_size.indexOf(",");
  1457. // var last_bracket_location_size = value_size.length;
  1458. // var size_width = value_size.substr(1, comma_location_size - 1);
  1459. // var size_height_pre = value_size.substr(comma_location_size + 1, last_bracket_location_size - 1);
  1460. // var size_height = size_height_pre.substring(0, size_height_pre.length - 1)
  1461. // console.log("Size string length: " + last_bracket_location_size);
  1462. // console.log("Size is " + value_size + ": " + size_width + ", " + size_height);
  1463. var code = 'iot_rectangle_value_coordinate = ' + value_coordinate + '\n' +
  1464. 'iot_rectangle_value_size = ' + value_size + '\n' +
  1465. '_iot_led_draw_rectangle(iot_rectangle_value_coordinate[0], iot_rectangle_value_coordinate[1], iot_rectangle_value_size[0], iot_rectangle_value_size[1], rgba_to_rgb_conversion' + value_color + ')\n';
  1466. return code;
  1467. };
  1468. Blockly.Blocks['iot_led_pattern_6x6'] = {
  1469. init: function () {
  1470. this.appendDummyInput()
  1471. .appendField(
  1472. new Blockly.FieldImage("./../blockly/media/pattern.png", 25, 25, "15"))
  1473. .appendField(Blockly.Msg.LED_DRAW + Blockly.Msg.LED_PATTERN);
  1474. this.appendDummyInput()
  1475. .appendField(" ")
  1476. .appendField((new Blockly.FieldColour("#000000")), "6_1")
  1477. .appendField((new Blockly.FieldColour("#000000")), "6_2")
  1478. .appendField((new Blockly.FieldColour("#000000")), "6_3")
  1479. .appendField((new Blockly.FieldColour("#000000")), "6_4")
  1480. .appendField((new Blockly.FieldColour("#000000")), "6_5")
  1481. .appendField((new Blockly.FieldColour("#000000")), "6_6")
  1482. .appendField(" ");
  1483. this.appendDummyInput()
  1484. .appendField(" ")
  1485. .appendField((new Blockly.FieldColour("#000000")), "5_1")
  1486. .appendField((new Blockly.FieldColour("#000000")), "5_2")
  1487. .appendField((new Blockly.FieldColour("#000000")), "5_3")
  1488. .appendField((new Blockly.FieldColour("#000000")), "5_4")
  1489. .appendField((new Blockly.FieldColour("#000000")), "5_5")
  1490. .appendField((new Blockly.FieldColour("#000000")), "5_6")
  1491. .appendField(" ");
  1492. this.appendDummyInput()
  1493. .appendField(" ")
  1494. .appendField((new Blockly.FieldColour("#000000")), "4_1")
  1495. .appendField((new Blockly.FieldColour("#000000")), "4_2")
  1496. .appendField((new Blockly.FieldColour("#000000")), "4_3")
  1497. .appendField((new Blockly.FieldColour("#000000")), "4_4")
  1498. .appendField((new Blockly.FieldColour("#000000")), "4_5")
  1499. .appendField((new Blockly.FieldColour("#000000")), "4_6")
  1500. .appendField(" ");
  1501. this.appendDummyInput()
  1502. .appendField(" ")
  1503. .appendField((new Blockly.FieldColour("#000000")), "3_1")
  1504. .appendField((new Blockly.FieldColour("#000000")), "3_2")
  1505. .appendField((new Blockly.FieldColour("#000000")), "3_3")
  1506. .appendField((new Blockly.FieldColour("#000000")), "3_4")
  1507. .appendField((new Blockly.FieldColour("#000000")), "3_5")
  1508. .appendField((new Blockly.FieldColour("#000000")), "3_6")
  1509. .appendField(" ");
  1510. this.appendDummyInput()
  1511. .appendField(" ")
  1512. .appendField((new Blockly.FieldColour("#000000")), "2_1")
  1513. .appendField((new Blockly.FieldColour("#000000")), "2_2")
  1514. .appendField((new Blockly.FieldColour("#000000")), "2_3")
  1515. .appendField((new Blockly.FieldColour("#000000")), "2_4")
  1516. .appendField((new Blockly.FieldColour("#000000")), "2_5")
  1517. .appendField((new Blockly.FieldColour("#000000")), "2_6")
  1518. .appendField(" ");
  1519. this.appendDummyInput()
  1520. .appendField(" ")
  1521. .appendField((new Blockly.FieldColour("#000000")), "1_1")
  1522. .appendField((new Blockly.FieldColour("#000000")), "1_2")
  1523. .appendField((new Blockly.FieldColour("#000000")), "1_3")
  1524. .appendField((new Blockly.FieldColour("#000000")), "1_4")
  1525. .appendField((new Blockly.FieldColour("#000000")), "1_5")
  1526. .appendField((new Blockly.FieldColour("#000000")), "1_6")
  1527. .appendField(" ");
  1528. this.setPreviousStatement(true);
  1529. this.setNextStatement(true);
  1530. this.setColour(AILEDCOLOR);
  1531. this.setTooltip('');
  1532. this.setHelpUrl('');
  1533. }
  1534. };
  1535. var iot_led_pattern_6x6_length = 0;
  1536. Blockly.Python['iot_led_pattern_6x6'] = function (block) {
  1537. // TODO: Assemble Python into code variable.
  1538. var rgb = [];
  1539. iot_led_pattern_6x6_length = iot_led_pattern_6x6_length + 1;
  1540. for (var i = 6; i >= 1; i--) {
  1541. for (var j = 1; j < 7; j++) {
  1542. var color = block.getFieldValue(i + '_' + j);
  1543. var d = 0,
  1544. e = 0,
  1545. f = 0;
  1546. try {
  1547. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1548. e = parseInt(color.substring(3, 5), 16),
  1549. f = parseInt(color.substring(5, 7), 16))
  1550. } catch (g) { }
  1551. rgb[(parseInt((i * 6) / 6) - 1) * 6 + (j - 1)] = '(rgba_to_rgb_conversion(' + d + ',' + e + ',' + f + '))';
  1552. }
  1553. }
  1554. Blockly.Python.codeFunctions_['iot_led_pattern_6x6_setup'] = '' +
  1555. 'def _iot_led_draw_picture(rgb):\n' +
  1556. ' for i in range(6, 0, -1):\n' +
  1557. ' for j in range(1, 7):\n' +
  1558. ' _6x6_led_matrix[(((i*6)//6)-1)*6+(j-1)] = (rgb[(((i*6)//6)-1)*6+(j-1)])\n' +
  1559. '\n';
  1560. Blockly.Python.codeFunctions_['iot_led_pattern_6x6_' + iot_led_pattern_6x6_length + '_setup'] = '' +
  1561. 'iot_led_pattern_6x6_' + iot_led_pattern_6x6_length + ' = [' + rgb + ']' +
  1562. '\n';
  1563. var code = '_iot_led_draw_picture(iot_led_pattern_6x6_' + iot_led_pattern_6x6_length + ')\n';
  1564. return code;
  1565. };
  1566. Blockly.Blocks['iot_led_matrix_show_above'] = {
  1567. init: function () {
  1568. this.appendDummyInput()
  1569. .appendField(Blockly.Msg.x_led_matrix_show_above_iot);
  1570. this.setInputsInline(false);
  1571. this.setPreviousStatement(true, null);
  1572. this.setNextStatement(true, null);
  1573. this.setColour(AILEDCOLOR);
  1574. this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Show_Above_TOOLTIP);
  1575. this.setHelpUrl("");
  1576. }
  1577. };
  1578. Blockly.Python['iot_led_matrix_show_above'] = function (block) {
  1579. // TODO: Assemble Python into code variable.
  1580. var code = '_6x6_led_matrix.write()\n';
  1581. return code;
  1582. };
  1583. Blockly.Blocks['iot_led_matrix_clear_screen'] = {
  1584. init: function () {
  1585. this.appendDummyInput()
  1586. .appendField(Blockly.Msg.x_led_matrix_clear_all_ai);
  1587. this.setInputsInline(false);
  1588. this.setPreviousStatement(true, null);
  1589. this.setNextStatement(true, null);
  1590. this.setColour(AILEDCOLOR);
  1591. this.setTooltip(Blockly.Msg.ai_led_matrix_clear_screen_TOOLTIP);
  1592. this.setHelpUrl("");
  1593. }
  1594. };
  1595. Blockly.Python['iot_led_matrix_clear_screen'] = function (block) {
  1596. // TODO: Assemble Python into code variable.
  1597. Blockly.Python.definitions_['iot_led_matrix_clear_screen_setup'] = '' +
  1598. 'def _iot_led_draw_rectangle(x, y, w, h, rgb):\n' +
  1599. ' for i in range(x, x+ w, 1):\n' +
  1600. ' for j in range(y, y+h, 1):\n' +
  1601. ' _6x6_led_matrix[(((j*6)//6)-1)*6+(i-1)] = (rgb)' +
  1602. '\n';
  1603. var code = '' +
  1604. '_iot_led_draw_rectangle(1,1,6,6,rgba_to_rgb_conversion(0,0,0))\n' +
  1605. '_6x6_led_matrix.write()\n' +
  1606. '';
  1607. return code;
  1608. };
  1609. /*
  1610. _ _____ ____ ____ _ _
  1611. | | | ____| _ \ / ___|| |_ _ __(_)_ __
  1612. | | | _| | | | | \___ \| __| '__| | '_ \
  1613. | |___| |___| |_| | ___) | |_| | | | |_) |
  1614. |_____|_____|____/ |____/ \__|_| |_| .__/
  1615. |_|
  1616. */
  1617. var ESP32_LED_STRIP_COLOR = AILEDCOLOR;
  1618. Blockly.Blocks['esp32_main_controller_led_strip_setup'] = {
  1619. init: function () {
  1620. this.appendDummyInput()
  1621. .appendField(new Blockly.FieldImage("blockly/media/led_strip_setup.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1622. this.appendDummyInput()
  1623. .appendField(Blockly.Msg.ledstrip_setup_text)
  1624. .appendField(new Blockly.FieldVariable("STRIP"), "varitem")
  1625. .appendField(Blockly.Msg.ledstrip_setup);
  1626. this.appendDummyInput()
  1627. .appendField(Blockly.Msg.ledstrip_set_gpio)
  1628. .appendField(new Blockly.FieldDropdown([
  1629. ["S1", "1"],
  1630. ["S2", "2"]
  1631. ]), "value_io");
  1632. this.appendValueInput("count")
  1633. .setCheck(null)
  1634. .appendField(Blockly.Msg.ledstrip_set_total_led);
  1635. this.setInputsInline(false);
  1636. this.setPreviousStatement(true, null);
  1637. this.setNextStatement(true, null);
  1638. this.setColour(ESP32_LED_STRIP_COLOR);
  1639. this.setHelpUrl("");
  1640. this.setTooltip();
  1641. }
  1642. };
  1643. Blockly.Python['esp32_main_controller_led_strip_setup'] = function (block) {
  1644. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1645. var value_io = block.getFieldValue('value_io');
  1646. var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
  1647. Blockly.Python.definitions_['v831_import_time'] = `import time`;
  1648. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  1649. sys.path.append("/root/")`
  1650. Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
  1651. Blockly.Python.addVariable(variable_name, `${variable_name} = multiFuncGpio(${value_io - 1},7)`, true);
  1652. var code = ""
  1653. return code;
  1654. };
  1655. Blockly.Blocks['esp32_main_controller_led_strip_set_bright'] = {
  1656. init: function () {
  1657. this.appendValueInput("brightness")
  1658. .setCheck(null)
  1659. .appendField(Blockly.Msg.ledstrip_set_brightness_1);
  1660. this.appendDummyInput()
  1661. .appendField(Blockly.Msg.ledstrip_set_brightness_after);
  1662. this.setInputsInline(true);
  1663. this.setPreviousStatement(true, null);
  1664. this.setNextStatement(true, null);
  1665. this.setColour(AILEDCOLOR);
  1666. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Led_strip_set_bright_TOOLTIP);
  1667. this.setHelpUrl("");
  1668. }
  1669. };
  1670. Blockly.Python['esp32_main_controller_led_strip_set_bright'] = function (block) {
  1671. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  1672. // TODO: Assemble Python into code variable.
  1673. var code = '_ai_brightness = ' + value_brightness + '\n';
  1674. return code;
  1675. };
  1676. Blockly.Blocks['esp32_main_controller_led_strip_set'] = {
  1677. init: function () {
  1678. this.appendDummyInput()
  1679. .appendField(Blockly.Msg.ledstrip_set_gpio)
  1680. .appendField(new Blockly.FieldDropdown([
  1681. ["S1", "1"],
  1682. ["S2", "2"]
  1683. ]), "value_io");
  1684. this.appendValueInput("location")
  1685. .setCheck(null)
  1686. .appendField(Blockly.Msg.ledstrip_set_no);
  1687. this.appendValueInput("color")
  1688. .setCheck(null)
  1689. .appendField(Blockly.Msg.ledstrip_set_color);
  1690. this.appendValueInput("brightness")
  1691. .setCheck(null)
  1692. .appendField(Blockly.Msg.ledstrip_set_brightness_setup);
  1693. this.setInputsInline(false);
  1694. this.setPreviousStatement(true, null);
  1695. this.setNextStatement(true, null);
  1696. this.setColour(ESP32_LED_STRIP_COLOR);
  1697. this.setHelpUrl("");
  1698. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Led_Strip_Set_TOOLTIP.replace('%1', "CocoPi"));
  1699. }
  1700. };
  1701. Blockly.Python['esp32_main_controller_led_strip_set'] = function (block) {
  1702. var value_location = Blockly.Python.valueToCode(block, 'location', Blockly.Python.ORDER_ATOMIC);
  1703. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  1704. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  1705. var value_io = block.getFieldValue('value_io');
  1706. // var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
  1707. Blockly.Python.definitions_['v831_import_time'] = `import time`;
  1708. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  1709. sys.path.append("/root/")`
  1710. Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
  1711. Blockly.Python.addVariable("STRIP" + value_io, `STRIP${value_io} = multiFuncGpio(${value_io - 1},7)
  1712. STRIP${value_io}.setBrightness(${value_brightness})`, true);
  1713. Blockly.Python.addVariable("setLedColor", 'setLedColor = ""', true)
  1714. // TODO: Assemble Python into code variable.
  1715. console.log(value_color)
  1716. var code = `setLedColor = ${value_color}
  1717. STRIP${value_io}.setRgbSerialColor(${value_location} - 1 ,setLedColor[0],setLedColor[1],setLedColor[2])
  1718. STRIP${value_io}.rgbSerialShow()
  1719. time.sleep(0.01)
  1720. `
  1721. return code;
  1722. };
  1723. Blockly.Blocks['esp32_main_controller_led_strip_set_one'] = {
  1724. init: function () {
  1725. this.appendDummyInput()
  1726. .appendField(Blockly.Msg.ledstrip_set_gpio)
  1727. .appendField(new Blockly.FieldDropdown([
  1728. ["S1", "1"],
  1729. ["S2", "2"]
  1730. ]), "value_io").appendField(Blockly.Msg.ledstrip_setup_text);
  1731. this.appendValueInput("location")
  1732. .setCheck(null)
  1733. .appendField(Blockly.Msg.uav_get_shooting_value_0);
  1734. this.appendValueInput("location1")
  1735. .setCheck(null)
  1736. .appendField(Blockly.Msg.OLCD_LINE_END)
  1737. this.appendValueInput("color")
  1738. .setCheck(null)
  1739. .appendField(Blockly.Msg.ledstrip_set_color);
  1740. this.appendValueInput("brightness")
  1741. .setCheck(null)
  1742. .appendField(Blockly.Msg.ledstrip_set_brightness_setup);
  1743. this.setInputsInline(false);
  1744. this.setPreviousStatement(true, null);
  1745. this.setNextStatement(true, null);
  1746. this.setColour(ESP32_LED_STRIP_COLOR);
  1747. this.setHelpUrl("");
  1748. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Led_Strip_Set_TOOLTIP.replace('%1', "CocoPi"));
  1749. }
  1750. };
  1751. Blockly.Python['esp32_main_controller_led_strip_set_one'] = function (block) {
  1752. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  1753. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  1754. var location1 = Blockly.Python.valueToCode(block, 'location1', Blockly.Python.ORDER_ATOMIC);
  1755. var location = Blockly.Python.valueToCode(block, 'location', Blockly.Python.ORDER_ATOMIC);
  1756. var value_io = block.getFieldValue('value_io');
  1757. let allBlocks = block.workspace.getAllBlocks();
  1758. let global = ""
  1759. try {
  1760. global = allBlocks[0].workspace.variableList.toString()
  1761. }
  1762. catch (e) {
  1763. console.log(e)
  1764. }
  1765. // var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
  1766. Blockly.Python.definitions_['v831_import_time'] = `import time`;
  1767. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  1768. sys.path.append("/root/")`
  1769. Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
  1770. Blockly.Python.addVariable("STRIP" + value_io, `STRIP${value_io} = multiFuncGpio(${value_io - 1},7)
  1771. STRIP${value_io}.setBrightness(${value_brightness})`, true);
  1772. Blockly.Python.addFunction("LEDSTRIPSHOW",`def LEDSTRIPSHOW(LEDCOLOR,num1,num2,brightness):
  1773. for i in range(num1-1,num2+1):
  1774. STRIP${value_io}.setRgbSerialColor(i-1 ,LEDCOLOR[0],LEDCOLOR[1],LEDCOLOR[2])
  1775. STRIP${value_io}.rgbSerialShow()
  1776. time.sleep(0.01)
  1777. `,true)
  1778. var code = `LEDSTRIPSHOW(${value_color},${location},${location1},${value_brightness})\n`
  1779. return code;
  1780. }
  1781. Blockly.Blocks['esp32_main_controller_led_strip_set_all'] = {
  1782. init: function () {
  1783. this.appendDummyInput()
  1784. .appendField(Blockly.Msg.ledstrip_set_gpio)
  1785. .appendField(new Blockly.FieldDropdown([
  1786. ["S1", "1"],
  1787. ["S2", "2"]
  1788. ]), "value_io").appendField(Blockly.Msg.ledstrip_set_color_all_on);
  1789. this.appendValueInput("color")
  1790. .setCheck(null)
  1791. .appendField(Blockly.Msg.ledstrip_set_color);
  1792. this.appendValueInput("brightness")
  1793. .setCheck(null)
  1794. .appendField(Blockly.Msg.ledstrip_set_brightness_setup);
  1795. this.setInputsInline(false);
  1796. this.setPreviousStatement(true, null);
  1797. this.setNextStatement(true, null);
  1798. this.setColour(ESP32_LED_STRIP_COLOR);
  1799. this.setHelpUrl("");
  1800. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Led_Strip_Set_TOOLTIP.replace('%1', "CocoPi"));
  1801. }
  1802. };
  1803. Blockly.Python['esp32_main_controller_led_strip_set_all'] = function (block) {
  1804. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  1805. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  1806. var value_io = block.getFieldValue('value_io');
  1807. let allBlocks = block.workspace.getAllBlocks();
  1808. let global = ""
  1809. try {
  1810. global = allBlocks[0].workspace.variableList.toString()
  1811. }
  1812. catch (e) {
  1813. console.log(e)
  1814. }
  1815. // var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
  1816. Blockly.Python.definitions_['v831_import_time'] = `import time`;
  1817. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  1818. sys.path.append("/root/")`
  1819. Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
  1820. Blockly.Python.addVariable("STRIP" + value_io, `STRIP${value_io} = multiFuncGpio(${value_io - 1},7)
  1821. STRIP${value_io}.setBrightness(${value_brightness})`, true);
  1822. Blockly.Python.addFunction("LEDSTRIPSHOW",`def LEDSTRIPSHOW(LEDCOLOR,num1,num2,brightness):
  1823. for i in range(num1-1,num2+1):
  1824. STRIP${value_io}.setRgbSerialColor(i-1 ,LEDCOLOR[0],LEDCOLOR[1],LEDCOLOR[2])
  1825. STRIP${value_io}.rgbSerialShow()
  1826. time.sleep(0.01)
  1827. `,true)
  1828. var code = `LEDSTRIPSHOW(${value_color},1,10,${value_brightness})\n`
  1829. return code;
  1830. }
  1831. Blockly.Blocks['esp32_main_controller_led_strip_set_all_off'] = {
  1832. init: function () {
  1833. this.appendDummyInput()
  1834. .appendField(Blockly.Msg.ledstrip_set_gpio_off)
  1835. .appendField(new Blockly.FieldDropdown([
  1836. ["S1", "1"],
  1837. ["S2", "2"]
  1838. ]), "value_io").appendField(Blockly.Msg.ledstrip_set_color_all_on);
  1839. this.setInputsInline(false);
  1840. this.setPreviousStatement(true, null);
  1841. this.setNextStatement(true, null);
  1842. this.setColour(ESP32_LED_STRIP_COLOR);
  1843. this.setHelpUrl("");
  1844. this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Led_Strip_Set_TOOLTIP.replace('%1', "CocoPi"));
  1845. }
  1846. };
  1847. Blockly.Python['esp32_main_controller_led_strip_set_all_off'] = function (block) {
  1848. var value_io = block.getFieldValue('value_io');
  1849. // var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
  1850. Blockly.Python.definitions_['v831_import_time'] = `import time`;
  1851. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  1852. sys.path.append("/root/")`
  1853. Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
  1854. Blockly.Python.addVariable("STRIPOFF" + value_io, `STRIPOFF${value_io} = multiFuncGpio(${value_io - 1},7)
  1855. STRIPOFF${value_io}.setBrightness(0)`, true);
  1856. Blockly.Python.addFunction("LEDSTRIPSHOWOFF",`def LEDSTRIPSHOWOFF(LEDCOLOR):
  1857. for i in range(10):
  1858. STRIPOFF${value_io}.setRgbSerialColor(i ,LEDCOLOR[0],LEDCOLOR[1],LEDCOLOR[2])
  1859. STRIPOFF${value_io}.rgbSerialShow()
  1860. time.sleep(0.01)
  1861. `,true)
  1862. var code = `LEDSTRIPSHOWOFF((0,0,0))\n`;
  1863. return code;
  1864. }
  1865. Blockly.Blocks['led_strip_show_above'] = {
  1866. init: function () {
  1867. this.appendDummyInput()
  1868. .appendField(Blockly.Msg.ledstrip_setup_text_show_above_set)
  1869. .appendField(new Blockly.FieldVariable("STRIP"), "varitem")
  1870. .appendField(Blockly.Msg.ledstrip_setup_text_show_above);
  1871. this.setInputsInline(false);
  1872. this.setPreviousStatement(true, null);
  1873. this.setNextStatement(true, null);
  1874. this.setColour(AILEDCOLOR);
  1875. this.setHelpUrl("");
  1876. this.setTooltip(Blockly.Msg.Led_Strip_Show_Above_TOOLTIP.replace('%1', "CocoPi"));
  1877. }
  1878. };
  1879. Blockly.Python['led_strip_show_above'] = function (block) {
  1880. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1881. // TODO: Assemble Python into code variable.
  1882. var code = `${variable_name}.pixelShow()
  1883. `
  1884. return code;
  1885. };
  1886. Blockly.Blocks['led_strip_color_rgb'] = {
  1887. init: function () {
  1888. this.appendDummyInput()
  1889. .appendField(Blockly.Msg.image_process_rgb_r);
  1890. this.appendValueInput("rgb_value_r")
  1891. .setCheck(null)
  1892. .appendField("");
  1893. this.appendDummyInput()
  1894. .appendField(Blockly.Msg.image_process_rgb_g);
  1895. this.appendValueInput("rgb_value_g")
  1896. .setCheck(null)
  1897. .appendField("");
  1898. this.appendDummyInput()
  1899. .appendField(Blockly.Msg.image_process_rgb_b);
  1900. this.appendValueInput("rgb_value_b")
  1901. .setCheck(null)
  1902. .appendField("");
  1903. this.setInputsInline(true);
  1904. this.setOutput(true, null);
  1905. this.setColour(AILEDCOLOR);
  1906. this.setTooltip(Blockly.Msg.Led_Strip_Color_Rgb_TOOLTIP);
  1907. this.setHelpUrl("");
  1908. }
  1909. };
  1910. Blockly.Python['led_strip_color_rgb'] = function (block) {
  1911. var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
  1912. var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
  1913. var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
  1914. // TODO: Assemble Python into code variable.
  1915. var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
  1916. // TODO: Change ORDER_NONE to the correct strength.
  1917. return [code, Blockly.Python.ORDER_NONE];
  1918. };
  1919. /*
  1920. ____ ____ _
  1921. / ___| __ _ _ __ ___ ___| _ \ __ _ __| |
  1922. | | _ / _` | '_ ` _ \ / _ \ |_) / _` |/ _` |
  1923. | |_| | (_| | | | | | | __/ __/ (_| | (_| |
  1924. \____|\__,_|_| |_| |_|\___|_| \__,_|\__,_|
  1925. */
  1926. var GAMEPAD_COLOR = "#183895";
  1927. Blockly.Blocks['x_iot_read_gamepad_button_pressed'] = {
  1928. init: function () {
  1929. this.appendDummyInput()
  1930. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_iot)
  1931. .appendField(new Blockly.FieldDropdown([
  1932. ["X", "x"],
  1933. ["Y", "y"],
  1934. ["A", "a"],
  1935. ["B", "b"],
  1936. ["L", "l"],
  1937. ["R", "r"]
  1938. ]), "type")
  1939. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_pressed_iot);
  1940. this.setInputsInline(true);
  1941. this.setOutput(true, null);
  1942. this.setColour(GAMEPAD_COLOR);
  1943. var thisBlock = this;
  1944. this.setTooltip(function () {
  1945. var mode = thisBlock.getFieldValue('type');
  1946. var TOOLTIPS = {
  1947. 'x': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "X"),
  1948. 'y': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "Y"),
  1949. 'a': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "A"),
  1950. 'b': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "B"),
  1951. 'l': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "L"),
  1952. 'r': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "R")
  1953. };
  1954. return TOOLTIPS[mode];
  1955. });
  1956. this.setHelpUrl("");
  1957. }
  1958. };
  1959. Blockly.Python['x_iot_read_gamepad_button_pressed'] = function (block) {
  1960. var dropdown_type = block.getFieldValue('type');
  1961. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  1962. 'from machine import ADC, Pin\n' +
  1963. 'import time\n' +
  1964. 'from machine import I2C\n' +
  1965. 'from ads1x15 import ADS1115\n' +
  1966. '\n';
  1967. Blockly.Python.definitions_['gamepad_button_def_iot'] = '' +
  1968. '_px = Pin(36, Pin.IN,Pin.PULL_UP)\n' +
  1969. '_py = Pin(39, Pin.IN,Pin.PULL_UP)\n' +
  1970. '_pa = Pin(4, Pin.IN,Pin.PULL_UP)\n' +
  1971. '_pb = Pin(2, Pin.IN,Pin.PULL_UP)\n' +
  1972. '_pl = Pin(17, Pin.IN,Pin.PULL_UP)\n' +
  1973. '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' +
  1974. '\n';
  1975. // TODO: Assemble Python into code variable.
  1976. var code = '_p' + dropdown_type + '.value() == 1';
  1977. // TODO: Change ORDER_NONE to the correct strength.
  1978. return [code, Blockly.Python.ORDER_NONE];
  1979. };
  1980. Blockly.Blocks['x_iot_read_gamepad_button_released'] = {
  1981. init: function () {
  1982. this.appendDummyInput()
  1983. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_iot)
  1984. .appendField(new Blockly.FieldDropdown([
  1985. ["X", "x"],
  1986. ["Y", "y"],
  1987. ["A", "a"],
  1988. ["B", "b"],
  1989. ["L", "l"],
  1990. ["R", "r"]
  1991. ]), "type")
  1992. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_released_iot);
  1993. this.setInputsInline(true);
  1994. this.setOutput(true, null);
  1995. this.setColour(GAMEPAD_COLOR);
  1996. var thisBlock = this;
  1997. this.setTooltip(function () {
  1998. var mode = thisBlock.getFieldValue('type');
  1999. var TOOLTIPS = {
  2000. 'x': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "X"),
  2001. 'y': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "Y"),
  2002. 'a': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "A"),
  2003. 'b': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "B"),
  2004. 'l': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "L"),
  2005. 'r': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "R")
  2006. };
  2007. return TOOLTIPS[mode];
  2008. });
  2009. this.setHelpUrl("");
  2010. }
  2011. };
  2012. Blockly.Python['x_iot_read_gamepad_button_released'] = function (block) {
  2013. var dropdown_type = block.getFieldValue('type');
  2014. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  2015. 'from machine import ADC, Pin\n' +
  2016. 'import time\n' +
  2017. 'from machine import I2C\n' +
  2018. 'from ads1x15 import ADS1115\n' +
  2019. '\n';
  2020. Blockly.Python.definitions_['gamepad_button_def_iot'] = '' +
  2021. '_px = Pin(36, Pin.IN,Pin.PULL_UP)\n' +
  2022. '_py = Pin(39, Pin.IN,Pin.PULL_UP)\n' +
  2023. '_pa = Pin(4, Pin.IN,Pin.PULL_UP)\n' +
  2024. '_pb = Pin(2, Pin.IN,Pin.PULL_UP)\n' +
  2025. '_pl = Pin(17, Pin.IN,Pin.PULL_UP)\n' +
  2026. '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' +
  2027. '\n';
  2028. // TODO: Assemble Python into code variable.
  2029. var code = '_p' + dropdown_type + '.value() == 0';
  2030. // TODO: Change ORDER_NONE to the correct strength.
  2031. return [code, Blockly.Python.ORDER_NONE];
  2032. };
  2033. Blockly.Blocks['x_iot_read_gamepad_button_x-r'] = {
  2034. init: function () {
  2035. this.appendDummyInput()
  2036. .appendField(Blockly.Msg.x_gamepad_read_button_status_first_text_iot)
  2037. .appendField(new Blockly.FieldDropdown([
  2038. ["X", "x"],
  2039. ["Y", "y"],
  2040. ["A", "a"],
  2041. ["B", "b"],
  2042. ["L", "l"],
  2043. ["R", "r"]
  2044. ]), "type")
  2045. .appendField(Blockly.Msg.x_gamepad_read_button_status_after_text_iot);
  2046. this.setInputsInline(true);
  2047. this.setOutput(true, null);
  2048. this.setColour(GAMEPAD_COLOR);
  2049. var thisBlock = this;
  2050. this.setTooltip(function () {
  2051. var mode = thisBlock.getFieldValue('type');
  2052. var TOOLTIPS = {
  2053. 'x': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "X"),
  2054. 'y': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "Y"),
  2055. 'a': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "A"),
  2056. 'b': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "B"),
  2057. 'l': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "L"),
  2058. 'r': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "R")
  2059. };
  2060. return TOOLTIPS[mode];
  2061. });
  2062. this.setHelpUrl("");
  2063. }
  2064. };
  2065. Blockly.Python['x_iot_read_gamepad_button_x-r'] = function (block) {
  2066. var dropdown_type = block.getFieldValue('type');
  2067. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  2068. 'from machine import ADC, Pin\n' +
  2069. 'import time\n' +
  2070. 'from machine import I2C\n' +
  2071. 'from ads1x15 import ADS1115\n' +
  2072. '\n';
  2073. Blockly.Python.definitions_['gamepad_button_def_iot'] = '' +
  2074. '_px = Pin(36, Pin.IN,Pin.PULL_UP)\n' +
  2075. '_py = Pin(39, Pin.IN,Pin.PULL_UP)\n' +
  2076. '_pa = Pin(4, Pin.IN,Pin.PULL_UP)\n' +
  2077. '_pb = Pin(2, Pin.IN,Pin.PULL_UP)\n' +
  2078. '_pl = Pin(17, Pin.IN,Pin.PULL_UP)\n' +
  2079. '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' +
  2080. '\n';
  2081. // TODO: Assemble Python into code variable.
  2082. var code = '_p' + dropdown_type + '.value()';
  2083. // TODO: Change ORDER_NONE to the correct strength.
  2084. return [code, Blockly.Python.ORDER_NONE];
  2085. };
  2086. Blockly.Blocks['x_iot_read_gamepad_joystick'] = {
  2087. init: function () {
  2088. this.appendDummyInput()
  2089. .appendField(Blockly.Msg.x_gamepad_read_joystick_read_iot)
  2090. .appendField(new Blockly.FieldDropdown([
  2091. [Blockly.Msg.x_gamepad_read_joystick_read_iot_x, "X"],
  2092. [Blockly.Msg.x_gamepad_read_joystick_read_iot_y, "Y"]
  2093. ]), "type")
  2094. .appendField(Blockly.Msg.x_gamepad_read_joystick_read_after_iot);
  2095. this.setInputsInline(true);
  2096. this.setOutput(true, null);
  2097. this.setColour(GAMEPAD_COLOR);
  2098. var thisBlock = this;
  2099. this.setTooltip(function () {
  2100. var mode = thisBlock.getFieldValue('type');
  2101. var TOOLTIPS = {
  2102. 'X': Blockly.Msg.x_iot_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_x),
  2103. 'Y': Blockly.Msg.x_iot_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_y)
  2104. };
  2105. return TOOLTIPS[mode];
  2106. });
  2107. this.setHelpUrl("");
  2108. }
  2109. };
  2110. Blockly.Python['x_iot_read_gamepad_joystick'] = function (block) {
  2111. var dropdown_type = block.getFieldValue('type');
  2112. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  2113. 'from machine import ADC, Pin\n' +
  2114. 'import time\n' +
  2115. 'from machine import I2C\n' +
  2116. 'from ads1x15 import ADS1115\n' +
  2117. '\n';
  2118. Blockly.Python.definitions_['gamepad_joystick_def_iot'] = '' +
  2119. '_adc_i2c = I2C(scl=Pin(22), sda=Pin(21))\n' +
  2120. '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
  2121. '\n';
  2122. Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' +
  2123. 'def map_number_scale(val):\n' +
  2124. ' return int(((val - 0) / (26400-0)) * (1023-0) + 0)\n' +
  2125. '\n' +
  2126. 'def _read_direction():\n' +
  2127. ' _adcX = map_number_scale(_joystick_adc.read(channel1=1))\n' +
  2128. ' _adcY = map_number_scale(_joystick_adc.read(channel1=0))\n' +
  2129. ' if (_adcY > 38) and (_adcY < 968) and (_adcX > 38) and (_adcX < 968):\n' +
  2130. ' return "still"\n' +
  2131. ' elif _adcX > 968:\n' +
  2132. ' return "left"\n' +
  2133. ' elif _adcX < 38:\n' +
  2134. ' return "right"\n' +
  2135. ' elif _adcY > 968:\n' +
  2136. ' return "top"\n' +
  2137. ' elif _adcY < 38:\n' +
  2138. ' return "bottom"\n' +
  2139. '\n';
  2140. // TODO: Assemble Python into code variable.
  2141. if (dropdown_type == "X") {
  2142. var code = 'map_number_scale(_joystick_adc.read(channel1=1))';
  2143. } else if (dropdown_type == "Y") {
  2144. var code = 'map_number_scale(_joystick_adc.read(channel1=0))';
  2145. }
  2146. // TODO: Change ORDER_NONE to the correct strength.
  2147. return [code, Blockly.Python.ORDER_NONE];
  2148. };
  2149. Blockly.Blocks['x_iot_read_gamepad_joystick_check'] = {
  2150. init: function () {
  2151. this.appendDummyInput()
  2152. .appendField(Blockly.Msg.x_gamepad_read_joystick_when_iot)
  2153. .appendField(new Blockly.FieldDropdown([
  2154. [Blockly.Msg.x_gamepad_read_joystick_when_top_iot, "top"],
  2155. [Blockly.Msg.x_gamepad_read_joystick_when_bottom_iot, "bottom"],
  2156. [Blockly.Msg.x_gamepad_read_joystick_when_left_iot, "left"],
  2157. [Blockly.Msg.x_gamepad_read_joystick_when_right_iot, "right"],
  2158. [Blockly.Msg.x_gamepad_read_joystick_when_not_iot, "still"],
  2159. ]), "type");
  2160. this.setInputsInline(true);
  2161. this.setOutput(true, null);
  2162. this.setColour(GAMEPAD_COLOR);
  2163. var thisBlock = this;
  2164. this.setTooltip(function () {
  2165. var mode = thisBlock.getFieldValue('type');
  2166. var TOOLTIPS = {
  2167. 'top': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_top_iot),
  2168. 'bottom': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_bottom_iot),
  2169. 'left': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_left_iot),
  2170. 'right': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_right_iot),
  2171. 'still': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_not_iot)
  2172. };
  2173. return TOOLTIPS[mode];
  2174. });
  2175. this.setHelpUrl("");
  2176. }
  2177. };
  2178. Blockly.Python['x_iot_read_gamepad_joystick_check'] = function (block) {
  2179. var dropdown_type = block.getFieldValue('type');
  2180. Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
  2181. 'from machine import ADC, Pin\n' +
  2182. 'import time\n' +
  2183. 'from machine import I2C\n' +
  2184. 'from ads1x15 import ADS1115\n' +
  2185. '\n';
  2186. Blockly.Python.definitions_['gamepad_joystick_def_iot'] = '' +
  2187. '_adc_i2c = I2C(scl=Pin(22), sda=Pin(21))\n' +
  2188. '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
  2189. '\n';
  2190. Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' +
  2191. 'def map_number_scale(val):\n' +
  2192. ' return int(((val - 0) / (26400-0)) * (1023-0) + 0)\n' +
  2193. '\n' +
  2194. 'def _read_direction():\n' +
  2195. ' _adcX = map_number_scale(_joystick_adc.read(channel1=1))\n' +
  2196. ' _adcY = map_number_scale(_joystick_adc.read(channel1=0))\n' +
  2197. ' if (_adcY > 38) and (_adcY < 968) and (_adcX > 38) and (_adcX < 968):\n' +
  2198. ' return "still"\n' +
  2199. ' elif _adcX > 968:\n' +
  2200. ' return "left"\n' +
  2201. ' elif _adcX < 38:\n' +
  2202. ' return "right"\n' +
  2203. ' elif _adcY > 968:\n' +
  2204. ' return "top"\n' +
  2205. ' elif _adcY < 38:\n' +
  2206. ' return "bottom"\n' +
  2207. '\n';
  2208. // TODO: Assemble Python into code variable.
  2209. var code = '_read_direction() == "' + dropdown_type + '"';
  2210. // TODO: Change ORDER_NONE to the correct strength.
  2211. return [code, Blockly.Python.ORDER_NONE];
  2212. };
  2213. Blockly.Blocks['x_ai_read_gamepad_button_pressed'] = {
  2214. init: function () {
  2215. this.appendDummyInput()
  2216. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_ai)
  2217. .appendField(new Blockly.FieldDropdown([
  2218. ["X", "x"],
  2219. ["Y", "y"],
  2220. ["A", "a"],
  2221. ["B", "b"],
  2222. ["L", "l"],
  2223. ["R", "r"]
  2224. ]), "type")
  2225. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_pressed_ai);
  2226. this.setInputsInline(true);
  2227. this.setOutput(true, null);
  2228. this.setColour(GAMEPAD_COLOR);
  2229. var thisBlock = this;
  2230. this.setTooltip(function () {
  2231. var mode = thisBlock.getFieldValue('type');
  2232. var TOOLTIPS = {
  2233. 'x': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "X"),
  2234. 'y': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "Y"),
  2235. 'a': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "A"),
  2236. 'b': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "B"),
  2237. 'l': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "L"),
  2238. 'r': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "R")
  2239. };
  2240. return TOOLTIPS[mode];
  2241. });
  2242. this.setHelpUrl("");
  2243. }
  2244. };
  2245. Blockly.Python['x_ai_read_gamepad_button_pressed'] = function (block) {
  2246. var dropdown_type = block.getFieldValue('type');
  2247. Blockly.Python.definitions_['gamepad_all_def'] = '' +
  2248. 'from fpioa_manager import *\n' +
  2249. 'from Maix import FPIOA, GPIO\n' +
  2250. '\n';
  2251. Blockly.Python.definitions_['gamepad_button_def'] = '' +
  2252. '_gp_side_button = [13, 14]\n' +
  2253. '_gp_right_button = [22, 21, 15, 17]\n' +
  2254. 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' +
  2255. 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' +
  2256. 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' +
  2257. 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' +
  2258. 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' +
  2259. 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' +
  2260. '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' +
  2261. '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' +
  2262. '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' +
  2263. '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' +
  2264. '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' +
  2265. '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' +
  2266. '\n';
  2267. // TODO: Assemble Python into code variable.
  2268. var code = '_gp_button_' + dropdown_type + '.value() == 1';
  2269. // TODO: Change ORDER_NONE to the correct strength.
  2270. return [code, Blockly.Python.ORDER_NONE];
  2271. };
  2272. Blockly.Blocks['x_ai_read_gamepad_button_released'] = {
  2273. init: function () {
  2274. this.appendDummyInput()
  2275. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_ai)
  2276. .appendField(new Blockly.FieldDropdown([
  2277. ["X", "x"],
  2278. ["Y", "y"],
  2279. ["A", "a"],
  2280. ["B", "b"],
  2281. ["L", "l"],
  2282. ["R", "r"]
  2283. ]), "type")
  2284. .appendField(Blockly.Msg.x_gamepad_read_button_first_text_released_ai);
  2285. this.setInputsInline(true);
  2286. this.setOutput(true, null);
  2287. this.setColour(GAMEPAD_COLOR);
  2288. var thisBlock = this;
  2289. this.setTooltip(function () {
  2290. var mode = thisBlock.getFieldValue('type');
  2291. var TOOLTIPS = {
  2292. 'x': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "X"),
  2293. 'y': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "Y"),
  2294. 'a': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "A"),
  2295. 'b': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "B"),
  2296. 'l': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "L"),
  2297. 'r': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "R")
  2298. };
  2299. return TOOLTIPS[mode];
  2300. });
  2301. this.setHelpUrl("");
  2302. }
  2303. };
  2304. Blockly.Python['x_ai_read_gamepad_button_released'] = function (block) {
  2305. var dropdown_type = block.getFieldValue('type');
  2306. Blockly.Python.definitions_['gamepad_all_def'] = '' +
  2307. 'from fpioa_manager import *\n' +
  2308. 'from Maix import FPIOA, GPIO\n' +
  2309. '\n';
  2310. Blockly.Python.definitions_['gamepad_button_def'] = '' +
  2311. '_gp_side_button = [13, 14]\n' +
  2312. '_gp_right_button = [15, 17, 22, 21]\n' +
  2313. 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' +
  2314. 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' +
  2315. 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' +
  2316. 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' +
  2317. 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' +
  2318. 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' +
  2319. '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' +
  2320. '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' +
  2321. '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' +
  2322. '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' +
  2323. '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' +
  2324. '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' +
  2325. '\n';
  2326. // TODO: Assemble Python into code variable.
  2327. var code = '_gp_button_' + dropdown_type + '.value() == 0';
  2328. // TODO: Change ORDER_NONE to the correct strength.
  2329. return [code, Blockly.Python.ORDER_NONE];
  2330. };
  2331. Blockly.Blocks['x_ai_read_gamepad_button_x-r'] = {
  2332. init: function () {
  2333. this.appendDummyInput()
  2334. .appendField(Blockly.Msg.x_gamepad_read_button_status_first_text_ai)
  2335. .appendField(new Blockly.FieldDropdown([
  2336. ["X", "x"],
  2337. ["Y", "y"],
  2338. ["A", "a"],
  2339. ["B", "b"],
  2340. ["L", "l"],
  2341. ["R", "r"]
  2342. ]), "type")
  2343. .appendField(Blockly.Msg.x_gamepad_read_button_status_after_text_ai);
  2344. this.setInputsInline(true);
  2345. this.setOutput(true, null);
  2346. this.setColour(GAMEPAD_COLOR);
  2347. var thisBlock = this;
  2348. this.setTooltip(function () {
  2349. var mode = thisBlock.getFieldValue('type');
  2350. var TOOLTIPS = {
  2351. 'x': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "X"),
  2352. 'y': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "Y"),
  2353. 'a': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "A"),
  2354. 'b': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "B"),
  2355. 'l': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "L"),
  2356. 'r': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "R")
  2357. };
  2358. return TOOLTIPS[mode];
  2359. });
  2360. this.setHelpUrl("");
  2361. }
  2362. };
  2363. Blockly.Python['x_ai_read_gamepad_button_x-r'] = function (block) {
  2364. var dropdown_type = block.getFieldValue('type');
  2365. Blockly.Python.definitions_['gamepad_all_def'] = '' +
  2366. 'from fpioa_manager import *\n' +
  2367. 'from Maix import FPIOA, GPIO\n' +
  2368. '\n';
  2369. Blockly.Python.definitions_['gamepad_button_def'] = '' +
  2370. '_gp_side_button = [13, 14]\n' +
  2371. '_gp_right_button = [15, 17, 22, 21]\n' +
  2372. 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' +
  2373. 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' +
  2374. 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' +
  2375. 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' +
  2376. 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' +
  2377. 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' +
  2378. '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' +
  2379. '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' +
  2380. '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' +
  2381. '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' +
  2382. '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' +
  2383. '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' +
  2384. '\n';
  2385. // TODO: Assemble Python into code variable.
  2386. var code = '_gp_button_' + dropdown_type + '.value()';
  2387. // TODO: Change ORDER_NONE to the correct strength.
  2388. return [code, Blockly.Python.ORDER_NONE];
  2389. };
  2390. Blockly.Blocks['x_ai_read_gamepad_joystick'] = {
  2391. init: function () {
  2392. this.appendDummyInput()
  2393. .appendField(Blockly.Msg.x_gamepad_read_joystick_read_ai)
  2394. .appendField(new Blockly.FieldDropdown([
  2395. [Blockly.Msg.x_gamepad_read_joystick_read_iot_x, "0"],
  2396. [Blockly.Msg.x_gamepad_read_joystick_read_iot_y, "1"]
  2397. ]), "type")
  2398. .appendField(Blockly.Msg.x_gamepad_read_joystick_read_after_ai);
  2399. this.setInputsInline(true);
  2400. this.setOutput(true, null);
  2401. this.setColour(GAMEPAD_COLOR);
  2402. var thisBlock = this;
  2403. this.setTooltip(function () {
  2404. var mode = thisBlock.getFieldValue('type');
  2405. var TOOLTIPS = {
  2406. '0': Blockly.Msg.x_ai_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_x),
  2407. '1': Blockly.Msg.x_ai_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_y)
  2408. };
  2409. return TOOLTIPS[mode];
  2410. });
  2411. this.setHelpUrl("");
  2412. }
  2413. };
  2414. Blockly.Python['x_ai_read_gamepad_joystick'] = function (block) {
  2415. var dropdown_type = block.getFieldValue('type');
  2416. Blockly.Python.definitions_['gamepad_joystick_def'] = '' +
  2417. 'from machine import I2C\n' +
  2418. 'from ads1x15 import ADS1115\n' +
  2419. '\n' +
  2420. '_adc_i2c = I2C(I2C.I2C0, freq=100000, scl=30, sda=29)\n' +
  2421. '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
  2422. '\n';
  2423. // TODO: Assemble Python into code variable.
  2424. var code = 'int(_joystick_adc.read(channel1=' + dropdown_type + ')/25.75)';
  2425. // TODO: Change ORDER_NONE to the correct strength.
  2426. return [code, Blockly.Python.ORDER_NONE];
  2427. };
  2428. Blockly.Blocks['x_ai_read_gamepad_joystick_check'] = {
  2429. init: function () {
  2430. this.appendDummyInput()
  2431. .appendField(Blockly.Msg.x_gamepad_read_joystick_when_ai)
  2432. .appendField(new Blockly.FieldDropdown([
  2433. [Blockly.Msg.x_gamepad_read_joystick_when_top_ai, "top"],
  2434. [Blockly.Msg.x_gamepad_read_joystick_when_bottom_ai, "bottom"],
  2435. [Blockly.Msg.x_gamepad_read_joystick_when_left_ai, "left"],
  2436. [Blockly.Msg.x_gamepad_read_joystick_when_right_ai, "right"],
  2437. [Blockly.Msg.x_gamepad_read_joystick_when_not_ai, "still"],
  2438. ]), "type");
  2439. this.setInputsInline(true);
  2440. this.setOutput(true, null);
  2441. this.setColour(GAMEPAD_COLOR);
  2442. var thisBlock = this;
  2443. this.setTooltip(function () {
  2444. var mode = thisBlock.getFieldValue('type');
  2445. var TOOLTIPS = {
  2446. 'top': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_top_ai),
  2447. 'bottom': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_bottom_ai),
  2448. 'left': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_left_ai),
  2449. 'right': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_right_ai),
  2450. 'still': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_not_ai)
  2451. };
  2452. return TOOLTIPS[mode];
  2453. });
  2454. this.setHelpUrl("");
  2455. }
  2456. };
  2457. Blockly.Python['x_ai_read_gamepad_joystick_check'] = function (block) {
  2458. var dropdown_type = block.getFieldValue('type');
  2459. Blockly.Python.definitions_['gamepad_joystick_def'] = '' +
  2460. 'from machine import I2C\n' +
  2461. 'from ads1x15 import ADS1115\n' +
  2462. '\n' +
  2463. '_adc_i2c = I2C(I2C.I2C0, freq=100000, scl=30, sda=29)\n' +
  2464. '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
  2465. '\n';
  2466. // TODO: Assemble Python into code variable.
  2467. '\n';
  2468. Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' +
  2469. 'def _read_direction():\n' +
  2470. ' 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' +
  2471. ' return "still" \n' +
  2472. ' elif int(_joystick_adc.read(channel1=1)/25.75) > 600:\n' +
  2473. ' return "left"\n' +
  2474. ' elif int(_joystick_adc.read(channel1=1)/25.75) < 400:\n' +
  2475. ' return "right"\n' +
  2476. ' elif int(_joystick_adc.read(channel1=0)/25.75) > 600:\n' +
  2477. ' return "top"\n' +
  2478. ' elif int(_joystick_adc.read(channel1=0)/25.75) < 400:\n' +
  2479. ' return "bottom"\n' +
  2480. '\n';
  2481. // TODO: Assemble Python into code variable.
  2482. var code = '_read_direction() == "' + dropdown_type + '"';
  2483. // TODO: Change ORDER_NONE to the correct strength.
  2484. return [code, Blockly.Python.ORDER_NONE];
  2485. };
  2486. /*
  2487. __ ___ _____ _____
  2488. /\ \ \/ __\ \_ \___/__ \
  2489. / \/ /__\//_____ / /\/ _ \ / /\/
  2490. / /\ / \/ \_____/\/ /_| (_) / /
  2491. \_\ \/\_____/ \____/ \___/\/
  2492. Blockly.Python.addVariable('', '', true);
  2493. Blockly.Python.definitions_['definition'] = '';
  2494. Blockly.Python.addSetup("setup", '');
  2495. */
  2496. var ESP32_NBIOT_COLOR = "#629978";
  2497. Blockly.Blocks['esp32_nbiot_setup'] = {
  2498. init: function () {
  2499. let type = getLocalStorage("type")
  2500. this.appendDummyInput()
  2501. .appendField(new Blockly.FieldImage("blockly/media/nbiot_esp32_header.png", 110, 40, { alt: "*", flipRtl: "FALSE" }));
  2502. type == 1 ? this.appendDummyInput()
  2503. .appendField(new Blockly.FieldDropdown([
  2504. [Blockly.Msg.nbiot_module_which_module_ai, "ai"],
  2505. [Blockly.Msg.nbiot_module_which_module_iot, "iot"]
  2506. ]), "module_type") : this.appendDummyInput()
  2507. .appendField(new Blockly.FieldDropdown([
  2508. [Blockly.Msg.nbiot_module_which_module_iot, "iot"],
  2509. [Blockly.Msg.nbiot_module_which_module_ai, "ai"]
  2510. ]), "module_type");
  2511. this.appendDummyInput()
  2512. .appendField(Blockly.Msg.nbiot_module_init);
  2513. this.setPreviousStatement(true, null);
  2514. this.setNextStatement(true, null);
  2515. this.setColour(ESP32_NBIOT_COLOR);
  2516. var thisBlock = this;
  2517. this.setTooltip(function () {
  2518. var mode = thisBlock.getFieldValue('module_type');
  2519. var TOOLTIPS = {
  2520. 'ai': Blockly.Msg.esp32_nbiot_setup_TOOLTIP.replace('%1', Blockly.Msg.nbiot_module_which_module_ai),
  2521. 'iot': Blockly.Msg.esp32_nbiot_setup_TOOLTIP.replace('%1', Blockly.Msg.nbiot_module_which_module_iot)
  2522. };
  2523. return TOOLTIPS[mode];
  2524. });
  2525. this.setHelpUrl("");
  2526. }
  2527. };
  2528. Blockly.Python['esp32_nbiot_setup'] = function (block) {
  2529. // TODO: Assemble Python into code variable.
  2530. var dropdown_module_type = block.getFieldValue('module_type');
  2531. if (dropdown_module_type == "iot") {
  2532. Blockly.Python.addVariable('_nbiot_serial_read', '_nbiot_serial_read = ""', true);
  2533. Blockly.Python.addVariable('_nbiot_readings', '_nbiot_readings = ""', true);
  2534. Blockly.Python.addVariable('_nbiot_power_on_status', '_nbiot_power_on_status = False', true);
  2535. Blockly.Python.addVariable('_nbiot_internet_status', '_nbiot_internet_status = False', true);
  2536. Blockly.Python.addVariable('_nbiot_internet_ip_address', '_nbiot_internet_ip_address = ""', true);
  2537. Blockly.Python.definitions_['import_definition'] = '' +
  2538. 'from machine import UART\n' +
  2539. 'from machine import Timer\n' +
  2540. 'import ubinascii as binascii\n' +
  2541. 'import time\n' +
  2542. '\n' +
  2543. '_nbiot_serial = UART(2,rx=16, tx=17, baudrate=115200, timeout=10)\n' +
  2544. '\n' +
  2545. '_nbiot_serial_timer = Timer(1)\n' +
  2546. '_nbiot_serial_timer.init(period=50, mode=Timer.PERIODIC, callback=lambda t: _read_nbiot_serial(_nbiot_serial))\n' +
  2547. '\n' +
  2548. 'def _read_nbiot_serial(uart):\n' +
  2549. ' global _nbiot_readings, _nbiot_power_on_status, _nbiot_internet_status, _nbiot_internet_ip_address\n' +
  2550. ' if _nbiot_serial.any():\n' +
  2551. ' _nbiot_serial_read = _nbiot_serial.readline().decode("utf-8")\n' +
  2552. ' _nbiot_serial_read = _nbiot_serial_read.strip()\n' +
  2553. ' _nbiot_readings = _nbiot_serial_read.strip()\n' +
  2554. '\n' +
  2555. ' if _nbiot_serial_read == "+CPIN: READY":\n' +
  2556. ' _nbiot_power_on_status = True\n' +
  2557. ' print("NB-IoT Module Initiated!\\n")\n' +
  2558. ' _nbiot_serial.write(b\'AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n\')\n' +
  2559. '\n' +
  2560. ' if "+IP" in _nbiot_serial_read:\n' +
  2561. ' _nbiot_internet_status = True\n' +
  2562. ' _nbiot_internet_ip_address = _nbiot_serial_read[5:len(_nbiot_serial_read)]\n' +
  2563. '';
  2564. var code = '';
  2565. } else if (dropdown_module_type == "ai") {
  2566. Blockly.Python.addVariable('_nbiot_serial_read', '_nbiot_serial_read = ""', true);
  2567. Blockly.Python.addVariable('_nbiot_readings', '_nbiot_readings = ""', true);
  2568. Blockly.Python.addVariable('_nbiot_power_on_status', '_nbiot_power_on_status = False', true);
  2569. Blockly.Python.addVariable('_nbiot_internet_status', '_nbiot_internet_status = False', true);
  2570. Blockly.Python.addVariable('_nbiot_internet_ip_address', '_nbiot_internet_ip_address = ""', true);
  2571. Blockly.Python.definitions_['import_definition'] = '' +
  2572. 'import machine, time\n' +
  2573. 'from fpioa_manager import fm\n' +
  2574. 'from machine import Timer\n' +
  2575. '\n' +
  2576. 'fm.register(13,fm.fpioa.UART2_TX)\n' +
  2577. 'fm.register(14,fm.fpioa.UART2_RX)\n' +
  2578. '\n' +
  2579. '_nbiot_serial = machine.UART(machine.UART.UART2,115200, 8, None, 1, timeout=50)\n' +
  2580. '\n' +
  2581. 'def _on_timer(timer):\n' +
  2582. ' _read_nbiot_serial(_nbiot_serial)\n' +
  2583. '\n' +
  2584. '_periodic_tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=1, callback=_on_timer, arg=_on_timer)\n' +
  2585. '_periodic_tim.start()\n' +
  2586. '\n' +
  2587. 'def _read_nbiot_serial(uart):\n' +
  2588. ' global _nbiot_readings, _nbiot_power_on_status, _nbiot_internet_status, _nbiot_internet_ip_address\n' +
  2589. ' if _nbiot_serial.any():\n' +
  2590. ' _nbiot_serial_read = _nbiot_serial.readline().decode("utf-8")\n' +
  2591. ' print(_nbiot_serial_read)\n' +
  2592. ' _nbiot_serial_read = _nbiot_serial_read.strip()\n' +
  2593. ' _nbiot_readings = _nbiot_serial_read.strip()\n' +
  2594. '\n' +
  2595. ' if _nbiot_serial_read == "+CPIN: READY":\n' +
  2596. ' _nbiot_power_on_status = True\n' +
  2597. ' print("NB-IoT Module Initiated!\\n")\n' +
  2598. ' _nbiot_serial.write(b\'AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n\')\n' +
  2599. '\n' +
  2600. ' if "+IP" in _nbiot_serial_read:\n' +
  2601. ' _nbiot_internet_status = True\n' +
  2602. ' _nbiot_internet_ip_address = _nbiot_serial_read[5:len(_nbiot_serial_read)]\n' +
  2603. ' print("NB-IoT is Online! Public IP is: " + str(_nbiot_internet_ip_address))\n' +
  2604. '\n' +
  2605. '_nbiot_serial_read = ""\n' +
  2606. '_nbiot_readings = ""\n' +
  2607. '_nbiot_power_on_status = False\n' +
  2608. '_nbiot_internet_status = False\n' +
  2609. '_nbiot_internet_ip_address = ""\n' +
  2610. '';
  2611. var code = '';
  2612. }
  2613. return code;
  2614. };
  2615. Blockly.Blocks['esp32_nbiot_get_power_status'] = {
  2616. init: function () {
  2617. this.appendDummyInput()
  2618. .appendField(Blockly.Msg.nbiot_module_power);
  2619. this.setOutput(true, null);
  2620. this.setColour(ESP32_NBIOT_COLOR);
  2621. this.setTooltip(Blockly.Msg.esp32_nbiot_get_power_status_TOOLTIP);
  2622. this.setHelpUrl("");
  2623. }
  2624. };
  2625. Blockly.Python['esp32_nbiot_get_power_status'] = function (block) {
  2626. // TODO: Assemble Python into code variable.
  2627. var code = '_nbiot_power_on_status';
  2628. // TODO: Change ORDER_NONE to the correct strength.
  2629. return [code, Blockly.Python.ORDER_NONE];
  2630. };
  2631. Blockly.Blocks['esp32_nbiot_get_network_status'] = {
  2632. init: function () {
  2633. this.appendDummyInput()
  2634. .appendField(Blockly.Msg.nbiot_module_connectivity);
  2635. this.setOutput(true, null);
  2636. this.setColour(ESP32_NBIOT_COLOR);
  2637. this.setTooltip(Blockly.Msg.esp32_nbiot_get_network_status_TOOLTIP);
  2638. this.setHelpUrl("");
  2639. }
  2640. };
  2641. Blockly.Python['esp32_nbiot_get_network_status'] = function (block) {
  2642. // TODO: Assemble Python into code variable.
  2643. var code = '_nbiot_internet_status';
  2644. // TODO: Change ORDER_NONE to the correct strength.
  2645. return [code, Blockly.Python.ORDER_NONE];
  2646. };
  2647. Blockly.Blocks['esp32_nbiot_get_network_ip'] = {
  2648. init: function () {
  2649. this.appendDummyInput()
  2650. .appendField(Blockly.Msg.nbiot_module_ip);
  2651. this.setOutput(true, null);
  2652. this.setColour(ESP32_NBIOT_COLOR);
  2653. this.setTooltip(Blockly.Msg.esp32_nbiot_get_network_ip_TOOLTIP);
  2654. this.setHelpUrl("");
  2655. }
  2656. };
  2657. Blockly.Python['esp32_nbiot_get_network_ip'] = function (block) {
  2658. // TODO: Assemble Python into code variable.
  2659. var code = '_nbiot_internet_ip_address';
  2660. // TODO: Change ORDER_NONE to the correct strength.
  2661. return [code, Blockly.Python.ORDER_NONE];
  2662. };
  2663. Blockly.Blocks['esp32_nbiot_onenet_init'] = {
  2664. init: function () {
  2665. this.appendDummyInput()
  2666. .appendField(new Blockly.FieldImage("blockly/media/nbiot_onenet.png", 200, 60, { alt: "*", flipRtl: "FALSE" }));
  2667. this.appendDummyInput()
  2668. .appendField(Blockly.Msg.nbiot_onenet_title);
  2669. this.appendValueInput("product_id")
  2670. .setCheck(null)
  2671. .appendField(Blockly.Msg.nbiot_onenet_connect_productid);
  2672. this.appendValueInput("device_id")
  2673. .setCheck(null)
  2674. .appendField(Blockly.Msg.nbiot_onenet_connect_deviceid);
  2675. this.appendValueInput("device_apikey")
  2676. .setCheck(null)
  2677. .appendField(Blockly.Msg.nbiot_onenet_connect_device_api);
  2678. this.setPreviousStatement(true, null);
  2679. this.setNextStatement(true, null);
  2680. this.setColour(ONENET_BLOCK_COLOR);
  2681. this.setTooltip(Blockly.Msg.esp32_nbiot_onenet_init_TOOLTIP);
  2682. this.setHelpUrl("");
  2683. }
  2684. };
  2685. Blockly.Python['esp32_nbiot_onenet_init'] = function (block) {
  2686. var value_product_id = Blockly.Python.valueToCode(block, 'product_id', Blockly.Python.ORDER_ATOMIC);
  2687. var value_device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
  2688. var value_device_apikey = Blockly.Python.valueToCode(block, 'device_apikey', Blockly.Python.ORDER_ATOMIC);
  2689. // TODO: Assemble Python into code variable.
  2690. Blockly.Python.addVariable('_nbiot_onenet_product_id', '', true);
  2691. Blockly.Python.addVariable('_nbiot_onenet_device_id', '', true);
  2692. Blockly.Python.addVariable('_nbiot_onent_device_apikey', '', true);
  2693. Blockly.Python.addVariable('_nbiot_onenet_connection_status', '', true);
  2694. Blockly.Python.addVariable('_nbiot_onenet_read_anything', '', true);
  2695. Blockly.Python.addVariable('_nbiot_onenet_init_state', '', true);
  2696. Blockly.Python.addSetup("setup", '' +
  2697. '_nbiot_onenet_init_state = False\n' +
  2698. '_nbiot_onenet_connection_status = False\n' +
  2699. '_nbiot_onenet_read_anything = False\n' +
  2700. '_nbiot_onenet_product_id = ' + value_product_id + '\n' +
  2701. '_nbiot_onenet_device_id = ' + value_device_id + '\n' +
  2702. '_nbiot_onent_device_apikey = ' + value_device_apikey + '\n' +
  2703. '\n' +
  2704. 'def _nbiot_send_onenet_data(data):\n' +
  2705. ' _nbiot_send_message = data\n' +
  2706. ' _nbiot_send_data_content = binascii.hexlify(_nbiot_send_message).decode("utf-8")\n' +
  2707. ' _nbiot_send_header = ",0300" + str(hex(len(_nbiot_send_message)).split(\'x\')[-1])\n' +
  2708. ' _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' +
  2709. ' return _nbiot_send_at_content\n' +
  2710. '\n' +
  2711. 'def _nbiot_read_onenet_data(raw_response):\n' +
  2712. ' _nbiot_receive = raw_response\n' +
  2713. ' if "+MQTTPUBLISH" not in raw_response:\n' +
  2714. ' return ""\n' +
  2715. ' elif "+MQTTPUBLISH" in raw_response:\n' +
  2716. ' _nbiot_occur = -1\n' +
  2717. ' for i in range(0, 6): _nbiot_occur = _nbiot_receive.find(",", _nbiot_occur + 1)\n' +
  2718. ' return _nbiot_receive[_nbiot_occur+1:len(_nbiot_receive)]\n' +
  2719. '\n' +
  2720. 'def _read_nbiot_onenet(_incoming_data):\n' +
  2721. ' global _nbiot_onenet_connection_status, _nbiot_onenet_read_anything\n' +
  2722. '\n' +
  2723. ' if "+MQTTOPEN: OK" in _incoming_data:\n' +
  2724. ' _nbiot_onenet_connection_status = True\n' +
  2725. ' # print("OneNET Connected!")\n' +
  2726. ' if "+MQTTOPEN: FAIL" in _incoming_data:\n' +
  2727. ' _nbiot_onenet_connection_status = False\n' +
  2728. ' # print("OneNET Lost Connection!")\n' +
  2729. '\n' +
  2730. ' if "+MQTTPUBLISH" in _incoming_data:\n' +
  2731. ' _nbiot_onenet_read_anything = True\n' +
  2732. ' else:\n' +
  2733. ' _nbiot_onenet_read_anything = False\n' +
  2734. '\n' +
  2735. ' if "+MQTTDISC" in _incoming_data:\n' +
  2736. ' _nbiot_onenet_read_anything = False\n' +
  2737. ' _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' +
  2738. ' print("Enabling OneNET Connection...")\n' +
  2739. ' _nbiot_serial.write(_nbiot_onenet_configuration.encode())\n' +
  2740. ' time.sleep_ms(100)\n' +
  2741. ' _nbiot_serial.write(b\'AT+MQTTOPEN=1,1,0\\r\\n\')\n' +
  2742. '\n' +
  2743. 'def _init_onenet_connection():\n' +
  2744. ' _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' +
  2745. ' print("Enabling OneNET Connection...")\n' +
  2746. ' _nbiot_serial.write(_nbiot_onenet_configuration.encode())\n' +
  2747. ' time.sleep_ms(100)\n' +
  2748. ' _nbiot_serial.write(b\'AT+MQTTOPEN=1,1,0\\r\\n\')\n' +
  2749. ' time.sleep_ms(100)\n' +
  2750. '');
  2751. var code = '' +
  2752. 'if (_nbiot_internet_status == True) and (_nbiot_onenet_init_state == False) and (_nbiot_onenet_connection_status == False):\n' +
  2753. ' _init_onenet_connection()\n' +
  2754. ' _nbiot_onenet_init_state = True\n' +
  2755. '\n' +
  2756. '_read_nbiot_onenet(_nbiot_readings)\n' +
  2757. '';
  2758. return code;
  2759. };
  2760. Blockly.Blocks['esp32_nbiot_onenet_connection_status'] = {
  2761. init: function () {
  2762. this.appendDummyInput()
  2763. .appendField(Blockly.Msg.nbiot_onenet_connectivity);
  2764. this.setOutput(true, null);
  2765. this.setColour(ONENET_BLOCK_COLOR);
  2766. this.setTooltip(Blockly.Msg.esp32_nbiot_onenet_connection_status_TOOLTIP);
  2767. this.setHelpUrl("");
  2768. }
  2769. };
  2770. Blockly.Python['esp32_nbiot_onenet_connection_status'] = function (block) {
  2771. // TODO: Assemble Python into code variable.
  2772. var code = '_nbiot_onenet_connection_status';
  2773. // TODO: Change ORDER_NONE to the correct strength.
  2774. return [code, Blockly.Python.ORDER_NONE];
  2775. };
  2776. Blockly.Blocks['iot_service_nbiot_onenet'] = {
  2777. init: function () {
  2778. this.appendDummyInput()
  2779. .appendField(new Blockly.FieldImage("blockly/media/nbiot_onenet_send.png", 210, 60, { alt: "*", flipRtl: "FALSE" }));
  2780. this.appendDummyInput()
  2781. .appendField(Blockly.Msg.nbiot_onenet_send_title);
  2782. /*this.appendValueInput("first_input")
  2783. .setCheck(null)
  2784. .appendField("Field 0");*/
  2785. this.itemCount_ = 1;
  2786. this.updateShape_();
  2787. this.setMutator(new Blockly.Mutator(['iot_service_nbiot_onenet_create_with_item']));
  2788. this.setColour(ONENET_BLOCK_COLOR);
  2789. this.setPreviousStatement(true, null);
  2790. this.setNextStatement(true, null);
  2791. this.setTooltip(Blockly.Msg.iot_service_nbiot_onenet_TOOLTIP);
  2792. this.setHelpUrl("");
  2793. },
  2794. mutationToDom: function () {
  2795. var container = document.createElement('mutation');
  2796. container.setAttribute('items', this.itemCount_);
  2797. return container;
  2798. },
  2799. domToMutation: function (xmlElement) {
  2800. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  2801. this.updateShape_();
  2802. },
  2803. decompose: function (workspace) {
  2804. var containerBlock = workspace.newBlock('iot_service_nbiot_onenet_create_with_container');
  2805. containerBlock.initSvg();
  2806. var connection = containerBlock.getInput('STACK').connection;
  2807. for (var i = 0; i < this.itemCount_; i++) {
  2808. var itemBlock = workspace.newBlock('iot_service_nbiot_onenet_create_with_item');
  2809. itemBlock.initSvg();
  2810. connection.connect(itemBlock.previousConnection);
  2811. connection = itemBlock.nextConnection;
  2812. }
  2813. return containerBlock;
  2814. },
  2815. compose: function (containerBlock) {
  2816. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2817. // Count number of inputs.
  2818. var connections = [];
  2819. while (itemBlock) {
  2820. connections.push(itemBlock.valueConnection_);
  2821. itemBlock = itemBlock.nextConnection &&
  2822. itemBlock.nextConnection.targetBlock();
  2823. }
  2824. // Disconnect any children that don't belong.
  2825. for (var i = 0; i < this.itemCount_; i++) {
  2826. var connection = this.getInput('ADD' + i).connection.targetConnection;
  2827. if (connection && connections.indexOf(connection) == -1) {
  2828. connection.disconnect();
  2829. }
  2830. }
  2831. this.itemCount_ = connections.length;
  2832. this.updateShape_();
  2833. // Reconnect any child blocks.
  2834. for (var i = 0; i < this.itemCount_; i++) {
  2835. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  2836. }
  2837. },
  2838. saveConnections: function (containerBlock) {
  2839. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2840. var i = 0;
  2841. while (itemBlock) {
  2842. var input = this.getInput('ADD' + i);
  2843. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  2844. i++;
  2845. itemBlock = itemBlock.nextConnection &&
  2846. itemBlock.nextConnection.targetBlock();
  2847. }
  2848. },
  2849. updateShape_: function () {
  2850. for (var i = 0; i < this.itemCount_; i++) {
  2851. if (!this.getInput('ADD' + i)) {
  2852. var input = this.appendValueInput('ADD' + i);
  2853. input.appendField(Blockly.Msg.nbiot_onenet_send_property)
  2854. .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
  2855. //input.appendField("資料 " + (i + 1) + ":");
  2856. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  2857. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  2858. }
  2859. }
  2860. while (this.getInput('ADD' + i)) {
  2861. this.removeInput('ADD' + i);
  2862. i++;
  2863. }
  2864. },
  2865. };
  2866. Blockly.Blocks['iot_service_nbiot_onenet_create_with_container'] = {
  2867. init: function () {
  2868. this.setColour(ONENET_BLOCK_COLOR);
  2869. this.appendDummyInput()
  2870. .appendField("Items");
  2871. this.appendStatementInput('STACK');
  2872. this.setTooltip('');
  2873. this.contextMenu = false;
  2874. }
  2875. };
  2876. Blockly.Blocks['iot_service_nbiot_onenet_create_with_item'] = {
  2877. init: function () {
  2878. this.setColour(ONENET_BLOCK_COLOR);
  2879. this.appendDummyInput()
  2880. .appendField("Field");
  2881. this.setPreviousStatement(true);
  2882. this.setNextStatement(true);
  2883. this.setTooltip('');
  2884. this.contextMenu = false;
  2885. }
  2886. };
  2887. Blockly.Python['iot_service_nbiot_onenet'] = function (block) {
  2888. // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
  2889. var item_field = '',
  2890. item_value = '';
  2891. var nbiot_onenet_data = "'{";
  2892. for (var n = 0; n < this.itemCount_; n++) {
  2893. item_field = block.getFieldValue("field" + n);
  2894. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  2895. nbiot_onenet_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
  2896. }
  2897. var nbiot_onenet_data_final = nbiot_onenet_data + "}'";
  2898. var code = '' +
  2899. '_data_to_be_send = ' + nbiot_onenet_data_final.slice(0, -3) + '}\\r\\n\'' + '\n' +
  2900. '_data_add_rn = _nbiot_send_onenet_data(_data_to_be_send) + "\\r\\n"\n' +
  2901. '_data_for_at = _data_add_rn.encode()\n' +
  2902. '_nbiot_serial.write(_data_for_at)\n' +
  2903. // '_nbiot_serial.write(_nbiot_send_onenet_data(_data_to_be_send).encode())\n' +
  2904. '\n' +
  2905. '';
  2906. return code;
  2907. };
  2908. Blockly.Blocks['iot_service_nbiot_onenet_read'] = {
  2909. init: function () {
  2910. this.appendDummyInput()
  2911. .appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg);
  2912. this.appendValueInput("message")
  2913. .setCheck(null);
  2914. this.appendDummyInput()
  2915. .appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg_when);
  2916. this.appendStatementInput("exec")
  2917. .setCheck(null)
  2918. .appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg_exec);
  2919. this.setPreviousStatement(true, null);
  2920. this.setNextStatement(true, null);
  2921. this.setColour(ONENET_BLOCK_COLOR);
  2922. this.setTooltip(Blockly.Msg.iot_service_nbiot_onenet_read_TOOLTIP);
  2923. this.setHelpUrl("");
  2924. }
  2925. };
  2926. Blockly.Python['iot_service_nbiot_onenet_read'] = function (block) {
  2927. var value_message = Blockly.Python.valueToCode(block, 'message', Blockly.Python.ORDER_ATOMIC);
  2928. var statements_exec = Blockly.Python.statementToCode(block, 'exec');
  2929. // TODO: Assemble Python into code variable.
  2930. var code = 'if (_nbiot_read_onenet_data(_nbiot_readings) == ' + value_message + '):\n' +
  2931. statements_exec +
  2932. '\n';
  2933. return code;
  2934. };
  2935. Blockly.Blocks['esp32_nbiot_onenet_read_value'] = {
  2936. init: function () {
  2937. this.appendDummyInput()
  2938. .appendField(Blockly.Msg.nbiot_onenet_receive_command);
  2939. this.setOutput(true, null);
  2940. this.setColour(ONENET_BLOCK_COLOR);
  2941. this.setTooltip(Blockly.Msg.esp32_nbiot_onenet_read_value_TOOLTIP);
  2942. this.setHelpUrl("");
  2943. }
  2944. };
  2945. Blockly.Python['esp32_nbiot_onenet_read_value'] = function (block) {
  2946. // TODO: Assemble Python into code variable.
  2947. var code = '_nbiot_read_onenet_data(_nbiot_readings)';
  2948. // TODO: Change ORDER_NONE to the correct strength.
  2949. return [code, Blockly.Python.ORDER_NONE];
  2950. };
  2951. Blockly.Blocks['iot_service_nbiot_cococloud_read'] = {
  2952. init: function () {
  2953. this.appendDummyInput()
  2954. .appendField(new Blockly.FieldImage("blockly/media/nbiot_cococloud_get.png", 260, 60, { alt: "*", flipRtl: "FALSE" }));
  2955. this.appendDummyInput()
  2956. .appendField(Blockly.Msg.nbiot_cococloud_get_event_title);
  2957. this.appendDummyInput()
  2958. .appendField(Blockly.Msg.nbiot_cococloud_get_event_api)
  2959. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "t_api");
  2960. this.setPreviousStatement(true, null);
  2961. this.setNextStatement(true, null);
  2962. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2963. this.setTooltip("");
  2964. this.setHelpUrl("");
  2965. }
  2966. };
  2967. Blockly.Python['iot_service_nbiot_cococloud_read'] = function (block) {
  2968. var api = block.getFieldValue('t_api');
  2969. // TODO: Assemble Python into code variable.
  2970. var code = '\n';
  2971. return code;
  2972. };
  2973. Blockly.Blocks['iot_service_nbiot_cococloud_read_data'] = {
  2974. init: function () {
  2975. this.appendDummyInput()
  2976. .appendField(Blockly.Msg.nbiot_cococloud_get_property);
  2977. this.appendValueInput("property")
  2978. .setCheck(null);
  2979. this.appendDummyInput()
  2980. .appendField(Blockly.Msg.nbiot_cococloud_get_data);
  2981. this.setInputsInline(true);
  2982. this.setOutput(true, null);
  2983. this.setColour(COCOCLOUD_BLOCK_COLOR);
  2984. this.setTooltip("");
  2985. this.setHelpUrl("");
  2986. }
  2987. };
  2988. Blockly.Python['iot_service_nbiot_cococloud_read_data'] = function (block) {
  2989. var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC);
  2990. // TODO: Assemble Python into code variable.
  2991. var code = '\n';
  2992. // TODO: Change ORDER_NONE to the correct strength.
  2993. return [code, Blockly.Python.ORDER_NONE];
  2994. };
  2995. Blockly.Blocks['iot_service_nbiot_cococloud'] = {
  2996. init: function () {
  2997. this.appendDummyInput()
  2998. .appendField(new Blockly.FieldImage("blockly/media/nbiot_cococloud_send.png", 260, 60, { alt: "*", flipRtl: "FALSE" }));
  2999. this.appendDummyInput()
  3000. .appendField(Blockly.Msg.nbiot_cococloud_send_title);
  3001. this.appendDummyInput()
  3002. .appendField(Blockly.Msg.nbiot_cococloud_send_api)
  3003. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "t_api");
  3004. /*this.appendValueInput("first_input")
  3005. .setCheck(null)
  3006. .appendField("Field 0");*/
  3007. this.itemCount_ = 1;
  3008. this.updateShape_();
  3009. this.setMutator(new Blockly.Mutator(['iot_service_nbiot_cococloud_create_with_item']));
  3010. this.setColour(COCOCLOUD_BLOCK_COLOR);
  3011. this.setPreviousStatement(true, null);
  3012. this.setNextStatement(true, null);
  3013. this.setTooltip(Blockly.Msg.iot_service_nbiot_cococloud_TOOLTIP);
  3014. this.setHelpUrl("");
  3015. },
  3016. mutationToDom: function () {
  3017. var container = document.createElement('mutation');
  3018. container.setAttribute('items', this.itemCount_);
  3019. return container;
  3020. },
  3021. domToMutation: function (xmlElement) {
  3022. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  3023. this.updateShape_();
  3024. },
  3025. decompose: function (workspace) {
  3026. var containerBlock = workspace.newBlock('iot_service_nbiot_cococloud_create_with_container');
  3027. containerBlock.initSvg();
  3028. var connection = containerBlock.getInput('STACK').connection;
  3029. for (var i = 0; i < this.itemCount_; i++) {
  3030. var itemBlock = workspace.newBlock('iot_service_nbiot_cococloud_create_with_item');
  3031. itemBlock.initSvg();
  3032. connection.connect(itemBlock.previousConnection);
  3033. connection = itemBlock.nextConnection;
  3034. }
  3035. return containerBlock;
  3036. },
  3037. compose: function (containerBlock) {
  3038. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  3039. // Count number of inputs.
  3040. var connections = [];
  3041. while (itemBlock) {
  3042. connections.push(itemBlock.valueConnection_);
  3043. itemBlock = itemBlock.nextConnection &&
  3044. itemBlock.nextConnection.targetBlock();
  3045. }
  3046. // Disconnect any children that don't belong.
  3047. for (var i = 0; i < this.itemCount_; i++) {
  3048. var connection = this.getInput('ADD' + i).connection.targetConnection;
  3049. if (connection && connections.indexOf(connection) == -1) {
  3050. connection.disconnect();
  3051. }
  3052. }
  3053. this.itemCount_ = connections.length;
  3054. this.updateShape_();
  3055. // Reconnect any child blocks.
  3056. for (var i = 0; i < this.itemCount_; i++) {
  3057. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  3058. }
  3059. },
  3060. saveConnections: function (containerBlock) {
  3061. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  3062. var i = 0;
  3063. while (itemBlock) {
  3064. var input = this.getInput('ADD' + i);
  3065. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  3066. i++;
  3067. itemBlock = itemBlock.nextConnection &&
  3068. itemBlock.nextConnection.targetBlock();
  3069. }
  3070. },
  3071. updateShape_: function () {
  3072. for (var i = 0; i < this.itemCount_; i++) {
  3073. if (!this.getInput('ADD' + i)) {
  3074. var input = this.appendValueInput('ADD' + i);
  3075. input.appendField(Blockly.Msg.nbiot_cococloud_send_property)
  3076. .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
  3077. //input.appendField("資料 " + (i + 1) + ":");
  3078. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  3079. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  3080. }
  3081. }
  3082. while (this.getInput('ADD' + i)) {
  3083. this.removeInput('ADD' + i);
  3084. i++;
  3085. }
  3086. },
  3087. };
  3088. Blockly.Blocks['iot_service_nbiot_cococloud_create_with_container'] = {
  3089. init: function () {
  3090. this.setColour(ESP32_NBIOT_COLOR);
  3091. this.appendDummyInput()
  3092. .appendField("Items");
  3093. this.appendStatementInput('STACK');
  3094. this.setTooltip('');
  3095. this.contextMenu = false;
  3096. }
  3097. };
  3098. Blockly.Blocks['iot_service_nbiot_cococloud_create_with_item'] = {
  3099. init: function () {
  3100. this.setColour(COCOCLOUD_BLOCK_COLOR);
  3101. this.appendDummyInput()
  3102. .appendField("Field");
  3103. this.setPreviousStatement(true);
  3104. this.setNextStatement(true);
  3105. this.setTooltip('');
  3106. this.contextMenu = false;
  3107. }
  3108. };
  3109. Blockly.Python['iot_service_nbiot_cococloud'] = function (block) {
  3110. var api = block.getFieldValue('t_api');
  3111. Blockly.Python.addVariable('_nbiot_cococloud_send_init_state', '', true);
  3112. Blockly.Python.addVariable('_nbiot_cococloud_json_content', '', true);
  3113. Blockly.Python.definitions_['import'] = '' +
  3114. '_nbiot_cococloud_send_init_state = False\n' +
  3115. '\n' +
  3116. 'def _nbiot_cococloud_send(data):\n' +
  3117. ' _nbiot_cococloud_send_data = \'AT+HTTPCONTENT=0,\"\'+ data + \'\"\\r\\n\'\n' +
  3118. ' print(_nbiot_cococloud_send_data)\n' +
  3119. ' _nbiot_serial.write(b\'AT+HTTPCLOSE=0\\r\\n\')\n' +
  3120. ' time.sleep_ms(500)\n' +
  3121. ' _nbiot_serial.write(b\'AT+HTTPCREATE=\"http://api.cocorobo.hk/\"\\r\\n\')\n' +
  3122. ' time.sleep_ms(500)\n' +
  3123. ' _nbiot_serial.write(b\'AT+HTTPHEADER=0,\"Content-Type: application/json\\\\r\\\\n\"\\r\\n\')\n' +
  3124. ' time.sleep_ms(500)\n' +
  3125. ' _nbiot_serial.write(_nbiot_cococloud_send_data.encode())\n' +
  3126. ' time.sleep_ms(500)\n' +
  3127. ' _nbiot_serial.write(b\'AT+HTTPSEND=0,1,\"/iot/data/eventAPIKey/' + api + '\"\\r\\n\')\n' +
  3128. ' time.sleep_ms(500)\n' +
  3129. '';
  3130. var item_field = '',
  3131. item_value = '';
  3132. var nbiot_cococloud_data = "'{";
  3133. for (var n = 0; n < this.itemCount_; n++) {
  3134. item_field = block.getFieldValue("field" + n);
  3135. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  3136. nbiot_cococloud_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
  3137. }
  3138. var nbiot_cococloud_data_final = nbiot_cococloud_data + "}'";
  3139. var code = '' +
  3140. '_nbiot_cococloud_json_content = ' + nbiot_cococloud_data_final.slice(0, -3) + '}\'' + '\n' +
  3141. '_nbiot_cococloud_send(_nbiot_cococloud_json_content)\n' +
  3142. '';
  3143. return code;
  3144. };
  3145. Blockly.Blocks['3rd_party_sensor_hcsr04'] = {
  3146. init: function () {
  3147. let types = getLocalStorage("type")
  3148. this.appendDummyInput()
  3149. .appendField(new Blockly.FieldImage("blockly/media/ultrasonic_setup_header.png", 120, 40, { alt: "*", flipRtl: "FALSE" }));
  3150. types == 1 ? this.appendDummyInput()
  3151. .appendField(new Blockly.FieldDropdown([
  3152. [Blockly.Msg.third_party_hcsr04_ai, "ai"],
  3153. [Blockly.Msg.third_party_hcsr04_iot, "iot"]
  3154. ]), "type")
  3155. .appendField(Blockly.Msg.third_party_hcsr04_setup_title)
  3156. .appendField(new Blockly.FieldDropdown([
  3157. ["1", "1"],
  3158. ["2", "2"],
  3159. ["3", "3"]
  3160. ]), "index") : this.appendDummyInput()
  3161. .appendField(new Blockly.FieldDropdown([
  3162. [Blockly.Msg.third_party_hcsr04_iot, "iot"],
  3163. [Blockly.Msg.third_party_hcsr04_ai, "ai"]
  3164. ]), "type")
  3165. .appendField(Blockly.Msg.third_party_hcsr04_setup_title)
  3166. .appendField(new Blockly.FieldDropdown([
  3167. ["1", "1"],
  3168. ["2", "2"],
  3169. ["3", "3"]
  3170. ]), "index");
  3171. types == 1 ? this.appendDummyInput()
  3172. .appendField(Blockly.Msg.third_party_hcsr04_trig_pin)
  3173. .appendField(new Blockly.FieldNumber(2, 0, 40, 1), "trig")
  3174. .appendField(Blockly.Msg.third_party_hcsr04_echo_pin)
  3175. .appendField(new Blockly.FieldNumber(3, 0, 40, 1), "echo") :
  3176. this.appendDummyInput()
  3177. .appendField(Blockly.Msg.third_party_hcsr04_trig_pin)
  3178. .appendField(new Blockly.FieldNumber(12, 0, 40, 1), "trig")
  3179. .appendField(Blockly.Msg.third_party_hcsr04_echo_pin)
  3180. .appendField(new Blockly.FieldNumber(13, 0, 40, 1), "echo");
  3181. this.setPreviousStatement(true, null);
  3182. this.setNextStatement(true, null);
  3183. this.setColour("#d72bd5");
  3184. this.setHelpUrl("");
  3185. var thisBlock = this;
  3186. this.setTooltip(function () {
  3187. var mode = thisBlock.getFieldValue('type');
  3188. var TOOLTIPS = {
  3189. 'ai': Blockly.Msg.Trd_Party_Sensor_Hcsr04_TOOLTIP.replace('%1', Blockly.Msg.third_party_hcsr04_ai),
  3190. 'iot': Blockly.Msg.Trd_Party_Sensor_Hcsr04_TOOLTIP.replace('%1', Blockly.Msg.third_party_hcsr04_iot)
  3191. };
  3192. return TOOLTIPS[mode];
  3193. });
  3194. }
  3195. };
  3196. Blockly.Python['3rd_party_sensor_hcsr04'] = function (block) {
  3197. var dropdown_type = block.getFieldValue('type');
  3198. var dropdown_index = block.getFieldValue('index');
  3199. var number_trig = block.getFieldValue('trig');
  3200. var number_echo = block.getFieldValue('echo');
  3201. // TODO: Assemble Python into code variable.
  3202. if (dropdown_type == "iot") {
  3203. Blockly.Python.definitions_['import_hcsr04_def_' + number_trig + number_echo] = '' +
  3204. 'from HCSR04 import HCSR04\n' +
  3205. 'from machine import Pin\n' +
  3206. 'import time\n' +
  3207. '\n' +
  3208. '_ultrasonic_sensor_trig_' + number_trig + ' = Pin(' + number_trig + ',Pin.OUT)\n' +
  3209. '_ultrasonic_sensor_echo_' + number_echo + ' = Pin(' + number_echo + ',Pin.IN)\n' +
  3210. '_ultrasonic_sensor_read_' + dropdown_index + ' = HCSR04(_ultrasonic_sensor_trig_' + number_trig + ',_ultrasonic_sensor_echo_' + number_echo + ')\n' +
  3211. '\n';
  3212. var code = '';
  3213. } else if (dropdown_type == "ai") {
  3214. Blockly.Python.definitions_['import_hcsr04_def_' + number_trig + number_echo] = '' +
  3215. 'from HCSR04 import HCSR04\n' +
  3216. 'from fpioa_manager import *\n' +
  3217. 'from Maix import GPIO\n' +
  3218. 'import time\n' +
  3219. '\n' +
  3220. 'fm.register(' + number_trig + ',fm.fpioa.GPIOHS' + number_trig + ')\n' +
  3221. 'fm.register(' + number_echo + ',fm.fpioa.GPIOHS' + number_echo + ')\n' +
  3222. '\n' +
  3223. '_ultrasonic_sensor_trig_' + number_trig + ' = GPIO(GPIO.GPIOHS' + number_trig + ', GPIO.OUT)\n' +
  3224. '_ultrasonic_sensor_echo_' + number_echo + ' = GPIO(GPIO.GPIOHS' + number_echo + ', GPIO.IN)\n' +
  3225. '_ultrasonic_sensor_read_' + dropdown_index + ' = HCSR04(_ultrasonic_sensor_trig_' + number_trig + ',_ultrasonic_sensor_echo_' + number_echo + ')\n' +
  3226. '\n';
  3227. var code = '';
  3228. }
  3229. return code;
  3230. };
  3231. Blockly.Blocks['3rd_party_sensor_hcsr04_read'] = {
  3232. init: function () {
  3233. this.appendDummyInput()
  3234. .appendField(Blockly.Msg.third_party_hcsr04_read_text)
  3235. .appendField(new Blockly.FieldDropdown([
  3236. ["1", "1"],
  3237. ["2", "2"],
  3238. ["3", "3"]
  3239. ]), "index")
  3240. .appendField(Blockly.Msg.third_party_hcsr04_read_text_after);
  3241. this.setOutput(true, null);
  3242. this.setColour("#d72bd5");
  3243. this.setTooltip(Blockly.Msg.Trd_Party_Sensor_Hcsr04_Read_TOOLTIP);
  3244. this.setHelpUrl("");
  3245. }
  3246. };
  3247. Blockly.Python['3rd_party_sensor_hcsr04_read'] = function (block) {
  3248. var dropdown_index = block.getFieldValue('index');
  3249. // TODO: Assemble Python into code variable.
  3250. var code = '_ultrasonic_sensor_read_' + dropdown_index + '.getDistance()';
  3251. // TODO: Change ORDER_NONE to the correct strength.
  3252. return [code, Blockly.Python.ORDER_NONE];
  3253. };
  3254. Blockly.Blocks['Pedometer_Init'] = {
  3255. init: function () {
  3256. this.appendDummyInput()
  3257. .appendField(Blockly.Msg.Pedometer_Init);
  3258. this.appendValueInput("time")
  3259. .setCheck(null)
  3260. .appendField(Blockly.Msg.Pedometer_Init_time);
  3261. this.appendValueInput("sensitivity")
  3262. .setCheck(null)
  3263. .appendField(Blockly.Msg.Pedometer_Init_sensitivity);
  3264. this.setInputsInline(false);
  3265. this.setPreviousStatement(true, null);
  3266. this.setNextStatement(true, null);
  3267. this.setColour("#d42b03");
  3268. this.setTooltip(Blockly.Msg.Pedometer_Init_TOOLTIP);
  3269. this.setHelpUrl("");
  3270. }
  3271. };
  3272. Blockly.Python['Pedometer_Init'] = function (block) {
  3273. var time = Blockly.Python.valueToCode(block, 'time', Blockly.Python.ORDER_ATOMIC);
  3274. var sensitivity = Blockly.Python.valueToCode(block, 'sensitivity', Blockly.Python.ORDER_ATOMIC);
  3275. Blockly.Python.definitions_['Pedometer_Init'] = `from machine import I2C, Pin
  3276. import math
  3277. import QMI8658
  3278. mpu_lib_version_is_latest = "QMI8658" in dir(QMI8658)
  3279. if mpu_lib_version_is_latest == True:
  3280. mpui2c = I2C(scl=Pin(22), sda=Pin(21))
  3281. QMI8658 = QMI8658.QMI8658(mpui2c)
  3282. mpucal = QMI8658.calibrate()
  3283. elif mpu_lib_version_is_latest == False:
  3284. #申明QMI8658传感器IIC通信引脚
  3285. _SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))
  3286. _MPU = QMI8658.accel(_SENSOR_MPU_PINS)
  3287. _MPU_SHAKING_OFFSET = _MPU.get_values()['AcZ']
  3288. _MPU_ACCL_X = _MPU.get_values()['AcX']
  3289. _MPU_ACCL_Y = _MPU.get_values()['AcY']
  3290. _MPU_ACCL_Z = _MPU.get_values()['AcZ']
  3291. _MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE('X')
  3292. _MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE('Y')
  3293. _MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE('Z')
  3294. threshold_time=`+ time + ` #时间筛选阈值
  3295. threshold_hill=`+ sensitivity + ` #波峰波谷差值阈值
  3296. xval=[None]*100 #初始X轴加速度原始数据采集列表
  3297. yval=[None]*100 #初始Y轴加速度原始数据采集列表
  3298. zval=[None]*100 #初始Z轴加速度原始数据采集列表
  3299. xavg=0 #X轴方向初始加速度平均值
  3300. yavg=0 #Y轴方向初始加速度平均值
  3301. zavg=0 #Z轴方向初始加速度平均值
  3302. xaccl=[None]*100 #X轴加速度实时采集列表
  3303. yaccl=[None]*100 #Y轴加速度实时采集列表
  3304. zaccl=[None]*100 #Z轴加速度实时采集列表
  3305. totvect=[0.0]*100 #三个方向总加速度矢量值
  3306. totave=[0.0]*100 #最大和最小加速度矢量值平均值
  3307. peak=0 #波峰数据
  3308. trough=0 #波谷数据
  3309. data=[0]*15 #实时采集数据滚动递进列表
  3310. steps=0 #行走步伐数
  3311. flag0=0 #步数提取标志位
  3312. flag1=0 #波峰提取标志位
  3313. flag2=0 #波谷提取标志位
  3314. hill=0 #波峰波谷差值
  3315. starttime=0 #波峰时间点
  3316. endtime=0 #波谷时间点
  3317. lasttime=0 #波峰波谷间隔时间
  3318. #获取三个方向加速度函数
  3319. def QMI8658_get_accel(type):
  3320. _MPU = QMI8658.accel(_SENSOR_MPU_PINS)
  3321. _MPU_SHAKING_OFFSET = _MPU.get_values()['AcZ']
  3322. _MPU_ACCL_X = _MPU.get_values()['AcX']
  3323. _MPU_ACCL_Y = _MPU.get_values()['AcY']
  3324. _MPU_ACCL_Z = _MPU.get_values()['AcZ']
  3325. _MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE('X')
  3326. _MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE('Y')
  3327. _MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE('Z')
  3328. if type == 'X':
  3329. return _MPU_ACCL_X
  3330. if type == 'Y':
  3331. return _MPU_ACCL_Y
  3332. if type == 'Z':
  3333. return _MPU_ACCL_Z
  3334. #初始化校准函数
  3335. def calibration():
  3336. sum0=0 #x轴加速度累计值
  3337. sum1=0 #y轴加速度累计值
  3338. sum2=0 #z轴加速度累计值
  3339. for i in range(100):
  3340. xval[i]= QMI8658.read(mpucal)['AcX']-345 if mpu_lib_version_is_latest == True else QMI8658_get_accel('X')-345
  3341. sum0=xval[i]+sum0
  3342. time.sleep_ms(100)
  3343. xavg=sum0/100
  3344. #print(xavg)
  3345. for i in range(100):
  3346. yval[i]= QMI8658.read(mpucal)['AcY']-346 if mpu_lib_version_is_latest == True else QMI8658_get_accel('Y')-346
  3347. sum1=yval[i]+sum1
  3348. time.sleep_ms(100)
  3349. yavg=sum1/100
  3350. #print(yavg)
  3351. for i in range(100):
  3352. zval[i]= QMI8658.read(mpucal)['AcZ']-416 if mpu_lib_version_is_latest == True else QMI8658_get_accel('Z')-416
  3353. sum2=zval[i]+sum2
  3354. time.sleep_ms(100)
  3355. zavg=sum2/100
  3356. #print(zavg)
  3357. def getPedometer():
  3358. global q, xaccl, yaccl, zaccl, totvect, totave, data, peak, flag0, flag1, flag2, starttime, lasttime, endtime, hill, trough, threshold_hill, hill, threshold_time, steps
  3359. for a in range(100):
  3360. xaccl[a]= QMI8658.read(mpucal)['AcX']-345 if mpu_lib_version_is_latest == True else QMI8658_get_accel('X')-345
  3361. time.sleep_ms(1)
  3362. yaccl[a]= QMI8658.read(mpucal)['AcY']-346 if mpu_lib_version_is_latest == True else QMI8658_get_accel('Y')-346
  3363. time.sleep_ms(1)
  3364. zaccl[a]= QMI8658.read(mpucal)['AcZ']-416 if mpu_lib_version_is_latest == True else QMI8658_get_accel('Z')-416
  3365. time.sleep_ms(1)
  3366. #计算X、Y、Z轴三个方向加速度值的平方根来计算总加速度矢量
  3367. totvect[a]=math.sqrt(((xaccl[a]-xavg)*(xaccl[a]-xavg))+((yaccl[a]-yavg)*(yaccl[a]-yavg))+((zaccl[a]-zavg)*(zaccl[a]-zavg)))
  3368. totave[a]=(totvect[a]+totave[a-1])/2
  3369. q=totave[a]
  3370. #数据轮替
  3371. data[2]=data[1]
  3372. data[1]=data[0]
  3373. data[0]=totave[a]
  3374. #检测是否到达一个波峰,如果是,将波峰数据采集标志位置1,并开始计时
  3375. if ((data[1]>data[0])&(data[1]>data[2])&(flag1==0)):
  3376. peak=data[1]
  3377. flag1=1
  3378. starttime=time.ticks_ms()
  3379. #检测是否到达一个波谷,如果是,将波谷数据采集标志位置1,并读取当前时间
  3380. if ((data[1]<data[0])&(data[1]<data[2])&(flag2==0)):
  3381. trough=data[1]
  3382. flag2=1
  3383. endtime=time.ticks_ms()
  3384. #如果采集到一组波峰波谷数据
  3385. if((flag1==1)&(flag2==1)):
  3386. flag1=0 #将波峰数据采集标志位置0
  3387. flag2=0 #将波谷数据采集标指位置1
  3388. hill=peak-trough #计算波峰波谷差值
  3389. lasttime=endtime-starttime #计算波峰波谷间隔时间
  3390. if(hill>threshold_hill)&(lasttime>threshold_time): #如果波峰波谷差值大于threshold_hill且间隔时间大于threshold_time
  3391. steps=steps+1 #步数加一
  3392. flag0=1 #将步数数据提取标志位置1
  3393. lasttime=0 #将波峰波谷间隔时间清零
  3394. hill=0 #将波峰波谷差值清零
  3395. time.sleep_ms(20)
  3396. return steps
  3397. `;
  3398. var code = "";
  3399. return code;
  3400. };
  3401. Blockly.Blocks['Pedometer_Run'] = {
  3402. init: function () {
  3403. this.appendDummyInput()
  3404. .appendField(Blockly.Msg.Pedometer_Run);
  3405. this.setInputsInline(false);
  3406. this.setPreviousStatement(true, null);
  3407. this.setNextStatement(true, null);
  3408. this.setColour("#d42b03");
  3409. this.setTooltip(Blockly.Msg.Pedometer_Run_TOOLTIP);
  3410. this.setHelpUrl("");
  3411. }
  3412. };
  3413. Blockly.Python['Pedometer_Run'] = function (block) {
  3414. var code = `
  3415. calibration()
  3416. `;
  3417. return code;
  3418. };
  3419. Blockly.Blocks['Pedometer_Get'] = {
  3420. init: function () {
  3421. this.appendDummyInput()
  3422. .appendField(Blockly.Msg.Pedometer_Get);
  3423. this.setInputsInline(true);
  3424. this.setOutput(true, null);
  3425. this.setColour("#d42b03");
  3426. this.setTooltip(Blockly.Msg.Pedometer_Get_TOOLTIP);
  3427. this.setHelpUrl("");
  3428. }
  3429. };
  3430. Blockly.Python['Pedometer_Get'] = function (block) {
  3431. var code = `getPedometer()`;
  3432. return [code, Blockly.Python.ORDER_NONE];
  3433. };
  3434. /* mlx90614 */
  3435. Blockly.Blocks['mlx90614_setup'] = {
  3436. init: function () {
  3437. this.appendDummyInput()
  3438. .appendField(new Blockly.FieldImage("blockly/media/body_temp_mlx.png", 220, 60, { alt: "*", flipRtl: "FALSE" }));
  3439. let types = getLocalStorage("type")
  3440. types == 1 ? this.appendDummyInput()
  3441. .appendField("")
  3442. .appendField(new Blockly.FieldDropdown([
  3443. [Blockly.Msg.time_ai_module, "2"],
  3444. [Blockly.Msg.time_iot_module, "1"]
  3445. ]), "digital_get_type")
  3446. .appendField(Blockly.Msg.MLX90614_SETUP_TEXT_1) :
  3447. this.appendDummyInput()
  3448. .appendField("")
  3449. .appendField(new Blockly.FieldDropdown([
  3450. [Blockly.Msg.time_iot_module, "1"],
  3451. [Blockly.Msg.time_ai_module, "2"]
  3452. ]), "digital_get_type")
  3453. .appendField(Blockly.Msg.MLX90614_SETUP_TEXT_1);
  3454. this.appendDummyInput()
  3455. .appendField(Blockly.Msg.MLX90614_SETUP_TEXT_2);
  3456. this.setPreviousStatement(true, null);
  3457. this.setNextStatement(true, null);
  3458. this.setInputsInline(false);
  3459. this.setColour("#5c42bd");
  3460. this.setHelpUrl("");
  3461. var thisBlock = this;
  3462. this.setTooltip(function () {
  3463. var mode = thisBlock.getFieldValue('digital_get_type');
  3464. var TOOLTIPS = {
  3465. '2': Blockly.Msg.MLX90614_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module),
  3466. '1': Blockly.Msg.MLX90614_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module)
  3467. };
  3468. return TOOLTIPS[mode];
  3469. });
  3470. }
  3471. };
  3472. Blockly.Python['mlx90614_setup'] = function (block) {
  3473. var dropdown_type = block.getFieldValue('digital_get_type');
  3474. if (dropdown_type == "1") {
  3475. Blockly.Python.definitions_['import_iot_mlx90614_setup'] = '' +
  3476. 'import mlx90614\n' +
  3477. 'from machine import I2C, Pin\n' +
  3478. 'import time\n' +
  3479. '\n' +
  3480. 'def mlx90614_init():\n' +
  3481. ' global sensor_mlx90614\n' +
  3482. ' i2c = I2C(scl=Pin(22), sda=Pin(21),freq=100000)\n' +
  3483. ' while True:\n' +
  3484. ' init_success = False\n' +
  3485. ' try:\n' +
  3486. ' sensor_mlx90614 = mlx90614.MLX90614(i2c)\n' +
  3487. ' init_success = True\n' +
  3488. ' except BaseException as e:\n' +
  3489. ' print(e)\n' +
  3490. ' if init_success == True:\n' +
  3491. ' break\n' +
  3492. '\n' +
  3493. 'def mlx90614_read(datatype):\n' +
  3494. ' read_from_mlx90614 = None\n' +
  3495. ' try:\n' +
  3496. ' if datatype == "read_ambient_tempC":\n' +
  3497. ' read_from_mlx90614 = sensor_mlx90614.read_ambient_tempC()\n' +
  3498. ' elif datatype == "read_ambient_tempF":\n' +
  3499. ' read_from_mlx90614 = sensor_mlx90614.read_ambient_tempF()\n' +
  3500. ' elif datatype == "read_object_tempC":\n' +
  3501. ' read_from_mlx90614 = sensor_mlx90614.read_object_tempC()\n' +
  3502. ' elif datatype == "read_object_tempF":\n' +
  3503. ' read_from_mlx90614 = sensor_mlx90614.read_object_tempF()\n' +
  3504. ' except BaseException as e:\n' +
  3505. ' read_from_mlx90614 = None\n' +
  3506. ' print(e)\n' +
  3507. ' read_from_mlx90614 = round(read_from_mlx90614, 2)\n' +
  3508. ' # print(type(read_from_mlx90614))\n' +
  3509. ' return read_from_mlx90614\n' +
  3510. '\n';
  3511. var code = 'mlx90614_init()\n';
  3512. } else if (dropdown_type == "2") {
  3513. Blockly.Python.definitions_['import_ai_mlx90614_setup'] = '' +
  3514. 'import mlx90614\n' +
  3515. 'from machine import I2C\n' +
  3516. 'import time\n' +
  3517. '\n' +
  3518. 'def mlx90614_init():\n' +
  3519. ' global sensor_mlx90614\n' +
  3520. ' i2c = I2C(I2C.I2C0, freq=100000, scl=30, sda=29)\n' +
  3521. ' while True:\n' +
  3522. ' init_success = False\n' +
  3523. ' try:\n' +
  3524. ' sensor_mlx90614 = mlx90614.MLX90614(i2c)\n' +
  3525. ' init_success = True\n' +
  3526. ' except BaseException as e:\n' +
  3527. ' print(e)\n' +
  3528. ' if init_success == True:\n' +
  3529. ' break\n' +
  3530. '\n' +
  3531. 'def mlx90614_read(datatype):\n' +
  3532. ' read_from_mlx90614 = None\n' +
  3533. ' try:\n' +
  3534. ' if datatype == "read_ambient_tempC":\n' +
  3535. ' read_from_mlx90614 = sensor_mlx90614.read_ambient_tempC()\n' +
  3536. ' elif datatype == "read_ambient_tempF":\n' +
  3537. ' read_from_mlx90614 = sensor_mlx90614.read_ambient_tempF()\n' +
  3538. ' elif datatype == "read_object_tempC":\n' +
  3539. ' read_from_mlx90614 = sensor_mlx90614.read_object_tempC()\n' +
  3540. ' elif datatype == "read_object_tempF":\n' +
  3541. ' read_from_mlx90614 = sensor_mlx90614.read_object_tempF()\n' +
  3542. ' except BaseException as e:\n' +
  3543. ' read_from_mlx90614 = None\n' +
  3544. ' print(e)\n' +
  3545. ' read_from_mlx90614 = round(read_from_mlx90614, 2)\n' +
  3546. ' # print(type(read_from_mlx90614))\n' +
  3547. ' return read_from_mlx90614\n' +
  3548. '\n';
  3549. var code = 'mlx90614_init()\n';
  3550. }
  3551. return code;
  3552. };
  3553. Blockly.Blocks['mlx90614_read_ambient'] = {
  3554. init: function () {
  3555. this.appendDummyInput()
  3556. .appendField(Blockly.Msg.MLX90614_READ_AMBIENT);
  3557. this.appendDummyInput()
  3558. .appendField(Blockly.Msg.MLX90614_READ_SET_TEMP)
  3559. .appendField(new Blockly.FieldDropdown([
  3560. [Blockly.Msg.MLX90614_READ_SET_TEMP_C, "C"],
  3561. [Blockly.Msg.MLX90614_READ_SET_TEMP_F, "F"]
  3562. ]), "type");
  3563. this.setInputsInline(false);
  3564. this.setOutput(true, null);
  3565. this.setColour("#5c42bd");
  3566. this.setTooltip(Blockly.Msg.MLX90614_READ_AMBIENT_TOOLTIP);
  3567. this.setHelpUrl("");
  3568. }
  3569. };
  3570. Blockly.Python['mlx90614_read_ambient'] = function (block) {
  3571. var dropdown_type = block.getFieldValue('type');
  3572. if (dropdown_type == "C") {
  3573. var code = `mlx90614_read("read_ambient_tempC")`;
  3574. } else if (dropdown_type == "F") {
  3575. var code = `mlx90614_read("read_ambient_tempF")`;
  3576. }
  3577. return [code, Blockly.Python.ORDER_NONE];
  3578. };
  3579. Blockly.Blocks['mlx90614_read_object'] = {
  3580. init: function () {
  3581. this.appendDummyInput()
  3582. .appendField(Blockly.Msg.MLX90614_READ_OBJECT);
  3583. this.appendDummyInput()
  3584. .appendField(Blockly.Msg.MLX90614_READ_SET_TEMP)
  3585. .appendField(new Blockly.FieldDropdown([
  3586. [Blockly.Msg.MLX90614_READ_SET_TEMP_C, "C"],
  3587. [Blockly.Msg.MLX90614_READ_SET_TEMP_F, "F"]
  3588. ]), "type");
  3589. this.setInputsInline(false);
  3590. this.setOutput(true, null);
  3591. this.setColour("#5c42bd");
  3592. this.setTooltip(Blockly.Msg.MLX90614_READ_OBJECT_TOOLTIP);
  3593. this.setHelpUrl("");
  3594. }
  3595. };
  3596. Blockly.Python['mlx90614_read_object'] = function (block) {
  3597. var dropdown_type = block.getFieldValue('type');
  3598. if (dropdown_type == "C") {
  3599. var code = `mlx90614_read("read_object_tempC")`;
  3600. } else if (dropdown_type == "F") {
  3601. var code = `mlx90614_read("read_object_tempF")`;
  3602. }
  3603. return [code, Blockly.Python.ORDER_NONE];
  3604. };
  3605. /* DHT11 or DHT22 */
  3606. Blockly.Blocks['DHT_setup'] = {
  3607. init: function () {
  3608. this.appendDummyInput()
  3609. .appendField(new Blockly.FieldImage("blockly/media/dht_header.png", 60, 60, { alt: "*", flipRtl: "FALSE" }));
  3610. let types = getLocalStorage("type")
  3611. types == 1 ? this.appendDummyInput()
  3612. .appendField("")
  3613. .appendField(new Blockly.FieldDropdown([
  3614. [Blockly.Msg.time_ai_module, "2"],
  3615. [Blockly.Msg.time_iot_module, "1"]
  3616. ]), "digital_get_type")
  3617. .appendField(Blockly.Msg.DHT_SETUP_TEXT_1) :
  3618. this.appendDummyInput()
  3619. .appendField("")
  3620. .appendField(new Blockly.FieldDropdown([
  3621. [Blockly.Msg.time_iot_module, "1"],
  3622. [Blockly.Msg.time_ai_module, "2"]
  3623. ]), "digital_get_type")
  3624. .appendField(Blockly.Msg.DHT_SETUP_TEXT_1);
  3625. this.appendDummyInput()
  3626. .appendField(Blockly.Msg.DHT_SETUP_TEXT_2)
  3627. .appendField(new Blockly.FieldDropdown([
  3628. ["DHT11", "1"],
  3629. ["DHT22", "2"]
  3630. ]), "type")
  3631. .appendField(Blockly.Msg.DHT_SETUP_TEXT_3);
  3632. types == 1 ? this.appendDummyInput()
  3633. .appendField(Blockly.Msg.DHT_SETUP_TEXT_4)
  3634. .appendField(new Blockly.FieldDropdown([
  3635. ["1", "0"],
  3636. ["2", "1"],
  3637. ["3", "2"],
  3638. ["13", "3"],
  3639. ["14", "4"],
  3640. ["15", "5"],
  3641. ["17", "6"],
  3642. ["21", "7"],
  3643. ["22", "8"],
  3644. ["23", "9"],
  3645. ["24", "10"],
  3646. ["29", "11"],
  3647. ["30", "12"]
  3648. ]), "gpio") :
  3649. this.appendDummyInput()
  3650. .appendField(Blockly.Msg.DHT_SETUP_TEXT_4)
  3651. .appendField(new Blockly.FieldDropdown([
  3652. ["4", "0"],
  3653. ["12", "1"],
  3654. ["13", "2"],
  3655. ["14", "3"],
  3656. ["15", "4"],
  3657. ["16", "5"],
  3658. ["17", "6"],
  3659. ["21", "7"],
  3660. ["22", "8"],
  3661. ["25", "9"],
  3662. ["27", "10"]
  3663. ]), "gpio");
  3664. this.setPreviousStatement(true, null);
  3665. this.setNextStatement(true, null);
  3666. this.setInputsInline(false);
  3667. this.setColour("#629978");
  3668. this.setHelpUrl("");
  3669. var thisBlock = this;
  3670. this.setTooltip(function () {
  3671. var mode = thisBlock.getFieldValue('digital_get_type');
  3672. var TOOLTIPS = {
  3673. '2': Blockly.Msg.DHT_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module),
  3674. '1': Blockly.Msg.DHT_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module)
  3675. };
  3676. return TOOLTIPS[mode];
  3677. });
  3678. }
  3679. };
  3680. Blockly.Python['DHT_setup'] = function (block) {
  3681. var dropdown_type = block.getFieldValue('digital_get_type');
  3682. var type = block.getFieldValue('type');
  3683. var gpio = block.getFieldValue('gpio');
  3684. var code = '';
  3685. if (dropdown_type == "1") {
  3686. if (type == "1") {
  3687. Blockly.Python.definitions_['import_iot_DHT_setup'] = '' +
  3688. 'import dht\n' +
  3689. 'import machine\n' +
  3690. 'import time\n' +
  3691. '\n' +
  3692. 'd = dht.DHT11(machine.Pin(' + iot_servo_pin[gpio] + '))\n' +
  3693. '\n';
  3694. Blockly.Python.definitions_['import_DHT_read_data'] = '' +
  3695. 'def GetDHTTemperatureValue():\n' +
  3696. ' try:\n' +
  3697. ' d.measure()\n' +
  3698. ' return d.temperature()\n' +
  3699. ' except:\n' +
  3700. ' return d.temperature()\n' +
  3701. '\n' +
  3702. 'def GetDHTHumidityValue():\n' +
  3703. ' try:\n' +
  3704. ' d.measure()\n' +
  3705. ' return d.humidity()\n' +
  3706. ' except:\n' +
  3707. ' return d.humidity()\n' +
  3708. '\n' +
  3709. '';
  3710. }
  3711. else {
  3712. Blockly.Python.definitions_['import_iot_DHT_setup'] = '' +
  3713. 'import dht\n' +
  3714. 'import machine\n' +
  3715. 'import time\n' +
  3716. '\n' +
  3717. 'd = dht.DHT22(machine.Pin(' + iot_servo_pin[gpio] + '))\n' +
  3718. '\n';
  3719. Blockly.Python.definitions_['import_DHT_read_data'] = '' +
  3720. 'def GetDHTTemperatureValue():\n' +
  3721. ' try:\n' +
  3722. ' d.measure()\n' +
  3723. ' return d.temperature()\n' +
  3724. ' except:\n' +
  3725. ' return d.temperature()\n' +
  3726. '\n' +
  3727. 'def GetDHTHumidityValue():\n' +
  3728. ' try:\n' +
  3729. ' d.measure()\n' +
  3730. ' return d.humidity()\n' +
  3731. ' except:\n' +
  3732. ' return d.humidity()\n' +
  3733. '\n' +
  3734. '';
  3735. }
  3736. } else if (dropdown_type == "2") {
  3737. if (type == "1") {
  3738. Blockly.Python.definitions_['import_ai_DHT_setup'] = '' +
  3739. 'import dhtsensor\n' +
  3740. 'import time\n' +
  3741. '\n' +
  3742. 'dhtsensor.DHT_IO_Register(' + ai_servo_pin[gpio] + ')\n' +
  3743. 'time.sleep_ms(100)\n' +
  3744. '\n' +
  3745. 'temperature = None\n' +
  3746. 'humidity = None\n' +
  3747. '\n';
  3748. Blockly.Python.definitions_['import_DHT_read_data'] = '' +
  3749. 'def GetDHTTemperatureValue():\n' +
  3750. ' global temperature,humidity\n' +
  3751. ' if dhtsensor.measure() and temperature == None:\n' +
  3752. ' temperature = dhtsensor.GetDHT11_temperature()\n' +
  3753. ' humidity = dhtsensor.GetDHT11_humidity()\n' +
  3754. ' time.sleep_ms(1000)\n' +
  3755. ' return temperature\n' +
  3756. ' elif temperature != None:\n' +
  3757. ' temperature = dhtsensor.GetDHT11_temperature()\n' +
  3758. ' humidity = None\n' +
  3759. ' return temperature\n' +
  3760. '\n' +
  3761. 'def GetDHTHumidityValue():\n' +
  3762. ' global temperature,humidity\n' +
  3763. ' if dhtsensor.measure() and humidity == None:\n' +
  3764. ' temperature = dhtsensor.GetDHT11_temperature()\n' +
  3765. ' humidity = dhtsensor.GetDHT11_humidity()\n' +
  3766. ' time.sleep_ms(1000)\n' +
  3767. ' return humidity\n' +
  3768. ' elif humidity != None:\n' +
  3769. ' humidity = dhtsensor.GetDHT11_humidity()\n' +
  3770. ' temperature = None\n' +
  3771. ' return humidity\n' +
  3772. '\n' +
  3773. '';
  3774. }
  3775. else {
  3776. Blockly.Python.definitions_['import_ai_DHT_setup'] = '' +
  3777. 'import dhtsensor\n' +
  3778. 'import time\n' +
  3779. '\n' +
  3780. 'dhtsensor.DHT_IO_Register(' + ai_servo_pin[gpio] + ')\n' +
  3781. 'time.sleep_ms(100)\n' +
  3782. '\n' +
  3783. 'temperature = None\n' +
  3784. 'humidity = None\n' +
  3785. '\n';
  3786. Blockly.Python.definitions_['import_DHT_read_data'] = '' +
  3787. 'def GetDHTTemperatureValue():\n' +
  3788. ' global temperature,humidity\n' +
  3789. ' if dhtsensor.measure() and temperature == None:\n' +
  3790. ' temperature = dhtsensor.GetDHT22_temperature()\n' +
  3791. ' humidity = dhtsensor.GetDHT22_humidity()\n' +
  3792. ' time.sleep_ms(1000)\n' +
  3793. ' return temperature * 0.1\n' +
  3794. ' elif temperature != None:\n' +
  3795. ' temperature = dhtsensor.GetDHT22_temperature()\n' +
  3796. ' humidity = None\n' +
  3797. ' return temperature * 0.1\n' +
  3798. '\n' +
  3799. 'def GetDHTHumidityValue():\n' +
  3800. ' global temperature,humidity\n' +
  3801. ' if dhtsensor.measure() and humidity == None:\n' +
  3802. ' temperature = dhtsensor.GetDHT22_temperature()\n' +
  3803. ' humidity = dhtsensor.GetDHT22_humidity()\n' +
  3804. ' time.sleep_ms(1000)\n' +
  3805. ' return humidity * 0.1\n' +
  3806. ' elif humidity != None:\n' +
  3807. ' humidity = dhtsensor.GetDHT22_humidity()\n' +
  3808. ' temperature = None\n' +
  3809. ' return humidity * 0.1\n' +
  3810. '\n' +
  3811. '';
  3812. }
  3813. }
  3814. return code;
  3815. };
  3816. Blockly.Blocks['DHT_read_data'] = {
  3817. init: function () {
  3818. // this.appendDummyInput()
  3819. // .appendField(new Blockly.FieldImage("blockly/media/env_get.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  3820. this.appendDummyInput()
  3821. .appendField(Blockly.Msg.ESP32_ENV_GET_TEXT)
  3822. .appendField(new Blockly.FieldDropdown([
  3823. [Blockly.Msg.basic_temperature, "temperature"],
  3824. [Blockly.Msg.basic_humidity, "humidity"]
  3825. // [Blockly.Msg.basic_pressure, "pressure"]
  3826. ]), "env_get_type")
  3827. .appendField(Blockly.Msg.basic_env_value);
  3828. this.setInputsInline(false);
  3829. this.setOutput(true, null);
  3830. this.setColour("#629978");
  3831. this.setHelpUrl("");
  3832. var thisBlock = this;
  3833. this.setTooltip(function () {
  3834. var mode = thisBlock.getFieldValue('env_get_type');
  3835. var TOOLTIPS = {
  3836. 'temperature': Blockly.Msg.Esp32_Main_Controller_Get_Environmental_Value_TOOLTIP.replace('%1', Blockly.Msg.basic_temperature),
  3837. 'humidity': Blockly.Msg.Esp32_Main_Controller_Get_Environmental_Value_TOOLTIP.replace('%1', Blockly.Msg.basic_humidity)
  3838. };
  3839. return TOOLTIPS[mode];
  3840. });
  3841. }
  3842. };
  3843. Blockly.Python['DHT_read_data'] = function (block) {
  3844. var env_get_type = block.getFieldValue('env_get_type');
  3845. var code = "";
  3846. if (env_get_type == "temperature") {
  3847. code = 'GetDHTTemperatureValue()';
  3848. } else if (env_get_type == "humidity") {
  3849. code = 'GetDHTHumidityValue()';
  3850. }
  3851. return [code, Blockly.Python.ORDER_NONE];
  3852. };
  3853. /* PM2.5 */
  3854. Blockly.Blocks['PM25_setup'] = {
  3855. init: function () {
  3856. this.appendDummyInput()
  3857. .appendField(new Blockly.FieldImage("blockly/media/pm25sensor_setup.png", 220, 60, { alt: "*", flipRtl: "FALSE" }));
  3858. let types = getLocalStorage("type")
  3859. types == 1 ? this.appendDummyInput()
  3860. .appendField("")
  3861. .appendField(new Blockly.FieldDropdown([
  3862. [Blockly.Msg.time_ai_module, "2"],
  3863. [Blockly.Msg.time_iot_module, "1"]
  3864. ]), "digital_get_type")
  3865. .appendField(Blockly.Msg.PM25_SETUP_TEXT) :
  3866. this.appendDummyInput()
  3867. .appendField("")
  3868. .appendField(new Blockly.FieldDropdown([
  3869. [Blockly.Msg.time_iot_module, "1"],
  3870. [Blockly.Msg.time_ai_module, "2"]
  3871. ]), "digital_get_type")
  3872. .appendField(Blockly.Msg.PM25_SETUP_TEXT);
  3873. this.appendDummyInput()
  3874. .appendField(Blockly.Msg.DHT_SETUP_TEXT_2)
  3875. .appendField(new Blockly.FieldDropdown([
  3876. ["GP2Y10", "1"]
  3877. ]), "type")
  3878. .appendField(Blockly.Msg.DHT_SETUP_TEXT_3);
  3879. this.setPreviousStatement(true, null);
  3880. this.setNextStatement(true, null);
  3881. this.setInputsInline(false);
  3882. this.setColour("#386dc8");
  3883. this.setHelpUrl("");
  3884. var thisBlock = this;
  3885. this.setTooltip(function () {
  3886. var mode = thisBlock.getFieldValue('digital_get_type');
  3887. var TOOLTIPS = {
  3888. '2': Blockly.Msg.PM25_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module),
  3889. '1': Blockly.Msg.PM25_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module)
  3890. };
  3891. return TOOLTIPS[mode];
  3892. });
  3893. }
  3894. };
  3895. Blockly.Python['PM25_setup'] = function (block) {
  3896. var dropdown_type = block.getFieldValue('digital_get_type');
  3897. var type = block.getFieldValue('type');
  3898. var code = '';
  3899. if (dropdown_type == "1") {
  3900. if (type == "1") {
  3901. Blockly.Python.definitions_['import_iot_PM25_setup'] = '' +
  3902. 'from machine import UART, Pin\n' +
  3903. 'import pm25\n' +
  3904. '\n' +
  3905. 'uart = UART(2, baudrate=2400, rx=16, tx=17, timeout=10)\n' +
  3906. '\n';
  3907. }
  3908. } else if (dropdown_type == "2") {
  3909. if (type == "1") {
  3910. Blockly.Python.definitions_['import_ai_PM25_setup'] = '' +
  3911. 'import machine, time\n' +
  3912. 'from fpioa_manager import fm\n' +
  3913. 'import pm25\n' +
  3914. '\n' +
  3915. 'fm.register(13,fm.fpioa.UART2_TX)\n' +
  3916. 'fm.register(14,fm.fpioa.UART2_RX)\n' +
  3917. '\n' +
  3918. 'uart = machine.UART(machine.UART.UART2,2400,bits=8,parity=None,stop=1)\n' +
  3919. '\n';
  3920. }
  3921. }
  3922. return code;
  3923. };
  3924. Blockly.Blocks['PM25_read_data'] = {
  3925. init: function () {
  3926. // this.appendDummyInput()
  3927. // .appendField(new Blockly.FieldImage("blockly/media/env_get.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  3928. this.appendDummyInput()
  3929. .appendField(Blockly.Msg.ESP32_ENV_GET_TEXT)
  3930. .appendField("PM 2.5")
  3931. .appendField(Blockly.Msg.basic_env_value);
  3932. this.setInputsInline(false);
  3933. this.setOutput(true, null);
  3934. this.setTooltip(Blockly.Msg.PM25_REDD_DATA_TOOLTIP);
  3935. this.setColour("#386dc8");
  3936. this.setHelpUrl("");
  3937. }
  3938. };
  3939. Blockly.Python['PM25_read_data'] = function (block) {
  3940. var code = "pm25.getPM25Value(uart)";
  3941. return [code, Blockly.Python.ORDER_NONE];
  3942. };
  3943. /* RTC */
  3944. var rtcColor = "#3c7683";
  3945. Blockly.Blocks['RTC_setup'] = {
  3946. init: function () {
  3947. this.appendDummyInput()
  3948. .appendField(new Blockly.FieldImage("blockly/media/sensors-rtc.png", 60, 60, { alt: "*", flipRtl: "FALSE" }));
  3949. this.appendDummyInput()
  3950. .appendField("")
  3951. .appendField(Blockly.Msg.time_ai_module)
  3952. .appendField(Blockly.Msg.RTC_SETUP);
  3953. this.appendDummyInput()
  3954. .appendField(Blockly.Msg.DHT_SETUP_TEXT_2)
  3955. .appendField(new Blockly.FieldDropdown([
  3956. ["DS3231", "1"]
  3957. ]), "type");
  3958. this.setPreviousStatement(true, null);
  3959. this.setNextStatement(true, null);
  3960. this.setInputsInline(false);
  3961. this.setColour(rtcColor);
  3962. this.setHelpUrl("");
  3963. this.setTooltip(Blockly.Msg.RTC_setup_TOOLTIP);
  3964. }
  3965. };
  3966. Blockly.Python['RTC_setup'] = function (block) {
  3967. var type = block.getFieldValue('type');
  3968. var code = '';
  3969. if (type == "1") {
  3970. Blockly.Python.definitions_['import_iot_RTC_setup'] = '' +
  3971. 'import machine\n' +
  3972. 'import DS3231micro\n' +
  3973. '\n' +
  3974. 'rtc = DS3231micro.DS3231(30, 29)\n' +
  3975. '\n';
  3976. }
  3977. return code;
  3978. };
  3979. Blockly.Blocks['RTC_setDateTime'] = {
  3980. init: function () {
  3981. this.appendDummyInput()
  3982. .appendField(Blockly.Msg.RTC_SETDATETIME);
  3983. this.appendDummyInput()
  3984. .appendField(Blockly.Msg.RTC_SETDATE)
  3985. .appendField(new Blockly.FieldNumber(18, 0, 50), "year")
  3986. .appendField("-")
  3987. .appendField(new Blockly.FieldNumber(1, 0, 12), "month")
  3988. .appendField("-")
  3989. .appendField(new Blockly.FieldNumber(1, 0, 31), "day")
  3990. this.appendDummyInput()
  3991. .appendField(Blockly.Msg.RTC_SETTIME)
  3992. .appendField(new Blockly.FieldNumber(12, 0, 24), "hour")
  3993. .appendField(":")
  3994. .appendField(new Blockly.FieldNumber(30, 0, 60), "minute")
  3995. .appendField(":")
  3996. .appendField(new Blockly.FieldNumber(10, 0, 60), "second")
  3997. this.appendDummyInput()
  3998. .appendField(Blockly.Msg.RTC_SETWEEK)
  3999. .appendField(new Blockly.FieldDropdown([
  4000. [Blockly.Msg.Monday, "1"],
  4001. [Blockly.Msg.Tuesday, "2"],
  4002. [Blockly.Msg.Wednesday, "3"],
  4003. [Blockly.Msg.Thursday, "4"],
  4004. [Blockly.Msg.Friday, "5"],
  4005. [Blockly.Msg.Saturday, "6"],
  4006. [Blockly.Msg.Sunday, "7"]
  4007. ]), "week")
  4008. this.setPreviousStatement(true);
  4009. this.setNextStatement(true);
  4010. this.setColour(rtcColor);
  4011. this.setTooltip(Blockly.Msg.RTC_setDateTime_TOOLTIP);
  4012. this.setHelpUrl("");
  4013. },
  4014. /**
  4015. * Called whenever anything on the workspace changes.
  4016. * It checks the number of data and time settting
  4017. * @this Blockly.Block
  4018. */
  4019. onchange: function () {
  4020. if (this.getFieldValue("year") > 50) {
  4021. this.setFieldValue("19", "year");
  4022. }
  4023. if (this.getFieldValue("month") > 12) {
  4024. this.setFieldValue("12", "month");
  4025. } else if (this.getFieldValue("month") < 0) {
  4026. this.setFieldValue("1", "month");
  4027. }
  4028. if (this.getFieldValue("day") > 31) {
  4029. this.setFieldValue("31", "day");
  4030. } else if (this.getFieldValue("day") < 0) {
  4031. this.setFieldValue("1", "day");
  4032. }
  4033. if (this.getFieldValue("hour") > 24) {
  4034. this.setFieldValue("24", "hour");
  4035. } else if (this.getFieldValue("hour") < 0) {
  4036. this.setFieldValue("1", "hour");
  4037. }
  4038. if (this.getFieldValue("minute") > 60) {
  4039. this.setFieldValue("60", "minute");
  4040. } else if (this.getFieldValue("minute") < 0) {
  4041. this.setFieldValue("1", "minute");
  4042. }
  4043. if (this.getFieldValue("second") > 60) {
  4044. this.setFieldValue("60", "second");
  4045. } else if (this.getFieldValue("second") < 0) {
  4046. this.setFieldValue("1", "second");
  4047. }
  4048. }
  4049. };
  4050. Blockly.Python['RTC_setDateTime'] = function (block) {
  4051. var year = block.getFieldValue("year");
  4052. var month = block.getFieldValue("month");
  4053. var day = block.getFieldValue("day");
  4054. var hour = block.getFieldValue("hour");
  4055. var minute = block.getFieldValue("minute");
  4056. var second = block.getFieldValue("second");
  4057. var week = block.getFieldValue('week');
  4058. var code = 'rtc.setDateTime(' + year + ',' + month + ',' + day + ',' + week + ',' + hour + ',' + minute + ',' + second + ')\n';
  4059. return code;
  4060. };
  4061. Blockly.Blocks['RTC_getDate'] = {
  4062. init: function () {
  4063. this.appendDummyInput()
  4064. .appendField(Blockly.Msg.RTC_GETDATE)
  4065. .appendField(new Blockly.FieldDropdown([
  4066. [Blockly.Msg.RTC_YEAR, "year"],
  4067. [Blockly.Msg.RTC_MONTH, "month"],
  4068. [Blockly.Msg.RTC_DAY, "day"],
  4069. ]), "date_detail")
  4070. this.setColour(rtcColor);
  4071. this.setOutput(true, null);
  4072. this.setHelpUrl("");
  4073. var thisBlock = this;
  4074. this.setTooltip(function () {
  4075. var mode = thisBlock.getFieldValue('date_detail');
  4076. var TOOLTIPS = {
  4077. "year": Blockly.Msg.RTC_GETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_YEAR),
  4078. "month": Blockly.Msg.RTC_GETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_MONTH),
  4079. "day": Blockly.Msg.RTC_GETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_DAY),
  4080. };
  4081. return TOOLTIPS[mode];
  4082. });
  4083. }
  4084. };
  4085. Blockly.Python['RTC_getDate'] = function (block) {
  4086. var date_detail = block.getFieldValue("date_detail");
  4087. var code = '';
  4088. if (date_detail == "year") {
  4089. code = 'rtc.getYear()';
  4090. }
  4091. else if (date_detail == "month") {
  4092. code = 'rtc.getMonth()';
  4093. }
  4094. else {
  4095. code = 'rtc.getDay()';
  4096. }
  4097. return [code, Blockly.Python.ORDER_NONE];
  4098. };
  4099. Blockly.Blocks['RTC_getTime'] = {
  4100. init: function () {
  4101. this.appendDummyInput()
  4102. .appendField(Blockly.Msg.RTC_GETTIME)
  4103. .appendField(new Blockly.FieldDropdown([
  4104. [Blockly.Msg.RTC_HOUR, "hour"],
  4105. [Blockly.Msg.RTC_MINUTE, "minute"],
  4106. [Blockly.Msg.RTC_SECOND, "second"],
  4107. ]), "time_detail")
  4108. this.setColour(rtcColor);
  4109. this.setOutput(true, null);
  4110. this.setHelpUrl("");
  4111. var thisBlock = this;
  4112. this.setTooltip(function () {
  4113. var mode = thisBlock.getFieldValue('time_detail');
  4114. var TOOLTIPS = {
  4115. "hour": Blockly.Msg.RTC_GETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_HOUR),
  4116. "minute": Blockly.Msg.RTC_GETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_MINUTE),
  4117. "second": Blockly.Msg.RTC_GETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_SECOND),
  4118. };
  4119. return TOOLTIPS[mode];
  4120. });
  4121. }
  4122. };
  4123. Blockly.Python['RTC_getTime'] = function (block) {
  4124. var time_detail = block.getFieldValue("time_detail");
  4125. var code = '';
  4126. if (time_detail == "hour") {
  4127. code = 'rtc.getHour()';
  4128. }
  4129. else if (time_detail == "minute") {
  4130. code = 'rtc.getMinutes()';
  4131. }
  4132. else {
  4133. code = 'rtc.getSeconds()';
  4134. }
  4135. return [code, Blockly.Python.ORDER_NONE];
  4136. };
  4137. Blockly.Blocks['RTC_getWeek'] = {
  4138. init: function () {
  4139. this.appendDummyInput()
  4140. .appendField(Blockly.Msg.RTC_GETWEEK);
  4141. this.setColour(rtcColor);
  4142. this.setOutput(true, null);
  4143. this.setTooltip(Blockly.Msg.RTC_getWeek_TOOLTIP);
  4144. this.setHelpUrl("");
  4145. }
  4146. };
  4147. Blockly.Python['RTC_getWeek'] = function (block) {
  4148. var code = 'rtc.getDayOfWeek()';
  4149. return [code, Blockly.Python.ORDER_NONE];
  4150. };
  4151. Blockly.Blocks['RTC_getDateTime'] = {
  4152. init: function () {
  4153. this.appendDummyInput()
  4154. .appendField(Blockly.Msg.RTC_getDateTime);
  4155. this.setColour(rtcColor);
  4156. this.setOutput(true, null);
  4157. this.setTooltip(Blockly.Msg.RTC_getDateTime_TOOLTIP);
  4158. this.setHelpUrl("");
  4159. }
  4160. };
  4161. Blockly.Python['RTC_getDateTime'] = function (block) {
  4162. var code = 'rtc.getDateTime()';
  4163. return [code, Blockly.Python.ORDER_NONE];
  4164. };
  4165. Blockly.Blocks['RTC_setDate'] = {
  4166. init: function () {
  4167. this.appendDummyInput()
  4168. .appendField(Blockly.Msg.RTC_setDATE)
  4169. .appendField(new Blockly.FieldDropdown([
  4170. [Blockly.Msg.RTC_YEAR, "year"],
  4171. [Blockly.Msg.RTC_MONTH, "month"],
  4172. [Blockly.Msg.RTC_DAY, "day"],
  4173. ]), "date_detail")
  4174. .appendField(Blockly.Msg.RTC_WEI)
  4175. .appendField(new Blockly.FieldNumber(0, null, null), "date")
  4176. this.setOutput(false, null);
  4177. this.setPreviousStatement(true);
  4178. this.setNextStatement(true);
  4179. this.setColour(rtcColor);
  4180. this.setHelpUrl("");
  4181. var thisBlock = this;
  4182. this.setTooltip(function () {
  4183. var mode = thisBlock.getFieldValue('date_detail');
  4184. var TOOLTIPS = {
  4185. "year": Blockly.Msg.RTC_SETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_YEAR),
  4186. "month": Blockly.Msg.RTC_SETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_MONTH),
  4187. "day": Blockly.Msg.RTC_SETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_DAY),
  4188. };
  4189. return TOOLTIPS[mode];
  4190. });
  4191. }
  4192. };
  4193. Blockly.Python['RTC_setDate'] = function (block) {
  4194. var date_detail = block.getFieldValue("date_detail");
  4195. var date = block.getFieldValue("date");
  4196. var code = '';
  4197. if (date_detail == "year") {
  4198. code = 'rtc.setYear(' + date + ')\n';
  4199. }
  4200. else if (date_detail == "month") {
  4201. code = 'rtc.setMonth(' + date + ')\n';
  4202. }
  4203. else {
  4204. code = 'rtc.setDay(' + date + ')\n';
  4205. }
  4206. return code;
  4207. };
  4208. Blockly.Blocks['RTC_setTime'] = {
  4209. init: function () {
  4210. this.appendDummyInput()
  4211. .appendField(Blockly.Msg.RTC_setTIME)
  4212. .appendField(new Blockly.FieldDropdown([
  4213. [Blockly.Msg.RTC_HOUR, "hour"],
  4214. [Blockly.Msg.RTC_MINUTE, "minute"],
  4215. [Blockly.Msg.RTC_SECOND, "second"],
  4216. ]), "time_detail")
  4217. .appendField(Blockly.Msg.RTC_WEI)
  4218. .appendField(new Blockly.FieldNumber(0, 0, null), "time")
  4219. this.setColour(rtcColor);
  4220. this.setPreviousStatement(true);
  4221. this.setNextStatement(true);
  4222. this.setHelpUrl("");
  4223. var thisBlock = this;
  4224. this.setTooltip(function () {
  4225. var mode = thisBlock.getFieldValue('time_detail');
  4226. var TOOLTIPS = {
  4227. "hour": Blockly.Msg.RTC_SETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_HOUR),
  4228. "minute": Blockly.Msg.RTC_SETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_MINUTE),
  4229. "second": Blockly.Msg.RTC_SETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_SECOND),
  4230. };
  4231. return TOOLTIPS[mode];
  4232. });
  4233. }
  4234. };
  4235. Blockly.Python['RTC_setTime'] = function (block) {
  4236. var time_detail = block.getFieldValue("time_detail");
  4237. var time = block.getFieldValue("time");
  4238. var code = '';
  4239. if (time_detail == "hour") {
  4240. code = 'rtc.setHour(' + time + ')\n';
  4241. }
  4242. else if (time_detail == "minute") {
  4243. code = 'rtc.setMinutes(' + time + ')\n';
  4244. }
  4245. else {
  4246. code = 'rtc.setSeconds(' + time + ')\n';
  4247. }
  4248. return code;
  4249. };
  4250. Blockly.Blocks['RTC_setWeek'] = {
  4251. init: function () {
  4252. this.appendDummyInput()
  4253. .appendField(Blockly.Msg.RTC_setWEEK)
  4254. .appendField(new Blockly.FieldNumber(1, 0, null), "week");
  4255. this.setColour(rtcColor);
  4256. this.setPreviousStatement(true);
  4257. this.setNextStatement(true);
  4258. this.setTooltip(Blockly.Msg.RTC_setWeek_TOOLTIP);
  4259. this.setHelpUrl("");
  4260. }
  4261. };
  4262. Blockly.Python['RTC_setWeek'] = function (block) {
  4263. var week = block.getFieldValue("week");
  4264. var code = 'rtc.setDayOfWeek(' + week + ')\n';
  4265. return code;
  4266. };