extension_module.js 189 KB

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