1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675 |
- // Serial
- Blockly.Blocks['serial_write_data_create_with_container'] = {
- init: function () {
- this.setColour("#22b845");
- this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_write_item);
- this.appendStatementInput('STACK');
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['serial_write_data_create_with_item'] = {
- init: function () {
- this.setColour("#22b845");
- this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_write_data);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['serial_write_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/uart_serial_send_header.png", 110, 35, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_write_title_text);
- this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
- .appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps")
- .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
- this.appendValueInput("ADD1")
- .setCheck(null)
- // .appendField("Field 0");
- this.itemCount_ = 1;
- this.setColour("#22b845");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'iot': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
- 'ai': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- },
- };
- Blockly.Python['serial_write_data'] = function (block) {
- // var api = block.getFieldValue('t_api');
- var number_uart_bps = block.getFieldValue('uart_bps');
- var ai_uart_data = Blockly.Python.valueToCode(this, "ADD1", Blockly.Python.ORDER_NONE);
- Blockly.Python.definitions_['v831_import_serial'] = `import serial
- SERIAL = serial.Serial("/dev/ttyS1",${number_uart_bps})
- `
- var code = `uart_data = bytes(str(${ai_uart_data})+"\\n","utf-8")
- SERIAL.write(uart_data)
- `
- return code;
- };
- Blockly.Blocks['serial_write_data_coco'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/cocorobo.png", 65, 65, { alt: "*", flipRtl: "FALSE" }));
- // this.appendDummyInput()
- // .appendField(Blockly.Msg.serialcomm_write_title_text_coco);
- this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
- .appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps");
- this.appendDummyInput()
- .appendField(Blockly.Msg.serial_send_data_on_send_data_cocorobo);
- /*this.appendValueInput("first_input")
- .setCheck(null)
- .appendField("Field 0");*/
- this.itemCount_ = 1;
- this.updateShape_();
- this.setMutator(new Blockly.Mutator(['serial_write_data_create_with_item']));
- this.setColour("#22b845");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'iot': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
- 'ai': Blockly.Msg.Serial_Write_Data_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- },
- mutationToDom: function () {
- var container = document.createElement('mutation');
- container.setAttribute('items', this.itemCount_);
- return container;
- },
- domToMutation: function (xmlElement) {
- this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
- this.updateShape_();
- },
- decompose: function (workspace) {
- var containerBlock = workspace.newBlock('serial_write_data_create_with_container');
- containerBlock.initSvg();
- var connection = containerBlock.getInput('STACK').connection;
- for (var i = 0; i < this.itemCount_; i++) {
- var itemBlock = workspace.newBlock('serial_write_data_create_with_item');
- itemBlock.initSvg();
- connection.connect(itemBlock.previousConnection);
- connection = itemBlock.nextConnection;
- }
- return containerBlock;
- },
- compose: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- // Count number of inputs.
- var connections = [];
- while (itemBlock) {
- connections.push(itemBlock.valueConnection_);
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- // Disconnect any children that don't belong.
- for (var i = 0; i < this.itemCount_; i++) {
- var connection = this.getInput('ADD' + i).connection.targetConnection;
- if (connection && connections.indexOf(connection) == -1) {
- connection.disconnect();
- }
- }
- this.itemCount_ = connections.length;
- this.updateShape_();
- // Reconnect any child blocks.
- for (var i = 0; i < this.itemCount_; i++) {
- Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
- }
- },
- saveConnections: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- var i = 0;
- while (itemBlock) {
- var input = this.getInput('ADD' + i);
- itemBlock.valueConnection_ = input && input.connection.targetConnection;
- i++;
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- },
- updateShape_: function () {
- for (var i = 0; i < this.itemCount_; i++) {
- if (!this.getInput('ADD' + i)) {
- var input = this.appendValueInput('ADD' + i);
- input.appendField(Blockly.Msg.serialcomm_write_item_first + i + Blockly.Msg.serialcomm_write_item_last);
- //input.appendField("資料 " + (i + 1) + ":");
- //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
- // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
- }
- }
- while (this.getInput('ADD' + i)) {
- this.removeInput('ADD' + i);
- i++;
- }
- },
- };
- Blockly.Python['serial_write_data_coco'] = function (block) {
- // var api = block.getFieldValue('t_api');
- var number_uart_bps = block.getFieldValue('uart_bps');
- var item_value = '';
- var ai_uart_data = "";
- for (var n = 0; n < this.itemCount_; n++) {
- item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
- ai_uart_data += 'str(' + item_value + ') + \"|\" + ';
- }
- var ai_uart_data_final = ai_uart_data + "";
- Blockly.Python.definitions_['v831_import_serial'] = `import serial
- SERIAL = serial.Serial("/dev/ttyS1",${number_uart_bps})
- `
- var code = `uart_data = bytes("SOF|"+${ai_uart_data_final.slice(0, -8)}+"|\\r\\n","utf-8")
- SERIAL.write(uart_data)
- `
- return code;
- };
- Blockly.Blocks['serial_read_data_setup'] = {
- init: function () {
- let types = getLocalStorage("type")
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/uart_serial_read_header.png", 110, 35, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_read_setup_title_text + Blockly.Msg.serialcomm_read_setup_title_text_1);
- this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
- .appendField(new Blockly.FieldNumber(115200, 0, 500000, 1), "uart_bps")
- .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
- this.setColour("#22b845");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'iot': Blockly.Msg.Serial_Read_Data_Setup_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
- 'ai': Blockly.Msg.Serial_Read_Data_Setup_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['serial_read_data_setup'] = function (block) {
- var number_uart_bps = block.getFieldValue('uart_bps');
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['v831_import_serial'] = `import serial
- SERIAL = serial.Serial("/dev/ttyS1",${number_uart_bps})
- `;
- Blockly.Python.definitions_['_read_serial_data'] = `def _read_serial_data(read_data,split, index):
- if read_data != None:
- read_str = ""
- try:
- read_str = str(read_data.decode("utf-8")).split(split)[index]
- except:
- read_str = str(read_data).split(split)[index]
- return read_str
- `
- var code = "";
- return code;
- };
- Blockly.Blocks['serial_read_data_setup_arduino'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/uart_serial_read_header.png", 110, 35, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_read_setup_title_text_arduino);
- this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
- .appendField(new Blockly.FieldNumber(9600, 0, 500000, 1), "uart_bps")
- .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
- this.setColour("#22b845");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'iot': Blockly.Msg.Serial_Read_Data_Setup_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_iot),
- 'ai': Blockly.Msg.Serial_Read_Data_Setup_TOOLTIP.replace('%1', Blockly.Msg.serialcomm_write_on_ai)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['serial_read_data_setup_arduino'] = function (block) {
- var number_uart_bps = block.getFieldValue('uart_bps');
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_["v831_import_os"] = `import os`
- Blockly.Python.definitions_['v831_import_serial_ardino_read'] = `import serial
- SERIALPATH = "/dev/ttyUSB0" if os.path.exists("/dev/ttyUSB0") else "/dev/ttyACM0"
- SERIAL = serial.Serial(SERIALPATH,${number_uart_bps})
- `;
- var code = "";
- return code;
- };
- Blockly.Blocks['serial_read_data_all'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.Serial_Read_Data_all)
- // .appendField(new Blockly.FieldDropdown([
- // ["serial_data", "serial_data"]
- // ]), "TYPE");
- this.setOutput(true, null);
- this.setColour("#22b845");
- this.setTooltip(Blockly.Msg.Serial_Read_Data_all);
- this.setHelpUrl("");
- }
- }
- Blockly.Python['serial_read_data_all'] = function (block) {
- var code = `SERIAL.readline().decode("UTF-8","ignore").strip()`;
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['serial_read_data_all_usb'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.Serial_Read_Data_all_usb)
- // .appendField(new Blockly.FieldDropdown([
- // ["serial_data", "serial_data"]
- // ]), "TYPE");
- this.setOutput(true, null);
- this.setColour("#22b845");
- this.setTooltip(Blockly.Msg.Serial_Read_Data_all);
- this.setHelpUrl("");
- }
- }
- Blockly.Python['serial_read_data_all_usb'] = function (block) {
- var code = `SERIAL.readline().decode("UTF-8","ignore").strip()`;
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['serial_read_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_GET_TEXT)
- .appendField(new Blockly.FieldDropdown([
- ["0", "0"],
- ["1", "1"],
- ["2", "2"],
- ["3", "3"],
- ["4", "4"],
- ["5", "5"],
- ["6", "6"],
- ["7", "7"],
- ["8", "8"],
- ["9", "9"],
- ["10", "10"],
- ["11", "11"],
- ]), "INDEX")
- .appendField(Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_TYPE_TEXT)
- // .appendField(new Blockly.FieldDropdown([
- // [Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_TYPE_STRING, "0"],
- // [Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_TYPE_INTEGER, "1"],
- // [Blockly.Msg.ARD_SERIAL_RECEIVE_DATASET_TYPE_FLOAT, "2"],
- // ]), "TYPE");
- this.setOutput(true, null);
- this.setColour("#22b845");
- this.setTooltip(Blockly.Msg.Serial_Read_Data_TOOLTIP);
- this.setHelpUrl("");
- }
- }
- Blockly.Python['serial_read_data'] = function (block) {
- var index = parseInt(block.getFieldValue("INDEX")) + 1;
- // var type = block.getFieldValue("TYPE")
- // var type_1 = block.getFieldValue("TYPE_1")
- Blockly.Python.definitions_['_read_serial_data'] = `def _read_serial_data(read_data,split, index):
- if read_data != None:
- read_str = ""
- try:
- read_str = str(read_data.decode("utf-8")).split(split)[index]
- except:
- read_str = str(read_data).split(split)[index]
- return read_str
- `
- var code = `_read_serial_data(SERIAL.readline().decode("UTF-8","ignore").strip(),"|",${index})`;
- return [code, Blockly.Python.ORDER_NONE];
- }
- Blockly.Blocks['serial_send_data_to_microbit'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/microbit_data_send_header.png", 90, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
- .appendField("115200")
- .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
- this.appendValueInput("NAME")
- // .appendField(Blockly.Msg.serial_send_data_on)
- .appendField(Blockly.Msg.serial_send_data_on_send_data);
- this.setInputsInline(false);
- this.setColour("#22b845");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.serial_send_data_on_send_data);
- }
- };
- Blockly.Python['serial_send_data_to_microbit'] = function (block) {
- var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC) || "";
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_["831_import_serial"] = `import serial
- SERIAL = serial.Serial("/dev/ttyS1",115200)`
- var code = `uart_data = bytes(${value_name}+"\\n","utf-8")
- SERIAL.write(uart_data)
- `;
- return code
- };
- Blockly.Blocks['serial_send_data_to_arduino'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/arduino.png", 90, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_write_title_text_arduino);
- this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_write_data_bps_title)
- .appendField(new Blockly.FieldNumber(9600, 0, 500000, 1), "uart_bps")
- .appendField(Blockly.Msg.serialcomm_write_data_bps_attrib);
- this.appendValueInput("NAME")
- // .appendField(Blockly.Msg.serial_send_data_on)
- .appendField(Blockly.Msg.serial_send_data_on_send_data_control_panel_arduino);
- this.setInputsInline(false);
- this.setColour("#22b845");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.serial_send_data_on_send_data);
- }
- };
- Blockly.Python['serial_send_data_to_arduino'] = function (block) {
- var number_uart_bps = block.getFieldValue('uart_bps');
- var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC) || "";
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_["v831_import_os"] = `import os`
- Blockly.Python.definitions_["v831_import_serial_ardino_read"] = `import serial
- SERIALPATH = "/dev/ttyUSB0" if os.path.exists("/dev/ttyUSB0") else "/dev/ttyACM0"
- SERIAL = serial.Serial(SERIALPATH,${number_uart_bps})`
- var code = `uart_data = bytes(${value_name}+"\\n","utf-8")
- SERIAL.write(uart_data)
- `;
- return code
- };
- /*
- ____
- / ___| ___ _ ____ _____
- \___ \ / _ \ '__\ \ / / _ \
- ___) | __/ | \ V / (_) |
- |____/ \___|_| \_/ \___/
- */
- var SERVO_BLOCK_COLOR = "#5bb2d6";
- var ai_servo_timer = [
- "Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM",
- "Timer.TIMER0, Timer.CHANNEL1, mode=Timer.MODE_PWM",
- "Timer.TIMER0, Timer.CHANNEL2, mode=Timer.MODE_PWM",
- "Timer.TIMER0, Timer.CHANNEL3, mode=Timer.MODE_PWM",
- "Timer.TIMER1, Timer.CHANNEL0, mode=Timer.MODE_PWM",
- "Timer.TIMER1, Timer.CHANNEL1, mode=Timer.MODE_PWM",
- "Timer.TIMER1, Timer.CHANNEL2, mode=Timer.MODE_PWM",
- "Timer.TIMER1, Timer.CHANNEL3, mode=Timer.MODE_PWM",
- "Timer.TIMER2, Timer.CHANNEL0, mode=Timer.MODE_PWM",
- "Timer.TIMER2, Timer.CHANNEL1, mode=Timer.MODE_PWM",
- "Timer.TIMER2, Timer.CHANNEL2, mode=Timer.MODE_PWM",
- "Timer.TIMER2, Timer.CHANNEL3, mode=Timer.MODE_PWM",
- "Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM"
- // "Timer.TIMER0, Timer.CHANNEL1, mode=Timer.MODE_PWM"
- ];
- var ai_servo_pin = [
- // '0',
- '1', '2', '3', '13', '14', '15', '17', '21', '22', '23', '24', '29', '30']
- Blockly.Blocks['extension_servo_setup_on_ai'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/esp32_servo_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.servo_setup_ai);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(servoCloor);
- this.setTooltip(Blockly.Msg.extension_servo_setup_on_ai_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['extension_servo_setup_on_ai'] = function (block) {
- Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
- Blockly.Python.definitions_['v831_import_time'] = `import time`
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
- // Blockly.Python.definitions_["v831_set_servo_S1_S2"] = `S1= multiFuncGpio(0,1)
- // S2= multiFuncGpio(1,1)`
- var code = '';
- return code;
- };
- Blockly.Blocks['extension_servo_write_on_ai'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.servo_set_gpio_ai)
- .appendField(new Blockly.FieldDropdown([
- ["S1", "S1"],
- ["S2", "S2"],
- ["P0", "P0"],
- ["P1", "P1"],
- ["P2", "P2"],
- ["P3", "P3"]
- ]), "gpio");
- this.appendValueInput("degree")
- .setCheck(null)
- .appendField(Blockly.Msg.servo_rotate_to_ai);
- this.appendDummyInput()
- .appendField(Blockly.Msg.servo_degree_ai);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(servoCloor);
- this.setTooltip(Blockly.Msg.extension_servo_write_on_ai_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- let extension_servo_write_on_ai_list = []
- Blockly.Python['extension_servo_write_on_ai'] = function (block) {
- var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC);
- var value_gpio = block.getFieldValue('gpio');
- Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
- Blockly.Python.definitions_['v831_import_time'] = `import time`
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- let code = ""
- if (value_gpio == "S1" || value_gpio == "S2") {
- Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
- const allBlocks = getBlocksByTypeName("extension_servo_write_on_ai")
- let onedegree = allBlocks[0].innerText
- let degree = 0
- let degree1 = 0
- try {
- if (value_gpio == "S1") {
- degree = onedegree.indexOf("S1") == 0 ? allBlocks[0].children[1].innerText : allBlocks[Math.floor((onedegree.indexOf("S1")) / 4)].children[1].innerText
- } else {
- degree1 = onedegree.indexOf("S2") == 0 ? allBlocks[0].children[1].innerText : allBlocks[Math.floor((onedegree.indexOf("S2")) / 4)].children[1].innerText
- }
- } catch (e) {
- console.log("error", e)
- }
- Blockly.Python.definitions_["v831_servo_init" + value_gpio] = `${value_gpio}= multiFuncGpio(${value_gpio[1] - 1},1)
- ${value_gpio}.servoCtrl(${allBlocks.length > 1 ? value_gpio == "S1" ? degree : degree1 : value_degree})`
- code = `${value_gpio}.servoCtrl(${value_degree})\n`
- } else {
- Blockly.Python.definitions_['v831_import_CocoPi_extServo'] = `from CocoPi import extServo`;
- Blockly.Python.definitions_["v831_servo_init_" + value_gpio] = `${value_gpio} = extServo(${value_gpio[1]})`
- code = `${value_gpio}.position(${value_degree})\n`
- }
- return code;
- };
- let servoCloor = "#386dc8"
- var iot_servo_pin = [
- '4', '12', '13', '14', '15', '16', '17', '21', '22', '25', '27']
- Blockly.Blocks['esp32_main_controller_servo_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/esp32_servo_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.servo_setup);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(servoCloor);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Servo_Setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_servo_setup'] = function (block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['definition_servo_setup'] = '' +
- 'from machine import Pin, PWM\n' +
- 'import time\n' +
- '';
- var code = '\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_servo_set'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.servo_set_gpio)
- .appendField(new Blockly.FieldDropdown([
- ["4", "0"],
- ["12", "1"],
- ["13", "2"],
- ["14", "3"],
- ["15", "4"],
- ["16", "5"],
- ["17", "6"],
- ["21", "7"],
- ["22", "8"],
- ["25", "9"],
- ["27", "10"]
- ]), "gpio");
- this.appendDummyInput()
- .appendField(Blockly.Msg.servo_rotate_to);
- this.appendValueInput("degree")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.servo_degree);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(servoCloor);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Servo_Set_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_servo_set'] = function (block) {
- var value_gpio = block.getFieldValue('gpio');
- var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- 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);
- Blockly.Python.definitions_['definition_servo_set'] = '' +
- 'def Servo(servo,angle):\n' +
- ' angle = angle - 90\n' +
- ' servo.freq(50)\n' +
- ' servo.duty(int(((angle+90)*2/180+0.5)/20*1023))\n' +
- ' time.sleep_ms(22)\n' +
- '';
- var code = '' +
- 'Servo(S' + iot_servo_pin[value_gpio] + ',' + value_degree + ')\n' +
- '';
- return code;
- };
- /*
- __ __ _
- | \/ | ___ | |_ ___ _ __
- | |\/| |/ _ \| __/ _ \| '__|
- | | | | (_) | || (_) | |
- |_| |_|\___/ \__\___/|_|
- */
- var MOTOR_BLOCK_COLOR = "#0000FF";
- Blockly.Blocks['esp32_motor_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/motor_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_motor_set_motor)
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(MOTOR_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Motor_Setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_motor_setup'] = function (block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['esp32_controller_set_motor'] = '' +
- 'import pca9685, machine, motor\n' +
- '\n' +
- '_iot_motor_i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))\n' +
- '_iot_motor = motor.DCMotors(_iot_motor_i2c)';
- var code = '';
- return code;
- };
- Blockly.Blocks['esp32_motor_run'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_motor_set_motor_turn)
- .appendField(new Blockly.FieldDropdown([
- ["A", "0"],
- ["B", "1"],
- ["C", "3"],
- ["D", "2"]
- ]), "motor_type")
- .appendField(Blockly.Msg.x_motor_set_motor_speed);
- this.appendValueInput("speed")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_motor_exec)
- // .appendField(new Blockly.FieldDropdown([
- // [Blockly.Msg.x_motor_cw, "pos"],
- // [Blockly.Msg.x_motor_acw, "neg"]
- // ]), "direction")
- .appendField(Blockly.Msg.x_motor_set_motor_turns);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(MOTOR_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.Esp32_Motor_Run_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_motor_run'] = function (block) {
- var dropdown_motor_type = block.getFieldValue('motor_type');
- var value_speed = Blockly.Python.valueToCode(block, 'speed', Blockly.Python.ORDER_ATOMIC);
- // var dropdown_direction = block.getFieldValue('direction');
- var code;
- if (value_speed.indexOf("-") == -1) {
- code = '_iot_motor.speed(' + dropdown_motor_type + ', (' + value_speed + '*16))\n';
- } else {
- code = '_iot_motor.speed(' + dropdown_motor_type + ', -(abs' + value_speed + '*16))\n';
- }
- return code;
- };
- Blockly.Blocks['ai_motor_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/motor_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_motor_set_motor_ai)
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(servoCloor);
- this.setTooltip(Blockly.Msg.ai_motor_setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_motor_setup'] = function (block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
- Blockly.Python.definitions_['v831_import_time'] = `import time`
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_CocoPi_stm8s_init'] = `from CocoPi import stm8s
- iic_slaver=stm8s()
- iic_slaver.clear()
- del iic_slaver`;
- Blockly.Python.definitions_['v831_import_CocoPi_stm8s'] = `from CocoPi import dcMotor`;
- // Blockly.Python.definitions_["v831_motor_init"] = `M1=dcMotor(1)
- // M2=dcMotor(2)`
- var code = '';
- return code;
- };
- Blockly.Blocks['ai_motor_run'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_motor_set_motor_turn_ai)
- .appendField(new Blockly.FieldDropdown([
- ["M1", "M1"],
- ["M2", "M2"],
- ["M3", "C"],
- ["M4", "D"],
- ["M5", "E"],
- ["M6", "F"]
- ]), "motor_type")
- .appendField(Blockly.Msg.x_motor_set_motor_speed_ai);
- this.appendValueInput("speed")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_motor_exec_ai)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.x_motor_cw_ai, "pos"],
- [Blockly.Msg.x_motor_acw_ai, "neg"]
- ]), "direction")
- .appendField(Blockly.Msg.x_motor_set_motor_turns_ai);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(servoCloor);
- this.setTooltip(Blockly.Msg.ai_motor_run_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_motor_run'] = function (block) {
- var dropdown_motor_type = block.getFieldValue('motor_type');
- var value_speed = Blockly.Python.valueToCode(block, 'speed', Blockly.Python.ORDER_ATOMIC);
- var dropdown_direction = block.getFieldValue('direction');
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
- Blockly.Python.definitions_['v831_import_time'] = `import time`
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- var code = ''
- if (dropdown_motor_type == "M1" || dropdown_motor_type == "M2") {
- Blockly.Python.definitions_['v831_import_CocoPi_stm8s_init'] = `from CocoPi import stm8s
- iic_slaver=stm8s()
- iic_slaver.clear()
- del iic_slaver`;
- Blockly.Python.definitions_['v831_import_CocoPi_stm8s'] = `from CocoPi import dcMotor`;
- Blockly.Python.definitions_["v831dcMotorCtrlFun" + dropdown_motor_type] = `${dropdown_motor_type} = dcMotor(${dropdown_motor_type.slice(1, 2)})`
- if (dropdown_direction == 'pos') {
- code = `${dropdown_motor_type}.dcMotorCtrl(1,${value_speed})\n`
- }
- else {
- code = `${dropdown_motor_type}.dcMotorCtrl(0,${value_speed})\n`
- }
- } else {
- Blockly.Python.definitions_['v831_import_CocoPi_extDcMotor'] = `from CocoPi import extDcMotor`;
- Blockly.Python.definitions_["v831dcMotorCtrlFun" + dropdown_motor_type] = `${dropdown_motor_type} = extDcMotor("${dropdown_motor_type}")`
- if (dropdown_direction == 'pos') {
- code = `${dropdown_motor_type}.speedControl(${value_speed})\n`
- }
- else {
- code = `${dropdown_motor_type}.speedControl(-${value_speed})\n`
- }
- }
- return code;
- };
- // ai_motor_stop
- Blockly.Blocks['ai_motor_stop'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_motor_set_motor_turn_ai)
- .appendField(new Blockly.FieldDropdown([
- ["M1", "M1"],
- ["M2", "M2"],
- ["M3", "C"],
- ["M4", "D"],
- ["M5", "E"],
- ["M6", "F"]
- ]), "motor_type")
- .appendField(Blockly.Msg.x_motor_set_motor_speed_stop);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(servoCloor);
- this.setTooltip(Blockly.Msg.x_motor_set_motor_speed_stop);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_motor_stop'] = function (block) {
- var dropdown_motor_type = block.getFieldValue('motor_type');
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
- Blockly.Python.definitions_['v831_import_time'] = `import time`
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- var code = ''
- if (dropdown_motor_type == "M1" || dropdown_motor_type == "M2") {
- Blockly.Python.definitions_['v831_import_CocoPi_stm8s_init'] = `from CocoPi import stm8s
- iic_slaver=stm8s()
- iic_slaver.clear()
- del iic_slaver`;
- Blockly.Python.definitions_['v831_import_CocoPi_stm8s'] = `from CocoPi import dcMotor`;
- Blockly.Python.definitions_["v831dcMotorCtrlFun" + dropdown_motor_type] = `${dropdown_motor_type} = dcMotor(${dropdown_motor_type.slice(1, 2)})`
-
- code = `${dropdown_motor_type}.dcMotorCtrl(0,0)\n`
- } else {
- Blockly.Python.definitions_['v831_import_CocoPi_extDcMotor'] = `from CocoPi import extDcMotor`;
- Blockly.Python.definitions_["v831dcMotorCtrlFun" + dropdown_motor_type] = `${dropdown_motor_type} = extDcMotor("${dropdown_motor_type}")`
- code = `${dropdown_motor_type}.speedControl(0)\n`
- }
- return code;
- };
- /*
- _ _____ ____ __ __ _ _
- | | | ____| _ \ | \/ | __ _| |_ _ __(_)_ __
- | | | _| | | | | | |\/| |/ _` | __| '__| \ \/ /
- | |___| |___| |_| | | | | | (_| | |_| | | |> <
- |_____|_____|____/ |_| |_|\__,_|\__|_| |_/_/\_\
- */
- let AILEDCOLOR = "#e8795b"
- Blockly.Blocks['ai_led_matrix_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/led_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_setup_ai);
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_1)
- .appendField(new Blockly.FieldNumber(50, 0, 255, 1), "brightness")
- .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_range);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.ai_led_matrix_setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_led_matrix_setup'] = function (block) {
- var number_brightness = block.getFieldValue('brightness');
- Blockly.Python.definitions_['ai_controller_led_setup'] = '' +
- 'from modules import ws2812\n' +
- '\n' +
- '_ai_led_matrix_total = 36\n' +
- '_ai_led_matrix = ws2812(24,_ai_led_matrix_total)\n' +
- '_ai_brightness = ' + number_brightness + '\n' +
- '\n' +
- 'def rgba_to_rgb_conversion(red, green, blue):\n' +
- ' if _ai_brightness <= 255:\n' +
- ' alpha = _ai_brightness / 255\n' +
- ' elif _ai_brightness > 255:\n' +
- ' alpha = 255 / 255\n' +
- ' elif _ai_brightness < 0:\n' +
- ' alpha = 0 / 255\n' +
- ' final_red = int((1 - alpha) * 0 + alpha * red)\n' +
- ' final_green = int((1 - alpha) * 0 + alpha * green)\n' +
- ' final_blue = int((1 - alpha) * 0 + alpha * blue)\n' +
- ' final = (final_red, final_green, final_blue)\n' +
- ' return final\n' +
- '';
- // TODO: Assemble Python into code variable.
- var code = '';
- return code;
- };
- Blockly.Blocks['ai_led_matrix_set_bright'] = {
- init: function () {
- this.appendValueInput("brightness")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_width_ai_set_brightness_1);
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_range);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.ai_led_matrix_set_bright_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_led_matrix_set_bright'] = function (block) {
- var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '_ai_brightness = ' + value_brightness + '\n';
- return code;
- };
- Blockly.Blocks['ai_led_matrix_xy'] = {
- init: function () {
- this.appendValueInput("x")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_width_ai_x);
- this.appendValueInput("y")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_width_ai_y);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.ai_led_matrix_xy_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_led_matrix_xy'] = function (block) {
- var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
- var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = value_x + ',' + value_y;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_led_matrix_wh'] = {
- init: function () {
- this.appendValueInput("w")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_width_ai);
- this.appendValueInput("h")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_height_ai);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.ai_led_matrix_wh_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_led_matrix_wh'] = function (block) {
- var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
- var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = value_w + ',' + value_h;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_led_matrix_color_picker'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_color_ai)
- .appendField(new Blockly.FieldColour("#ff0000"), "color");
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Color_Picker_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_led_matrix_color_picker'] = function (block) {
- var color = block.getFieldValue('color');
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- // TODO: Assemble Python into code variable.
- var code = d + ',' + e + ',' + f;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['ai_led_matrix_draw_pixel'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/led_draw-pixel.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_draw_title_ai);
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_draw_pixel_ai);
- this.appendValueInput("color")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_parameter_color_ai);
- this.appendValueInput("coordinate")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_parameter_coord_ai);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.ai_led_matrix_draw_pixel_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_led_matrix_draw_pixel'] = function (block) {
- var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
- var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = 'ai_pixel_value_coordinate = ' + value_coordinate + '\n' +
- '_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';
- return code;
- };
- Blockly.Blocks['ai_led_matrix_draw_rectangle'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/led_draw-rect.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_draw_title_ai);
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_draw_rect_ai);
- this.appendValueInput("color")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_parameter_color_ai);
- this.appendValueInput("coordinate")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_parameter_coord_ai);
- this.appendValueInput("size")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_parameter_size_ai);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.ai_led_matrix_draw_rectangle_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_led_matrix_draw_rectangle'] = function (block) {
- var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
- var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
- var value_size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.codeFunctions_['ai_led_matrix_draw_rectangle_setup'] = '' +
- 'def _ai_led_draw_rectangle(x, y, w, h, rgb):\n' +
- ' for i in range(x, x+ w, 1):\n' +
- ' for j in range(y, y+h, 1):\n' +
- ' _a = _ai_led_matrix.set_led((((j*6)//6)-1)*6+(i-1),(rgb))\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- // var comma_location = value_coordinate.indexOf(",");
- // var last_bracket_location = value_coordinate.length;
- // var cord_x = value_coordinate.substr(1, comma_location - 1);
- // var cord_y_pre = value_coordinate.substr(comma_location + 1, last_bracket_location - 1);
- // var cord_y = cord_y_pre.substring(0, cord_y_pre.length - 1)
- // // console.log(value_coordinate + ": " + cord_x + ", " + cord_y);
- // var comma_location_size = value_size.indexOf(",");
- // var last_bracket_location_size = value_size.length;
- // var size_width = value_size.substr(1, comma_location_size - 1);
- // var size_height_pre = value_size.substr(comma_location_size + 1, last_bracket_location_size - 1);
- // var size_height = size_height_pre.substring(0, size_height_pre.length - 1)
- // console.log("Size string length: " + last_bracket_location_size);
- // console.log("Size is " + value_size + ": " + size_width + ", " + size_height);
- var code = 'ai_rectangle_value_coordinate = ' + value_coordinate + '\n' +
- 'ai_rectangle_value_size = ' + value_size + '\n' +
- '_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';
- return code;
- };
- Blockly.Blocks['ai_led_pattern_6x6'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(
- new Blockly.FieldImage("./../blockly/media/pattern.png", 25, 25, "15"))
- .appendField(Blockly.Msg.LED_DRAW + Blockly.Msg.LED_PATTERN);
- this.appendDummyInput()
- .appendField(" ")
- .appendField((new Blockly.FieldColour("#000000")), "6_1")
- .appendField((new Blockly.FieldColour("#000000")), "6_2")
- .appendField((new Blockly.FieldColour("#000000")), "6_3")
- .appendField((new Blockly.FieldColour("#000000")), "6_4")
- .appendField((new Blockly.FieldColour("#000000")), "6_5")
- .appendField((new Blockly.FieldColour("#000000")), "6_6")
- .appendField(" ");
- this.appendDummyInput()
- .appendField(" ")
- .appendField((new Blockly.FieldColour("#000000")), "5_1")
- .appendField((new Blockly.FieldColour("#000000")), "5_2")
- .appendField((new Blockly.FieldColour("#000000")), "5_3")
- .appendField((new Blockly.FieldColour("#000000")), "5_4")
- .appendField((new Blockly.FieldColour("#000000")), "5_5")
- .appendField((new Blockly.FieldColour("#000000")), "5_6")
- .appendField(" ");
- this.appendDummyInput()
- .appendField(" ")
- .appendField((new Blockly.FieldColour("#000000")), "4_1")
- .appendField((new Blockly.FieldColour("#000000")), "4_2")
- .appendField((new Blockly.FieldColour("#000000")), "4_3")
- .appendField((new Blockly.FieldColour("#000000")), "4_4")
- .appendField((new Blockly.FieldColour("#000000")), "4_5")
- .appendField((new Blockly.FieldColour("#000000")), "4_6")
- .appendField(" ");
- this.appendDummyInput()
- .appendField(" ")
- .appendField((new Blockly.FieldColour("#000000")), "3_1")
- .appendField((new Blockly.FieldColour("#000000")), "3_2")
- .appendField((new Blockly.FieldColour("#000000")), "3_3")
- .appendField((new Blockly.FieldColour("#000000")), "3_4")
- .appendField((new Blockly.FieldColour("#000000")), "3_5")
- .appendField((new Blockly.FieldColour("#000000")), "3_6")
- .appendField(" ");
- this.appendDummyInput()
- .appendField(" ")
- .appendField((new Blockly.FieldColour("#000000")), "2_1")
- .appendField((new Blockly.FieldColour("#000000")), "2_2")
- .appendField((new Blockly.FieldColour("#000000")), "2_3")
- .appendField((new Blockly.FieldColour("#000000")), "2_4")
- .appendField((new Blockly.FieldColour("#000000")), "2_5")
- .appendField((new Blockly.FieldColour("#000000")), "2_6")
- .appendField(" ");
- this.appendDummyInput()
- .appendField(" ")
- .appendField((new Blockly.FieldColour("#000000")), "1_1")
- .appendField((new Blockly.FieldColour("#000000")), "1_2")
- .appendField((new Blockly.FieldColour("#000000")), "1_3")
- .appendField((new Blockly.FieldColour("#000000")), "1_4")
- .appendField((new Blockly.FieldColour("#000000")), "1_5")
- .appendField((new Blockly.FieldColour("#000000")), "1_6")
- .appendField(" ");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour(AILEDCOLOR);
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- var ai_led_pattern_6x6_length = 0;
- Blockly.Python['ai_led_pattern_6x6'] = function (block) {
- // TODO: Assemble Python into code variable.
- var rgb = [];
- ai_led_pattern_6x6_length = ai_led_pattern_6x6_length + 1;
- for (var i = 6; i >= 1; i--) {
- for (var j = 1; j < 7; j++) {
- var color = block.getFieldValue(i + '_' + j);
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- rgb[(parseInt((i * 6) / 6) - 1) * 6 + (j - 1)] = '(rgba_to_rgb_conversion(' + d + ',' + e + ',' + f + '))';
- }
- }
- Blockly.Python.codeFunctions_['ai_led_pattern_6x6_setup'] = '' +
- 'def _ai_led_draw_picture(rgb):\n' +
- ' for i in range(6, 0, -1):\n' +
- ' for j in range(1, 7):\n' +
- ' _a = _ai_led_matrix.set_led((((i*6)//6)-1)*6+(j-1),(rgb[(((i*6)//6)-1)*6+(j-1)]))\n' +
- '\n';
- Blockly.Python.codeFunctions_['ai_led_pattern_6x6_' + ai_led_pattern_6x6_length + '_setup'] = '' +
- 'ai_led_pattern_6x6_' + ai_led_pattern_6x6_length + ' = [' + rgb + ']' +
- '\n';
- var code = '_ai_led_draw_picture(ai_led_pattern_6x6_' + ai_led_pattern_6x6_length + ')\n';
- return code;
- };
- Blockly.Blocks['ai_led_matrix_show_above'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_show_above_ai);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.ai_led_matrix_show_above_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_led_matrix_show_above'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_ai_led_matrix.display()\n';
- return code;
- };
- Blockly.Blocks['ai_led_matrix_clear_screen'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_clear_all_ai);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.ai_led_matrix_clear_screen_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_led_matrix_clear_screen'] = function (block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['ai_led_matrix_clear_screen_setup'] = '' +
- 'def _ai_led_draw_rectangle(x, y, w, h, rgb):\n' +
- ' for i in range(x, x+ w, 1):\n' +
- ' for j in range(y, y+h, 1):\n' +
- ' _a = _ai_led_matrix.set_led((((i*6)//6)-1)*6+(j-1),(rgb))\n' +
- '';
- var code = '' +
- '_ai_led_draw_rectangle(1, 1, 6, 6, rgba_to_rgb_conversion(0, 0, 0))\n' +
- '_ai_led_matrix.display()\n' +
- '';
- return code;
- };
- Blockly.Blocks['ai_led_matrix_rgb_value_input'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_r);
- this.appendValueInput("rgb_value_r")
- .setCheck(null)
- .appendField("");
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_g);
- this.appendValueInput("rgb_value_g")
- .setCheck(null)
- .appendField("");
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_b);
- this.appendValueInput("rgb_value_b")
- .setCheck(null)
- .appendField("");
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.ai_led_matrix_rgb_value_input_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['ai_led_matrix_rgb_value_input'] = function (block) {
- var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
- var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
- var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['iot_led_matrix_rgb_value_input'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_r);
- this.appendValueInput("rgb_value_r")
- .setCheck(null)
- .appendField("");
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_g);
- this.appendValueInput("rgb_value_g")
- .setCheck(null)
- .appendField("");
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_b);
- this.appendValueInput("rgb_value_b")
- .setCheck(null)
- .appendField("");
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Rgb_Value_Input_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_led_matrix_rgb_value_input'] = function (block) {
- var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
- var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
- var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['iot_led_matrix_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/led_setup.png", 50, 40, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_setup_iot);
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_setup_iot_io)
- .appendField(new Blockly.FieldDropdown([
- ["25", "25"],
- ["15", "15"]
- ]), "io");
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_setup_iot_set_bright_1)
- .appendField(new Blockly.FieldNumber(50, 0, 255, 1), "brightness")
- .appendField(Blockly.Msg.x_led_matrix_setup_iot_set_bright_range);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Setup_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_led_matrix_setup'] = function (block) {
- var number_brightness = block.getFieldValue('brightness');
- var dropdown_io = block.getFieldValue('io');
- Blockly.Python.definitions_['iot_controller_led_setup'] = '' +
- 'import machine, neopixel, time\n' +
- '_6x6_led_matrix = neopixel.NeoPixel(machine.Pin(' + dropdown_io + '), 36)\n' +
- '_iot_brightness = ' + number_brightness + '\n' +
- '\n' +
- 'def rgba_to_rgb_conversion(red, green, blue):\n' +
- ' if _iot_brightness <= 255:\n' +
- ' alpha = _iot_brightness / 255\n' +
- ' elif _iot_brightness > 255:\n' +
- ' alpha = 255 / 255\n' +
- ' elif _iot_brightness < 0:\n' +
- ' alpha = 0 / 255\n' +
- ' final_red = int((1 - alpha) * 0 + alpha * red)\n' +
- ' final_green = int((1 - alpha) * 0 + alpha * green)\n' +
- ' final_blue = int((1 - alpha) * 0 + alpha * blue)\n' +
- ' final = (final_red, final_green, final_blue)\n' +
- ' return final\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '';
- return code;
- };
- Blockly.Blocks['iot_led_matrix_set_bright'] = {
- init: function () {
- this.appendValueInput("brightness")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_width_ai_set_brightness_1);
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_setup_ai_set_bright_range);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Set_Bright_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_led_matrix_set_bright'] = function (block) {
- var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '_iot_brightness = ' + value_brightness + '\n';
- return code;
- };
- Blockly.Blocks['iot_led_matrix_xy'] = {
- init: function () {
- this.appendValueInput("x")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_width_iot_x);
- this.appendValueInput("y")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_width_iot_y);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.Iot_Led_Matrix_xy_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_led_matrix_xy'] = function (block) {
- var value_x = Blockly.Python.valueToCode(block, 'x', Blockly.Python.ORDER_ATOMIC);
- var value_y = Blockly.Python.valueToCode(block, 'y', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = value_x + ',' + value_y;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['iot_led_matrix_wh'] = {
- init: function () {
- this.appendValueInput("w")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_width_iot);
- this.appendValueInput("h")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_height_iot);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.Iot_Led_Matrix_wh_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_led_matrix_wh'] = function (block) {
- var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
- var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = value_w + ',' + value_h;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['iot_led_matrix_color_picker'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_color_iot)
- .appendField(new Blockly.FieldColour("#ff0000"), "color");
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Color_Picker_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_led_matrix_color_picker'] = function (block) {
- var color = block.getFieldValue('color');
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- // TODO: Assemble Python into code variable.
- var code = d + ',' + e + ',' + f;
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_MEMBER];
- };
- Blockly.Blocks['iot_led_matrix_draw_pixel'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/led_draw-pixel.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_draw_title_iot);
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_draw_pixel_iot);
- this.appendValueInput("color")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_parameter_color_iot);
- this.appendValueInput("coordinate")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_parameter_coord_iot);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Draw_Pixel_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_led_matrix_draw_pixel'] = function (block) {
- var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
- var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- // _6x6_led_matrix[(((1*6)//6)-1)*6+(1-1)] = (0, 0, 255)
- var code = 'iot_pixel_value_coordinate = ' + value_coordinate + '\n' +
- '_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';
- return code;
- };
- Blockly.Blocks['iot_led_matrix_draw_rectangle'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/led_draw-rect.png", 30, 30, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_draw_title_iot);
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_draw_rect_ai);
- this.appendValueInput("color")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_parameter_color_ai);
- this.appendValueInput("coordinate")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_parameter_coord_ai);
- this.appendValueInput("size")
- .setCheck(null)
- .appendField(Blockly.Msg.x_led_matrix_parameter_size_ai);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Draw_Rectangle_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_led_matrix_draw_rectangle'] = function (block) {
- var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
- var value_coordinate = Blockly.Python.valueToCode(block, 'coordinate', Blockly.Python.ORDER_ATOMIC);
- var value_size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['iot_led_matrix_draw_rectangle_setup'] = '' +
- 'def _iot_led_draw_rectangle(x, y, w, h, rgb):\n' +
- ' for i in range(x, x+ w, 1):\n' +
- ' for j in range(y, y+h, 1):\n' +
- ' _6x6_led_matrix[(((j*6)//6)-1)*6+(i-1)] = (rgb)' +
- '\n';
- // TODO: Assemble Python into code variable.
- // var comma_location = value_coordinate.indexOf(",");
- // var last_bracket_location = value_coordinate.length;
- // var cord_x = value_coordinate.substr(1, comma_location - 1);
- // var cord_y_pre = value_coordinate.substr(comma_location + 1, last_bracket_location - 1);
- // var cord_y = cord_y_pre.substring(0, cord_y_pre.length - 1)
- // // console.log(value_coordinate + ": " + cord_x + ", " + cord_y);
- // var comma_location_size = value_size.indexOf(",");
- // var last_bracket_location_size = value_size.length;
- // var size_width = value_size.substr(1, comma_location_size - 1);
- // var size_height_pre = value_size.substr(comma_location_size + 1, last_bracket_location_size - 1);
- // var size_height = size_height_pre.substring(0, size_height_pre.length - 1)
- // console.log("Size string length: " + last_bracket_location_size);
- // console.log("Size is " + value_size + ": " + size_width + ", " + size_height);
- var code = 'iot_rectangle_value_coordinate = ' + value_coordinate + '\n' +
- 'iot_rectangle_value_size = ' + value_size + '\n' +
- '_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';
- return code;
- };
- Blockly.Blocks['iot_led_pattern_6x6'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(
- new Blockly.FieldImage("./../blockly/media/pattern.png", 25, 25, "15"))
- .appendField(Blockly.Msg.LED_DRAW + Blockly.Msg.LED_PATTERN);
- this.appendDummyInput()
- .appendField(" ")
- .appendField((new Blockly.FieldColour("#000000")), "6_1")
- .appendField((new Blockly.FieldColour("#000000")), "6_2")
- .appendField((new Blockly.FieldColour("#000000")), "6_3")
- .appendField((new Blockly.FieldColour("#000000")), "6_4")
- .appendField((new Blockly.FieldColour("#000000")), "6_5")
- .appendField((new Blockly.FieldColour("#000000")), "6_6")
- .appendField(" ");
- this.appendDummyInput()
- .appendField(" ")
- .appendField((new Blockly.FieldColour("#000000")), "5_1")
- .appendField((new Blockly.FieldColour("#000000")), "5_2")
- .appendField((new Blockly.FieldColour("#000000")), "5_3")
- .appendField((new Blockly.FieldColour("#000000")), "5_4")
- .appendField((new Blockly.FieldColour("#000000")), "5_5")
- .appendField((new Blockly.FieldColour("#000000")), "5_6")
- .appendField(" ");
- this.appendDummyInput()
- .appendField(" ")
- .appendField((new Blockly.FieldColour("#000000")), "4_1")
- .appendField((new Blockly.FieldColour("#000000")), "4_2")
- .appendField((new Blockly.FieldColour("#000000")), "4_3")
- .appendField((new Blockly.FieldColour("#000000")), "4_4")
- .appendField((new Blockly.FieldColour("#000000")), "4_5")
- .appendField((new Blockly.FieldColour("#000000")), "4_6")
- .appendField(" ");
- this.appendDummyInput()
- .appendField(" ")
- .appendField((new Blockly.FieldColour("#000000")), "3_1")
- .appendField((new Blockly.FieldColour("#000000")), "3_2")
- .appendField((new Blockly.FieldColour("#000000")), "3_3")
- .appendField((new Blockly.FieldColour("#000000")), "3_4")
- .appendField((new Blockly.FieldColour("#000000")), "3_5")
- .appendField((new Blockly.FieldColour("#000000")), "3_6")
- .appendField(" ");
- this.appendDummyInput()
- .appendField(" ")
- .appendField((new Blockly.FieldColour("#000000")), "2_1")
- .appendField((new Blockly.FieldColour("#000000")), "2_2")
- .appendField((new Blockly.FieldColour("#000000")), "2_3")
- .appendField((new Blockly.FieldColour("#000000")), "2_4")
- .appendField((new Blockly.FieldColour("#000000")), "2_5")
- .appendField((new Blockly.FieldColour("#000000")), "2_6")
- .appendField(" ");
- this.appendDummyInput()
- .appendField(" ")
- .appendField((new Blockly.FieldColour("#000000")), "1_1")
- .appendField((new Blockly.FieldColour("#000000")), "1_2")
- .appendField((new Blockly.FieldColour("#000000")), "1_3")
- .appendField((new Blockly.FieldColour("#000000")), "1_4")
- .appendField((new Blockly.FieldColour("#000000")), "1_5")
- .appendField((new Blockly.FieldColour("#000000")), "1_6")
- .appendField(" ");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour(AILEDCOLOR);
- this.setTooltip('');
- this.setHelpUrl('');
- }
- };
- var iot_led_pattern_6x6_length = 0;
- Blockly.Python['iot_led_pattern_6x6'] = function (block) {
- // TODO: Assemble Python into code variable.
- var rgb = [];
- iot_led_pattern_6x6_length = iot_led_pattern_6x6_length + 1;
- for (var i = 6; i >= 1; i--) {
- for (var j = 1; j < 7; j++) {
- var color = block.getFieldValue(i + '_' + j);
- var d = 0,
- e = 0,
- f = 0;
- try {
- 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
- e = parseInt(color.substring(3, 5), 16),
- f = parseInt(color.substring(5, 7), 16))
- } catch (g) { }
- rgb[(parseInt((i * 6) / 6) - 1) * 6 + (j - 1)] = '(rgba_to_rgb_conversion(' + d + ',' + e + ',' + f + '))';
- }
- }
- Blockly.Python.codeFunctions_['iot_led_pattern_6x6_setup'] = '' +
- 'def _iot_led_draw_picture(rgb):\n' +
- ' for i in range(6, 0, -1):\n' +
- ' for j in range(1, 7):\n' +
- ' _6x6_led_matrix[(((i*6)//6)-1)*6+(j-1)] = (rgb[(((i*6)//6)-1)*6+(j-1)])\n' +
- '\n';
- Blockly.Python.codeFunctions_['iot_led_pattern_6x6_' + iot_led_pattern_6x6_length + '_setup'] = '' +
- 'iot_led_pattern_6x6_' + iot_led_pattern_6x6_length + ' = [' + rgb + ']' +
- '\n';
- var code = '_iot_led_draw_picture(iot_led_pattern_6x6_' + iot_led_pattern_6x6_length + ')\n';
- return code;
- };
- Blockly.Blocks['iot_led_matrix_show_above'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_show_above_iot);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.Iot_Led_Matrix_Show_Above_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_led_matrix_show_above'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_6x6_led_matrix.write()\n';
- return code;
- };
- Blockly.Blocks['iot_led_matrix_clear_screen'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_led_matrix_clear_all_ai);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.ai_led_matrix_clear_screen_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_led_matrix_clear_screen'] = function (block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['iot_led_matrix_clear_screen_setup'] = '' +
- 'def _iot_led_draw_rectangle(x, y, w, h, rgb):\n' +
- ' for i in range(x, x+ w, 1):\n' +
- ' for j in range(y, y+h, 1):\n' +
- ' _6x6_led_matrix[(((j*6)//6)-1)*6+(i-1)] = (rgb)' +
- '\n';
- var code = '' +
- '_iot_led_draw_rectangle(1,1,6,6,rgba_to_rgb_conversion(0,0,0))\n' +
- '_6x6_led_matrix.write()\n' +
- '';
- return code;
- };
- /*
- _ _____ ____ ____ _ _
- | | | ____| _ \ / ___|| |_ _ __(_)_ __
- | | | _| | | | | \___ \| __| '__| | '_ \
- | |___| |___| |_| | ___) | |_| | | | |_) |
- |_____|_____|____/ |____/ \__|_| |_| .__/
- |_|
- */
- var ESP32_LED_STRIP_COLOR = AILEDCOLOR;
- Blockly.Blocks['esp32_main_controller_led_strip_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/led_strip_setup.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ledstrip_setup_text)
- .appendField(new Blockly.FieldVariable("STRIP"), "varitem")
- .appendField(Blockly.Msg.ledstrip_setup);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ledstrip_set_gpio)
- .appendField(new Blockly.FieldDropdown([
- ["S1", "1"],
- ["S2", "2"]
- ]), "value_io");
- this.appendValueInput("count")
- .setCheck(null)
- .appendField(Blockly.Msg.ledstrip_set_total_led);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_LED_STRIP_COLOR);
- this.setHelpUrl("");
- this.setTooltip();
- }
- };
- Blockly.Python['esp32_main_controller_led_strip_setup'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_io = block.getFieldValue('value_io');
- var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['v831_import_time'] = `import time`;
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
- Blockly.Python.addVariable(variable_name, `${variable_name} = multiFuncGpio(${value_io - 1},7)`, true);
- var code = ""
- return code;
- };
- Blockly.Blocks['esp32_main_controller_led_strip_set_bright'] = {
- init: function () {
- this.appendValueInput("brightness")
- .setCheck(null)
- .appendField(Blockly.Msg.ledstrip_set_brightness_1);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ledstrip_set_brightness_after);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Led_strip_set_bright_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_main_controller_led_strip_set_bright'] = function (block) {
- var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '_ai_brightness = ' + value_brightness + '\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_led_strip_set'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ledstrip_set_gpio)
- .appendField(new Blockly.FieldDropdown([
- ["S1", "1"],
- ["S2", "2"]
- ]), "value_io");
- this.appendValueInput("location")
- .setCheck(null)
- .appendField(Blockly.Msg.ledstrip_set_no);
- this.appendValueInput("color")
- .setCheck(null)
- .appendField(Blockly.Msg.ledstrip_set_color);
- this.appendValueInput("brightness")
- .setCheck(null)
- .appendField(Blockly.Msg.ledstrip_set_brightness_setup);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_LED_STRIP_COLOR);
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Led_Strip_Set_TOOLTIP.replace('%1', "CocoPi"));
- }
- };
- Blockly.Python['esp32_main_controller_led_strip_set'] = function (block) {
- var value_location = Blockly.Python.valueToCode(block, 'location', Blockly.Python.ORDER_ATOMIC);
- var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
- var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
- var value_io = block.getFieldValue('value_io');
- // var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['v831_import_time'] = `import time`;
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
- Blockly.Python.addVariable("STRIP" + value_io, `STRIP${value_io} = multiFuncGpio(${value_io - 1},7)
- STRIP${value_io}.setBrightness(${value_brightness})`, true);
- Blockly.Python.addVariable("setLedColor", 'setLedColor = ""', true)
- // TODO: Assemble Python into code variable.
- console.log(value_color)
- var code = `setLedColor = ${value_color}
- STRIP${value_io}.setRgbSerialColor(${value_location} - 1 ,setLedColor[0],setLedColor[1],setLedColor[2])
- STRIP${value_io}.rgbSerialShow()
- time.sleep(0.01)
- `
- return code;
- };
- Blockly.Blocks['esp32_main_controller_led_strip_set_one'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ledstrip_set_gpio)
- .appendField(new Blockly.FieldDropdown([
- ["S1", "1"],
- ["S2", "2"]
- ]), "value_io").appendField(Blockly.Msg.ledstrip_setup_text);
- this.appendValueInput("location")
- .setCheck(null)
- .appendField(Blockly.Msg.uav_get_shooting_value_0);
- this.appendValueInput("location1")
- .setCheck(null)
- .appendField(Blockly.Msg.OLCD_LINE_END)
- this.appendValueInput("color")
- .setCheck(null)
- .appendField(Blockly.Msg.ledstrip_set_color);
- this.appendValueInput("brightness")
- .setCheck(null)
- .appendField(Blockly.Msg.ledstrip_set_brightness_setup);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_LED_STRIP_COLOR);
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Led_Strip_Set_TOOLTIP.replace('%1', "CocoPi"));
- }
- };
- Blockly.Python['esp32_main_controller_led_strip_set_one'] = function (block) {
- var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
- var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
- var location1 = Blockly.Python.valueToCode(block, 'location1', Blockly.Python.ORDER_ATOMIC);
- var location = Blockly.Python.valueToCode(block, 'location', Blockly.Python.ORDER_ATOMIC);
- var value_io = block.getFieldValue('value_io');
- let allBlocks = block.workspace.getAllBlocks();
- let global = ""
- try {
- global = allBlocks[0].workspace.variableList.toString()
- }
- catch (e) {
- console.log(e)
- }
- // var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['v831_import_time'] = `import time`;
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
- Blockly.Python.addVariable("STRIP" + value_io, `STRIP${value_io} = multiFuncGpio(${value_io - 1},7)
- STRIP${value_io}.setBrightness(${value_brightness})`, true);
- Blockly.Python.addFunction("LEDSTRIPSHOW",`def LEDSTRIPSHOW(LEDCOLOR,num1,num2,brightness):
- for i in range(num1-1,num2+1):
- STRIP${value_io}.setRgbSerialColor(i-1 ,LEDCOLOR[0],LEDCOLOR[1],LEDCOLOR[2])
- STRIP${value_io}.rgbSerialShow()
- time.sleep(0.01)
- `,true)
- var code = `LEDSTRIPSHOW(${value_color},${location},${location1},${value_brightness})\n`
- return code;
- }
- Blockly.Blocks['esp32_main_controller_led_strip_set_all'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ledstrip_set_gpio)
- .appendField(new Blockly.FieldDropdown([
- ["S1", "1"],
- ["S2", "2"]
- ]), "value_io").appendField(Blockly.Msg.ledstrip_set_color_all_on);
- this.appendValueInput("color")
- .setCheck(null)
- .appendField(Blockly.Msg.ledstrip_set_color);
- this.appendValueInput("brightness")
- .setCheck(null)
- .appendField(Blockly.Msg.ledstrip_set_brightness_setup);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_LED_STRIP_COLOR);
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Led_Strip_Set_TOOLTIP.replace('%1', "CocoPi"));
- }
- };
- Blockly.Python['esp32_main_controller_led_strip_set_all'] = function (block) {
- var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
- var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
- var value_io = block.getFieldValue('value_io');
- let allBlocks = block.workspace.getAllBlocks();
- let global = ""
- try {
- global = allBlocks[0].workspace.variableList.toString()
- }
- catch (e) {
- console.log(e)
- }
- // var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['v831_import_time'] = `import time`;
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
- Blockly.Python.addVariable("STRIP" + value_io, `STRIP${value_io} = multiFuncGpio(${value_io - 1},7)
- STRIP${value_io}.setBrightness(${value_brightness})`, true);
- Blockly.Python.addFunction("LEDSTRIPSHOW",`def LEDSTRIPSHOW(LEDCOLOR,num1,num2,brightness):
-
- for i in range(num1-1,num2+1):
- STRIP${value_io}.setRgbSerialColor(i-1 ,LEDCOLOR[0],LEDCOLOR[1],LEDCOLOR[2])
- STRIP${value_io}.rgbSerialShow()
- time.sleep(0.01)
- `,true)
- var code = `LEDSTRIPSHOW(${value_color},1,10,${value_brightness})\n`
- return code;
- }
- Blockly.Blocks['esp32_main_controller_led_strip_set_all_off'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ledstrip_set_gpio_off)
- .appendField(new Blockly.FieldDropdown([
- ["S1", "1"],
- ["S2", "2"]
- ]), "value_io").appendField(Blockly.Msg.ledstrip_set_color_all_on);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_LED_STRIP_COLOR);
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.Esp32_Main_Controller_Led_Strip_Set_TOOLTIP.replace('%1', "CocoPi"));
- }
- };
- Blockly.Python['esp32_main_controller_led_strip_set_all_off'] = function (block) {
- var value_io = block.getFieldValue('value_io');
- // var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['v831_import_time'] = `import time`;
- Blockly.Python.definitions_['v831_import_sys'] = `import sys
- sys.path.append("/root/")`
- Blockly.Python.definitions_['v831_import_CocoPi_multiFuncGpio'] = `from CocoPi import multiFuncGpio`;
- Blockly.Python.addVariable("STRIPOFF" + value_io, `STRIPOFF${value_io} = multiFuncGpio(${value_io - 1},7)
- STRIPOFF${value_io}.setBrightness(0)`, true);
- Blockly.Python.addFunction("LEDSTRIPSHOWOFF",`def LEDSTRIPSHOWOFF(LEDCOLOR):
- for i in range(10):
-
- STRIPOFF${value_io}.setRgbSerialColor(i ,LEDCOLOR[0],LEDCOLOR[1],LEDCOLOR[2])
- STRIPOFF${value_io}.rgbSerialShow()
- time.sleep(0.01)
- `,true)
- var code = `LEDSTRIPSHOWOFF((0,0,0))\n`;
- return code;
- }
- Blockly.Blocks['led_strip_show_above'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ledstrip_setup_text_show_above_set)
- .appendField(new Blockly.FieldVariable("STRIP"), "varitem")
- .appendField(Blockly.Msg.ledstrip_setup_text_show_above);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(AILEDCOLOR);
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.Led_Strip_Show_Above_TOOLTIP.replace('%1', "CocoPi"));
- }
- };
- Blockly.Python['led_strip_show_above'] = function (block) {
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- // TODO: Assemble Python into code variable.
- var code = `${variable_name}.pixelShow()
- `
- return code;
- };
- Blockly.Blocks['led_strip_color_rgb'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_r);
- this.appendValueInput("rgb_value_r")
- .setCheck(null)
- .appendField("");
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_g);
- this.appendValueInput("rgb_value_g")
- .setCheck(null)
- .appendField("");
- this.appendDummyInput()
- .appendField(Blockly.Msg.image_process_rgb_b);
- this.appendValueInput("rgb_value_b")
- .setCheck(null)
- .appendField("");
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(AILEDCOLOR);
- this.setTooltip(Blockly.Msg.Led_Strip_Color_Rgb_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['led_strip_color_rgb'] = function (block) {
- var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
- var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
- var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- ____ ____ _
- / ___| __ _ _ __ ___ ___| _ \ __ _ __| |
- | | _ / _` | '_ ` _ \ / _ \ |_) / _` |/ _` |
- | |_| | (_| | | | | | | __/ __/ (_| | (_| |
- \____|\__,_|_| |_| |_|\___|_| \__,_|\__,_|
- */
- var GAMEPAD_COLOR = "#183895";
- Blockly.Blocks['x_iot_read_gamepad_button_pressed'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_gamepad_read_button_first_text_iot)
- .appendField(new Blockly.FieldDropdown([
- ["X", "x"],
- ["Y", "y"],
- ["A", "a"],
- ["B", "b"],
- ["L", "l"],
- ["R", "r"]
- ]), "type")
- .appendField(Blockly.Msg.x_gamepad_read_button_first_text_pressed_iot);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(GAMEPAD_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'x': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "X"),
- 'y': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "Y"),
- 'a': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "A"),
- 'b': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "B"),
- 'l': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "L"),
- 'r': Blockly.Msg.x_iot_read_gamepad_button_pressed_TOOLTIP.replace('%1', "R")
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['x_iot_read_gamepad_button_pressed'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
- 'from machine import ADC, Pin\n' +
- 'import time\n' +
- 'from machine import I2C\n' +
- 'from ads1x15 import ADS1115\n' +
- '\n';
- Blockly.Python.definitions_['gamepad_button_def_iot'] = '' +
- '_px = Pin(36, Pin.IN,Pin.PULL_UP)\n' +
- '_py = Pin(39, Pin.IN,Pin.PULL_UP)\n' +
- '_pa = Pin(4, Pin.IN,Pin.PULL_UP)\n' +
- '_pb = Pin(2, Pin.IN,Pin.PULL_UP)\n' +
- '_pl = Pin(17, Pin.IN,Pin.PULL_UP)\n' +
- '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '_p' + dropdown_type + '.value() == 1';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['x_iot_read_gamepad_button_released'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_gamepad_read_button_first_text_iot)
- .appendField(new Blockly.FieldDropdown([
- ["X", "x"],
- ["Y", "y"],
- ["A", "a"],
- ["B", "b"],
- ["L", "l"],
- ["R", "r"]
- ]), "type")
- .appendField(Blockly.Msg.x_gamepad_read_button_first_text_released_iot);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(GAMEPAD_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'x': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "X"),
- 'y': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "Y"),
- 'a': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "A"),
- 'b': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "B"),
- 'l': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "L"),
- 'r': Blockly.Msg.x_iot_read_gamepad_button_released_TOOLTIP.replace('%1', "R")
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['x_iot_read_gamepad_button_released'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
- 'from machine import ADC, Pin\n' +
- 'import time\n' +
- 'from machine import I2C\n' +
- 'from ads1x15 import ADS1115\n' +
- '\n';
- Blockly.Python.definitions_['gamepad_button_def_iot'] = '' +
- '_px = Pin(36, Pin.IN,Pin.PULL_UP)\n' +
- '_py = Pin(39, Pin.IN,Pin.PULL_UP)\n' +
- '_pa = Pin(4, Pin.IN,Pin.PULL_UP)\n' +
- '_pb = Pin(2, Pin.IN,Pin.PULL_UP)\n' +
- '_pl = Pin(17, Pin.IN,Pin.PULL_UP)\n' +
- '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '_p' + dropdown_type + '.value() == 0';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['x_iot_read_gamepad_button_x-r'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_gamepad_read_button_status_first_text_iot)
- .appendField(new Blockly.FieldDropdown([
- ["X", "x"],
- ["Y", "y"],
- ["A", "a"],
- ["B", "b"],
- ["L", "l"],
- ["R", "r"]
- ]), "type")
- .appendField(Blockly.Msg.x_gamepad_read_button_status_after_text_iot);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(GAMEPAD_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'x': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "X"),
- 'y': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "Y"),
- 'a': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "A"),
- 'b': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "B"),
- 'l': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "L"),
- 'r': Blockly.Msg.x_iot_read_gamepad_button_xr_TOOLTIP.replace('%1', "R")
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['x_iot_read_gamepad_button_x-r'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
- 'from machine import ADC, Pin\n' +
- 'import time\n' +
- 'from machine import I2C\n' +
- 'from ads1x15 import ADS1115\n' +
- '\n';
- Blockly.Python.definitions_['gamepad_button_def_iot'] = '' +
- '_px = Pin(36, Pin.IN,Pin.PULL_UP)\n' +
- '_py = Pin(39, Pin.IN,Pin.PULL_UP)\n' +
- '_pa = Pin(4, Pin.IN,Pin.PULL_UP)\n' +
- '_pb = Pin(2, Pin.IN,Pin.PULL_UP)\n' +
- '_pl = Pin(17, Pin.IN,Pin.PULL_UP)\n' +
- '_pr = Pin(16, Pin.IN,Pin.PULL_UP)\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '_p' + dropdown_type + '.value()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['x_iot_read_gamepad_joystick'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_gamepad_read_joystick_read_iot)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.x_gamepad_read_joystick_read_iot_x, "X"],
- [Blockly.Msg.x_gamepad_read_joystick_read_iot_y, "Y"]
- ]), "type")
- .appendField(Blockly.Msg.x_gamepad_read_joystick_read_after_iot);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(GAMEPAD_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'X': Blockly.Msg.x_iot_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_x),
- 'Y': Blockly.Msg.x_iot_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_y)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['x_iot_read_gamepad_joystick'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
- 'from machine import ADC, Pin\n' +
- 'import time\n' +
- 'from machine import I2C\n' +
- 'from ads1x15 import ADS1115\n' +
- '\n';
- Blockly.Python.definitions_['gamepad_joystick_def_iot'] = '' +
- '_adc_i2c = I2C(scl=Pin(22), sda=Pin(21))\n' +
- '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
- '\n';
- Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' +
- 'def map_number_scale(val):\n' +
- ' return int(((val - 0) / (26400-0)) * (1023-0) + 0)\n' +
- '\n' +
- 'def _read_direction():\n' +
- ' _adcX = map_number_scale(_joystick_adc.read(channel1=1))\n' +
- ' _adcY = map_number_scale(_joystick_adc.read(channel1=0))\n' +
- ' if (_adcY > 38) and (_adcY < 968) and (_adcX > 38) and (_adcX < 968):\n' +
- ' return "still"\n' +
- ' elif _adcX > 968:\n' +
- ' return "left"\n' +
- ' elif _adcX < 38:\n' +
- ' return "right"\n' +
- ' elif _adcY > 968:\n' +
- ' return "top"\n' +
- ' elif _adcY < 38:\n' +
- ' return "bottom"\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- if (dropdown_type == "X") {
- var code = 'map_number_scale(_joystick_adc.read(channel1=1))';
- } else if (dropdown_type == "Y") {
- var code = 'map_number_scale(_joystick_adc.read(channel1=0))';
- }
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['x_iot_read_gamepad_joystick_check'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_gamepad_read_joystick_when_iot)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.x_gamepad_read_joystick_when_top_iot, "top"],
- [Blockly.Msg.x_gamepad_read_joystick_when_bottom_iot, "bottom"],
- [Blockly.Msg.x_gamepad_read_joystick_when_left_iot, "left"],
- [Blockly.Msg.x_gamepad_read_joystick_when_right_iot, "right"],
- [Blockly.Msg.x_gamepad_read_joystick_when_not_iot, "still"],
- ]), "type");
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(GAMEPAD_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'top': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_top_iot),
- 'bottom': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_bottom_iot),
- 'left': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_left_iot),
- 'right': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_right_iot),
- 'still': Blockly.Msg.x_iot_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_not_iot)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['x_iot_read_gamepad_joystick_check'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['gamepad_all_def_iot'] = '' +
- 'from machine import ADC, Pin\n' +
- 'import time\n' +
- 'from machine import I2C\n' +
- 'from ads1x15 import ADS1115\n' +
- '\n';
- Blockly.Python.definitions_['gamepad_joystick_def_iot'] = '' +
- '_adc_i2c = I2C(scl=Pin(22), sda=Pin(21))\n' +
- '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
- '\n';
- Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' +
- 'def map_number_scale(val):\n' +
- ' return int(((val - 0) / (26400-0)) * (1023-0) + 0)\n' +
- '\n' +
- 'def _read_direction():\n' +
- ' _adcX = map_number_scale(_joystick_adc.read(channel1=1))\n' +
- ' _adcY = map_number_scale(_joystick_adc.read(channel1=0))\n' +
- ' if (_adcY > 38) and (_adcY < 968) and (_adcX > 38) and (_adcX < 968):\n' +
- ' return "still"\n' +
- ' elif _adcX > 968:\n' +
- ' return "left"\n' +
- ' elif _adcX < 38:\n' +
- ' return "right"\n' +
- ' elif _adcY > 968:\n' +
- ' return "top"\n' +
- ' elif _adcY < 38:\n' +
- ' return "bottom"\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '_read_direction() == "' + dropdown_type + '"';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['x_ai_read_gamepad_button_pressed'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_gamepad_read_button_first_text_ai)
- .appendField(new Blockly.FieldDropdown([
- ["X", "x"],
- ["Y", "y"],
- ["A", "a"],
- ["B", "b"],
- ["L", "l"],
- ["R", "r"]
- ]), "type")
- .appendField(Blockly.Msg.x_gamepad_read_button_first_text_pressed_ai);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(GAMEPAD_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'x': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "X"),
- 'y': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "Y"),
- 'a': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "A"),
- 'b': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "B"),
- 'l': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "L"),
- 'r': Blockly.Msg.x_ai_read_gamepad_button_pressed_TOOLTIP.replace('%1', "R")
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['x_ai_read_gamepad_button_pressed'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['gamepad_all_def'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import FPIOA, GPIO\n' +
- '\n';
- Blockly.Python.definitions_['gamepad_button_def'] = '' +
- '_gp_side_button = [13, 14]\n' +
- '_gp_right_button = [22, 21, 15, 17]\n' +
- 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' +
- 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' +
- 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' +
- 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' +
- 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' +
- 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' +
- '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' +
- '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' +
- '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' +
- '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' +
- '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' +
- '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '_gp_button_' + dropdown_type + '.value() == 1';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['x_ai_read_gamepad_button_released'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_gamepad_read_button_first_text_ai)
- .appendField(new Blockly.FieldDropdown([
- ["X", "x"],
- ["Y", "y"],
- ["A", "a"],
- ["B", "b"],
- ["L", "l"],
- ["R", "r"]
- ]), "type")
- .appendField(Blockly.Msg.x_gamepad_read_button_first_text_released_ai);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(GAMEPAD_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'x': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "X"),
- 'y': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "Y"),
- 'a': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "A"),
- 'b': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "B"),
- 'l': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "L"),
- 'r': Blockly.Msg.x_ai_read_gamepad_button_released_TOOLTIP.replace('%1', "R")
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['x_ai_read_gamepad_button_released'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['gamepad_all_def'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import FPIOA, GPIO\n' +
- '\n';
- Blockly.Python.definitions_['gamepad_button_def'] = '' +
- '_gp_side_button = [13, 14]\n' +
- '_gp_right_button = [15, 17, 22, 21]\n' +
- 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' +
- 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' +
- 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' +
- 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' +
- 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' +
- 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' +
- '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' +
- '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' +
- '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' +
- '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' +
- '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' +
- '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '_gp_button_' + dropdown_type + '.value() == 0';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['x_ai_read_gamepad_button_x-r'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_gamepad_read_button_status_first_text_ai)
- .appendField(new Blockly.FieldDropdown([
- ["X", "x"],
- ["Y", "y"],
- ["A", "a"],
- ["B", "b"],
- ["L", "l"],
- ["R", "r"]
- ]), "type")
- .appendField(Blockly.Msg.x_gamepad_read_button_status_after_text_ai);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(GAMEPAD_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'x': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "X"),
- 'y': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "Y"),
- 'a': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "A"),
- 'b': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "B"),
- 'l': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "L"),
- 'r': Blockly.Msg.x_ai_read_gamepad_button_xr_TOOLTIP.replace('%1', "R")
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['x_ai_read_gamepad_button_x-r'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['gamepad_all_def'] = '' +
- 'from fpioa_manager import *\n' +
- 'from Maix import FPIOA, GPIO\n' +
- '\n';
- Blockly.Python.definitions_['gamepad_button_def'] = '' +
- '_gp_side_button = [13, 14]\n' +
- '_gp_right_button = [15, 17, 22, 21]\n' +
- 'FPIOA().set_function(_gp_side_button[0],FPIOA.GPIO0)\n' +
- 'FPIOA().set_function(_gp_side_button[1],FPIOA.GPIO1)\n' +
- 'FPIOA().set_function(_gp_right_button[0],FPIOA.GPIO2)\n' +
- 'FPIOA().set_function(_gp_right_button[1],FPIOA.GPIO3)\n' +
- 'FPIOA().set_function(_gp_right_button[2],FPIOA.GPIO4)\n' +
- 'FPIOA().set_function(_gp_right_button[3],FPIOA.GPIO5)\n' +
- '_gp_button_l = GPIO(GPIO.GPIO0,GPIO.IN)\n' +
- '_gp_button_r = GPIO(GPIO.GPIO1,GPIO.IN)\n' +
- '_gp_button_x = GPIO(GPIO.GPIO2,GPIO.IN)\n' +
- '_gp_button_y = GPIO(GPIO.GPIO3,GPIO.IN)\n' +
- '_gp_button_a = GPIO(GPIO.GPIO4,GPIO.IN)\n' +
- '_gp_button_b = GPIO(GPIO.GPIO5,GPIO.IN)\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '_gp_button_' + dropdown_type + '.value()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['x_ai_read_gamepad_joystick'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_gamepad_read_joystick_read_ai)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.x_gamepad_read_joystick_read_iot_x, "0"],
- [Blockly.Msg.x_gamepad_read_joystick_read_iot_y, "1"]
- ]), "type")
- .appendField(Blockly.Msg.x_gamepad_read_joystick_read_after_ai);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(GAMEPAD_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- '0': Blockly.Msg.x_ai_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_x),
- '1': Blockly.Msg.x_ai_read_gamepad_joystick_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_read_iot_y)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['x_ai_read_gamepad_joystick'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['gamepad_joystick_def'] = '' +
- 'from machine import I2C\n' +
- 'from ads1x15 import ADS1115\n' +
- '\n' +
- '_adc_i2c = I2C(I2C.I2C0, freq=100000, scl=30, sda=29)\n' +
- '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = 'int(_joystick_adc.read(channel1=' + dropdown_type + ')/25.75)';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['x_ai_read_gamepad_joystick_check'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.x_gamepad_read_joystick_when_ai)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.x_gamepad_read_joystick_when_top_ai, "top"],
- [Blockly.Msg.x_gamepad_read_joystick_when_bottom_ai, "bottom"],
- [Blockly.Msg.x_gamepad_read_joystick_when_left_ai, "left"],
- [Blockly.Msg.x_gamepad_read_joystick_when_right_ai, "right"],
- [Blockly.Msg.x_gamepad_read_joystick_when_not_ai, "still"],
- ]), "type");
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(GAMEPAD_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'top': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_top_ai),
- 'bottom': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_bottom_ai),
- 'left': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_left_ai),
- 'right': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_right_ai),
- 'still': Blockly.Msg.x_ai_read_gamepad_joystick_check_TOOLTIP.replace('%1', Blockly.Msg.x_gamepad_read_joystick_when_not_ai)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['x_ai_read_gamepad_joystick_check'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- Blockly.Python.definitions_['gamepad_joystick_def'] = '' +
- 'from machine import I2C\n' +
- 'from ads1x15 import ADS1115\n' +
- '\n' +
- '_adc_i2c = I2C(I2C.I2C0, freq=100000, scl=30, sda=29)\n' +
- '_joystick_adc = ADS1115(_adc_i2c, 0x48, 1)\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- '\n';
- Blockly.Python.definitions_['gamepad_joystick_check_direction_def'] = '' +
- 'def _read_direction():\n' +
- ' 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' +
- ' return "still" \n' +
- ' elif int(_joystick_adc.read(channel1=1)/25.75) > 600:\n' +
- ' return "left"\n' +
- ' elif int(_joystick_adc.read(channel1=1)/25.75) < 400:\n' +
- ' return "right"\n' +
- ' elif int(_joystick_adc.read(channel1=0)/25.75) > 600:\n' +
- ' return "top"\n' +
- ' elif int(_joystick_adc.read(channel1=0)/25.75) < 400:\n' +
- ' return "bottom"\n' +
- '\n';
- // TODO: Assemble Python into code variable.
- var code = '_read_direction() == "' + dropdown_type + '"';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- /*
- __ ___ _____ _____
- /\ \ \/ __\ \_ \___/__ \
- / \/ /__\//_____ / /\/ _ \ / /\/
- / /\ / \/ \_____/\/ /_| (_) / /
- \_\ \/\_____/ \____/ \___/\/
- Blockly.Python.addVariable('', '', true);
- Blockly.Python.definitions_['definition'] = '';
- Blockly.Python.addSetup("setup", '');
-
- */
- var ESP32_NBIOT_COLOR = "#629978";
- Blockly.Blocks['esp32_nbiot_setup'] = {
- init: function () {
- let type = getLocalStorage("type")
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/nbiot_esp32_header.png", 110, 40, { alt: "*", flipRtl: "FALSE" }));
- type == 1 ? this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.nbiot_module_which_module_ai, "ai"],
- [Blockly.Msg.nbiot_module_which_module_iot, "iot"]
- ]), "module_type") : this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.nbiot_module_which_module_iot, "iot"],
- [Blockly.Msg.nbiot_module_which_module_ai, "ai"]
- ]), "module_type");
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_module_init);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_NBIOT_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('module_type');
- var TOOLTIPS = {
- 'ai': Blockly.Msg.esp32_nbiot_setup_TOOLTIP.replace('%1', Blockly.Msg.nbiot_module_which_module_ai),
- 'iot': Blockly.Msg.esp32_nbiot_setup_TOOLTIP.replace('%1', Blockly.Msg.nbiot_module_which_module_iot)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_nbiot_setup'] = function (block) {
- // TODO: Assemble Python into code variable.
- var dropdown_module_type = block.getFieldValue('module_type');
- if (dropdown_module_type == "iot") {
- Blockly.Python.addVariable('_nbiot_serial_read', '_nbiot_serial_read = ""', true);
- Blockly.Python.addVariable('_nbiot_readings', '_nbiot_readings = ""', true);
- Blockly.Python.addVariable('_nbiot_power_on_status', '_nbiot_power_on_status = False', true);
- Blockly.Python.addVariable('_nbiot_internet_status', '_nbiot_internet_status = False', true);
- Blockly.Python.addVariable('_nbiot_internet_ip_address', '_nbiot_internet_ip_address = ""', true);
- Blockly.Python.definitions_['import_definition'] = '' +
- 'from machine import UART\n' +
- 'from machine import Timer\n' +
- 'import ubinascii as binascii\n' +
- 'import time\n' +
- '\n' +
- '_nbiot_serial = UART(2,rx=16, tx=17, baudrate=115200, timeout=10)\n' +
- '\n' +
- '_nbiot_serial_timer = Timer(1)\n' +
- '_nbiot_serial_timer.init(period=50, mode=Timer.PERIODIC, callback=lambda t: _read_nbiot_serial(_nbiot_serial))\n' +
- '\n' +
- 'def _read_nbiot_serial(uart):\n' +
- ' global _nbiot_readings, _nbiot_power_on_status, _nbiot_internet_status, _nbiot_internet_ip_address\n' +
- ' if _nbiot_serial.any():\n' +
- ' _nbiot_serial_read = _nbiot_serial.readline().decode("utf-8")\n' +
- ' _nbiot_serial_read = _nbiot_serial_read.strip()\n' +
- ' _nbiot_readings = _nbiot_serial_read.strip()\n' +
- '\n' +
- ' if _nbiot_serial_read == "+CPIN: READY":\n' +
- ' _nbiot_power_on_status = True\n' +
- ' print("NB-IoT Module Initiated!\\n")\n' +
- ' _nbiot_serial.write(b\'AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n\')\n' +
- '\n' +
- ' if "+IP" in _nbiot_serial_read:\n' +
- ' _nbiot_internet_status = True\n' +
- ' _nbiot_internet_ip_address = _nbiot_serial_read[5:len(_nbiot_serial_read)]\n' +
- '';
- var code = '';
- } else if (dropdown_module_type == "ai") {
- Blockly.Python.addVariable('_nbiot_serial_read', '_nbiot_serial_read = ""', true);
- Blockly.Python.addVariable('_nbiot_readings', '_nbiot_readings = ""', true);
- Blockly.Python.addVariable('_nbiot_power_on_status', '_nbiot_power_on_status = False', true);
- Blockly.Python.addVariable('_nbiot_internet_status', '_nbiot_internet_status = False', true);
- Blockly.Python.addVariable('_nbiot_internet_ip_address', '_nbiot_internet_ip_address = ""', true);
- Blockly.Python.definitions_['import_definition'] = '' +
- 'import machine, time\n' +
- 'from fpioa_manager import fm\n' +
- 'from machine import Timer\n' +
- '\n' +
- 'fm.register(13,fm.fpioa.UART2_TX)\n' +
- 'fm.register(14,fm.fpioa.UART2_RX)\n' +
- '\n' +
- '_nbiot_serial = machine.UART(machine.UART.UART2,115200, 8, None, 1, timeout=50)\n' +
- '\n' +
- 'def _on_timer(timer):\n' +
- ' _read_nbiot_serial(_nbiot_serial)\n' +
- '\n' +
- '_periodic_tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=1, callback=_on_timer, arg=_on_timer)\n' +
- '_periodic_tim.start()\n' +
- '\n' +
- 'def _read_nbiot_serial(uart):\n' +
- ' global _nbiot_readings, _nbiot_power_on_status, _nbiot_internet_status, _nbiot_internet_ip_address\n' +
- ' if _nbiot_serial.any():\n' +
- ' _nbiot_serial_read = _nbiot_serial.readline().decode("utf-8")\n' +
- ' print(_nbiot_serial_read)\n' +
- ' _nbiot_serial_read = _nbiot_serial_read.strip()\n' +
- ' _nbiot_readings = _nbiot_serial_read.strip()\n' +
- '\n' +
- ' if _nbiot_serial_read == "+CPIN: READY":\n' +
- ' _nbiot_power_on_status = True\n' +
- ' print("NB-IoT Module Initiated!\\n")\n' +
- ' _nbiot_serial.write(b\'AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n\')\n' +
- '\n' +
- ' if "+IP" in _nbiot_serial_read:\n' +
- ' _nbiot_internet_status = True\n' +
- ' _nbiot_internet_ip_address = _nbiot_serial_read[5:len(_nbiot_serial_read)]\n' +
- ' print("NB-IoT is Online! Public IP is: " + str(_nbiot_internet_ip_address))\n' +
- '\n' +
- '_nbiot_serial_read = ""\n' +
- '_nbiot_readings = ""\n' +
- '_nbiot_power_on_status = False\n' +
- '_nbiot_internet_status = False\n' +
- '_nbiot_internet_ip_address = ""\n' +
- '';
- var code = '';
- }
- return code;
- };
- Blockly.Blocks['esp32_nbiot_get_power_status'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_module_power);
- this.setOutput(true, null);
- this.setColour(ESP32_NBIOT_COLOR);
- this.setTooltip(Blockly.Msg.esp32_nbiot_get_power_status_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_nbiot_get_power_status'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_nbiot_power_on_status';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_nbiot_get_network_status'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_module_connectivity);
- this.setOutput(true, null);
- this.setColour(ESP32_NBIOT_COLOR);
- this.setTooltip(Blockly.Msg.esp32_nbiot_get_network_status_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_nbiot_get_network_status'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_nbiot_internet_status';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_nbiot_get_network_ip'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_module_ip);
- this.setOutput(true, null);
- this.setColour(ESP32_NBIOT_COLOR);
- this.setTooltip(Blockly.Msg.esp32_nbiot_get_network_ip_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_nbiot_get_network_ip'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_nbiot_internet_ip_address';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['esp32_nbiot_onenet_init'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/nbiot_onenet.png", 200, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_onenet_title);
- this.appendValueInput("product_id")
- .setCheck(null)
- .appendField(Blockly.Msg.nbiot_onenet_connect_productid);
- this.appendValueInput("device_id")
- .setCheck(null)
- .appendField(Blockly.Msg.nbiot_onenet_connect_deviceid);
- this.appendValueInput("device_apikey")
- .setCheck(null)
- .appendField(Blockly.Msg.nbiot_onenet_connect_device_api);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_nbiot_onenet_init_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_nbiot_onenet_init'] = function (block) {
- var value_product_id = Blockly.Python.valueToCode(block, 'product_id', Blockly.Python.ORDER_ATOMIC);
- var value_device_id = Blockly.Python.valueToCode(block, 'device_id', Blockly.Python.ORDER_ATOMIC);
- var value_device_apikey = Blockly.Python.valueToCode(block, 'device_apikey', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- Blockly.Python.addVariable('_nbiot_onenet_product_id', '', true);
- Blockly.Python.addVariable('_nbiot_onenet_device_id', '', true);
- Blockly.Python.addVariable('_nbiot_onent_device_apikey', '', true);
- Blockly.Python.addVariable('_nbiot_onenet_connection_status', '', true);
- Blockly.Python.addVariable('_nbiot_onenet_read_anything', '', true);
- Blockly.Python.addVariable('_nbiot_onenet_init_state', '', true);
- Blockly.Python.addSetup("setup", '' +
- '_nbiot_onenet_init_state = False\n' +
- '_nbiot_onenet_connection_status = False\n' +
- '_nbiot_onenet_read_anything = False\n' +
- '_nbiot_onenet_product_id = ' + value_product_id + '\n' +
- '_nbiot_onenet_device_id = ' + value_device_id + '\n' +
- '_nbiot_onent_device_apikey = ' + value_device_apikey + '\n' +
- '\n' +
- 'def _nbiot_send_onenet_data(data):\n' +
- ' _nbiot_send_message = data\n' +
- ' _nbiot_send_data_content = binascii.hexlify(_nbiot_send_message).decode("utf-8")\n' +
- ' _nbiot_send_header = ",0300" + str(hex(len(_nbiot_send_message)).split(\'x\')[-1])\n' +
- ' _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' +
- ' return _nbiot_send_at_content\n' +
- '\n' +
- 'def _nbiot_read_onenet_data(raw_response):\n' +
- ' _nbiot_receive = raw_response\n' +
- ' if "+MQTTPUBLISH" not in raw_response:\n' +
- ' return ""\n' +
- ' elif "+MQTTPUBLISH" in raw_response:\n' +
- ' _nbiot_occur = -1\n' +
- ' for i in range(0, 6): _nbiot_occur = _nbiot_receive.find(",", _nbiot_occur + 1)\n' +
- ' return _nbiot_receive[_nbiot_occur+1:len(_nbiot_receive)]\n' +
- '\n' +
- 'def _read_nbiot_onenet(_incoming_data):\n' +
- ' global _nbiot_onenet_connection_status, _nbiot_onenet_read_anything\n' +
- '\n' +
- ' if "+MQTTOPEN: OK" in _incoming_data:\n' +
- ' _nbiot_onenet_connection_status = True\n' +
- ' # print("OneNET Connected!")\n' +
- ' if "+MQTTOPEN: FAIL" in _incoming_data:\n' +
- ' _nbiot_onenet_connection_status = False\n' +
- ' # print("OneNET Lost Connection!")\n' +
- '\n' +
- ' if "+MQTTPUBLISH" in _incoming_data:\n' +
- ' _nbiot_onenet_read_anything = True\n' +
- ' else:\n' +
- ' _nbiot_onenet_read_anything = False\n' +
- '\n' +
- ' if "+MQTTDISC" in _incoming_data:\n' +
- ' _nbiot_onenet_read_anything = False\n' +
- ' _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' +
- ' print("Enabling OneNET Connection...")\n' +
- ' _nbiot_serial.write(_nbiot_onenet_configuration.encode())\n' +
- ' time.sleep_ms(100)\n' +
- ' _nbiot_serial.write(b\'AT+MQTTOPEN=1,1,0\\r\\n\')\n' +
- '\n' +
- 'def _init_onenet_connection():\n' +
- ' _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' +
- ' print("Enabling OneNET Connection...")\n' +
- ' _nbiot_serial.write(_nbiot_onenet_configuration.encode())\n' +
- ' time.sleep_ms(100)\n' +
- ' _nbiot_serial.write(b\'AT+MQTTOPEN=1,1,0\\r\\n\')\n' +
- ' time.sleep_ms(100)\n' +
- '');
- var code = '' +
- 'if (_nbiot_internet_status == True) and (_nbiot_onenet_init_state == False) and (_nbiot_onenet_connection_status == False):\n' +
- ' _init_onenet_connection()\n' +
- ' _nbiot_onenet_init_state = True\n' +
- '\n' +
- '_read_nbiot_onenet(_nbiot_readings)\n' +
- '';
- return code;
- };
- Blockly.Blocks['esp32_nbiot_onenet_connection_status'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_onenet_connectivity);
- this.setOutput(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_nbiot_onenet_connection_status_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_nbiot_onenet_connection_status'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_nbiot_onenet_connection_status';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['iot_service_nbiot_onenet'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/nbiot_onenet_send.png", 210, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_onenet_send_title);
- /*this.appendValueInput("first_input")
- .setCheck(null)
- .appendField("Field 0");*/
- this.itemCount_ = 1;
- this.updateShape_();
- this.setMutator(new Blockly.Mutator(['iot_service_nbiot_onenet_create_with_item']));
- this.setColour(ONENET_BLOCK_COLOR);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setTooltip(Blockly.Msg.iot_service_nbiot_onenet_TOOLTIP);
- this.setHelpUrl("");
- },
- mutationToDom: function () {
- var container = document.createElement('mutation');
- container.setAttribute('items', this.itemCount_);
- return container;
- },
- domToMutation: function (xmlElement) {
- this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
- this.updateShape_();
- },
- decompose: function (workspace) {
- var containerBlock = workspace.newBlock('iot_service_nbiot_onenet_create_with_container');
- containerBlock.initSvg();
- var connection = containerBlock.getInput('STACK').connection;
- for (var i = 0; i < this.itemCount_; i++) {
- var itemBlock = workspace.newBlock('iot_service_nbiot_onenet_create_with_item');
- itemBlock.initSvg();
- connection.connect(itemBlock.previousConnection);
- connection = itemBlock.nextConnection;
- }
- return containerBlock;
- },
- compose: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- // Count number of inputs.
- var connections = [];
- while (itemBlock) {
- connections.push(itemBlock.valueConnection_);
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- // Disconnect any children that don't belong.
- for (var i = 0; i < this.itemCount_; i++) {
- var connection = this.getInput('ADD' + i).connection.targetConnection;
- if (connection && connections.indexOf(connection) == -1) {
- connection.disconnect();
- }
- }
- this.itemCount_ = connections.length;
- this.updateShape_();
- // Reconnect any child blocks.
- for (var i = 0; i < this.itemCount_; i++) {
- Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
- }
- },
- saveConnections: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- var i = 0;
- while (itemBlock) {
- var input = this.getInput('ADD' + i);
- itemBlock.valueConnection_ = input && input.connection.targetConnection;
- i++;
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- },
- updateShape_: function () {
- for (var i = 0; i < this.itemCount_; i++) {
- if (!this.getInput('ADD' + i)) {
- var input = this.appendValueInput('ADD' + i);
- input.appendField(Blockly.Msg.nbiot_onenet_send_property)
- .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
- //input.appendField("資料 " + (i + 1) + ":");
- //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
- // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
- }
- }
- while (this.getInput('ADD' + i)) {
- this.removeInput('ADD' + i);
- i++;
- }
- },
- };
- Blockly.Blocks['iot_service_nbiot_onenet_create_with_container'] = {
- init: function () {
- this.setColour(ONENET_BLOCK_COLOR);
- this.appendDummyInput()
- .appendField("Items");
- this.appendStatementInput('STACK');
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['iot_service_nbiot_onenet_create_with_item'] = {
- init: function () {
- this.setColour(ONENET_BLOCK_COLOR);
- this.appendDummyInput()
- .appendField("Field");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Python['iot_service_nbiot_onenet'] = function (block) {
- // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
- var item_field = '',
- item_value = '';
- var nbiot_onenet_data = "'{";
- for (var n = 0; n < this.itemCount_; n++) {
- item_field = block.getFieldValue("field" + n);
- item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
- nbiot_onenet_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
- }
- var nbiot_onenet_data_final = nbiot_onenet_data + "}'";
- var code = '' +
- '_data_to_be_send = ' + nbiot_onenet_data_final.slice(0, -3) + '}\\r\\n\'' + '\n' +
- '_data_add_rn = _nbiot_send_onenet_data(_data_to_be_send) + "\\r\\n"\n' +
- '_data_for_at = _data_add_rn.encode()\n' +
- '_nbiot_serial.write(_data_for_at)\n' +
- // '_nbiot_serial.write(_nbiot_send_onenet_data(_data_to_be_send).encode())\n' +
- '\n' +
- '';
- return code;
- };
- Blockly.Blocks['iot_service_nbiot_onenet_read'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg);
- this.appendValueInput("message")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg_when);
- this.appendStatementInput("exec")
- .setCheck(null)
- .appendField(Blockly.Msg.nbiot_onenet_when_incoming_msg_exec);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.iot_service_nbiot_onenet_read_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_nbiot_onenet_read'] = function (block) {
- var value_message = Blockly.Python.valueToCode(block, 'message', Blockly.Python.ORDER_ATOMIC);
- var statements_exec = Blockly.Python.statementToCode(block, 'exec');
- // TODO: Assemble Python into code variable.
- var code = 'if (_nbiot_read_onenet_data(_nbiot_readings) == ' + value_message + '):\n' +
- statements_exec +
- '\n';
- return code;
- };
- Blockly.Blocks['esp32_nbiot_onenet_read_value'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_onenet_receive_command);
- this.setOutput(true, null);
- this.setColour(ONENET_BLOCK_COLOR);
- this.setTooltip(Blockly.Msg.esp32_nbiot_onenet_read_value_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['esp32_nbiot_onenet_read_value'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_nbiot_read_onenet_data(_nbiot_readings)';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['iot_service_nbiot_cococloud_read'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/nbiot_cococloud_get.png", 260, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_cococloud_get_event_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_cococloud_get_event_api)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "t_api");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_nbiot_cococloud_read'] = function (block) {
- var api = block.getFieldValue('t_api');
- // TODO: Assemble Python into code variable.
- var code = '\n';
- return code;
- };
- Blockly.Blocks['iot_service_nbiot_cococloud_read_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_cococloud_get_property);
- this.appendValueInput("property")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_cococloud_get_data);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['iot_service_nbiot_cococloud_read_data'] = function (block) {
- var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '\n';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['iot_service_nbiot_cococloud'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/nbiot_cococloud_send.png", 260, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_cococloud_send_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.nbiot_cococloud_send_api)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "t_api");
- /*this.appendValueInput("first_input")
- .setCheck(null)
- .appendField("Field 0");*/
- this.itemCount_ = 1;
- this.updateShape_();
- this.setMutator(new Blockly.Mutator(['iot_service_nbiot_cococloud_create_with_item']));
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setTooltip(Blockly.Msg.iot_service_nbiot_cococloud_TOOLTIP);
- this.setHelpUrl("");
- },
- mutationToDom: function () {
- var container = document.createElement('mutation');
- container.setAttribute('items', this.itemCount_);
- return container;
- },
- domToMutation: function (xmlElement) {
- this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
- this.updateShape_();
- },
- decompose: function (workspace) {
- var containerBlock = workspace.newBlock('iot_service_nbiot_cococloud_create_with_container');
- containerBlock.initSvg();
- var connection = containerBlock.getInput('STACK').connection;
- for (var i = 0; i < this.itemCount_; i++) {
- var itemBlock = workspace.newBlock('iot_service_nbiot_cococloud_create_with_item');
- itemBlock.initSvg();
- connection.connect(itemBlock.previousConnection);
- connection = itemBlock.nextConnection;
- }
- return containerBlock;
- },
- compose: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- // Count number of inputs.
- var connections = [];
- while (itemBlock) {
- connections.push(itemBlock.valueConnection_);
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- // Disconnect any children that don't belong.
- for (var i = 0; i < this.itemCount_; i++) {
- var connection = this.getInput('ADD' + i).connection.targetConnection;
- if (connection && connections.indexOf(connection) == -1) {
- connection.disconnect();
- }
- }
- this.itemCount_ = connections.length;
- this.updateShape_();
- // Reconnect any child blocks.
- for (var i = 0; i < this.itemCount_; i++) {
- Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
- }
- },
- saveConnections: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- var i = 0;
- while (itemBlock) {
- var input = this.getInput('ADD' + i);
- itemBlock.valueConnection_ = input && input.connection.targetConnection;
- i++;
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- },
- updateShape_: function () {
- for (var i = 0; i < this.itemCount_; i++) {
- if (!this.getInput('ADD' + i)) {
- var input = this.appendValueInput('ADD' + i);
- input.appendField(Blockly.Msg.nbiot_cococloud_send_property)
- .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
- //input.appendField("資料 " + (i + 1) + ":");
- //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
- // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
- }
- }
- while (this.getInput('ADD' + i)) {
- this.removeInput('ADD' + i);
- i++;
- }
- },
- };
- Blockly.Blocks['iot_service_nbiot_cococloud_create_with_container'] = {
- init: function () {
- this.setColour(ESP32_NBIOT_COLOR);
- this.appendDummyInput()
- .appendField("Items");
- this.appendStatementInput('STACK');
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['iot_service_nbiot_cococloud_create_with_item'] = {
- init: function () {
- this.setColour(COCOCLOUD_BLOCK_COLOR);
- this.appendDummyInput()
- .appendField("Field");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Python['iot_service_nbiot_cococloud'] = function (block) {
- var api = block.getFieldValue('t_api');
- Blockly.Python.addVariable('_nbiot_cococloud_send_init_state', '', true);
- Blockly.Python.addVariable('_nbiot_cococloud_json_content', '', true);
- Blockly.Python.definitions_['import'] = '' +
- '_nbiot_cococloud_send_init_state = False\n' +
- '\n' +
- 'def _nbiot_cococloud_send(data):\n' +
- ' _nbiot_cococloud_send_data = \'AT+HTTPCONTENT=0,\"\'+ data + \'\"\\r\\n\'\n' +
- ' print(_nbiot_cococloud_send_data)\n' +
- ' _nbiot_serial.write(b\'AT+HTTPCLOSE=0\\r\\n\')\n' +
- ' time.sleep_ms(500)\n' +
- ' _nbiot_serial.write(b\'AT+HTTPCREATE=\"http://api.cocorobo.hk/\"\\r\\n\')\n' +
- ' time.sleep_ms(500)\n' +
- ' _nbiot_serial.write(b\'AT+HTTPHEADER=0,\"Content-Type: application/json\\\\r\\\\n\"\\r\\n\')\n' +
- ' time.sleep_ms(500)\n' +
- ' _nbiot_serial.write(_nbiot_cococloud_send_data.encode())\n' +
- ' time.sleep_ms(500)\n' +
- ' _nbiot_serial.write(b\'AT+HTTPSEND=0,1,\"/iot/data/eventAPIKey/' + api + '\"\\r\\n\')\n' +
- ' time.sleep_ms(500)\n' +
- '';
- var item_field = '',
- item_value = '';
- var nbiot_cococloud_data = "'{";
- for (var n = 0; n < this.itemCount_; n++) {
- item_field = block.getFieldValue("field" + n);
- item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
- nbiot_cococloud_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
- }
- var nbiot_cococloud_data_final = nbiot_cococloud_data + "}'";
- var code = '' +
- '_nbiot_cococloud_json_content = ' + nbiot_cococloud_data_final.slice(0, -3) + '}\'' + '\n' +
- '_nbiot_cococloud_send(_nbiot_cococloud_json_content)\n' +
- '';
- return code;
- };
- Blockly.Blocks['3rd_party_sensor_hcsr04'] = {
- init: function () {
- let types = getLocalStorage("type")
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/ultrasonic_setup_header.png", 120, 40, { alt: "*", flipRtl: "FALSE" }));
- types == 1 ? this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.third_party_hcsr04_ai, "ai"],
- [Blockly.Msg.third_party_hcsr04_iot, "iot"]
- ]), "type")
- .appendField(Blockly.Msg.third_party_hcsr04_setup_title)
- .appendField(new Blockly.FieldDropdown([
- ["1", "1"],
- ["2", "2"],
- ["3", "3"]
- ]), "index") : this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.third_party_hcsr04_iot, "iot"],
- [Blockly.Msg.third_party_hcsr04_ai, "ai"]
- ]), "type")
- .appendField(Blockly.Msg.third_party_hcsr04_setup_title)
- .appendField(new Blockly.FieldDropdown([
- ["1", "1"],
- ["2", "2"],
- ["3", "3"]
- ]), "index");
- types == 1 ? this.appendDummyInput()
- .appendField(Blockly.Msg.third_party_hcsr04_trig_pin)
- .appendField(new Blockly.FieldNumber(2, 0, 40, 1), "trig")
- .appendField(Blockly.Msg.third_party_hcsr04_echo_pin)
- .appendField(new Blockly.FieldNumber(3, 0, 40, 1), "echo") :
- this.appendDummyInput()
- .appendField(Blockly.Msg.third_party_hcsr04_trig_pin)
- .appendField(new Blockly.FieldNumber(12, 0, 40, 1), "trig")
- .appendField(Blockly.Msg.third_party_hcsr04_echo_pin)
- .appendField(new Blockly.FieldNumber(13, 0, 40, 1), "echo");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d72bd5");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'ai': Blockly.Msg.Trd_Party_Sensor_Hcsr04_TOOLTIP.replace('%1', Blockly.Msg.third_party_hcsr04_ai),
- 'iot': Blockly.Msg.Trd_Party_Sensor_Hcsr04_TOOLTIP.replace('%1', Blockly.Msg.third_party_hcsr04_iot)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['3rd_party_sensor_hcsr04'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- var dropdown_index = block.getFieldValue('index');
- var number_trig = block.getFieldValue('trig');
- var number_echo = block.getFieldValue('echo');
- // TODO: Assemble Python into code variable.
- if (dropdown_type == "iot") {
- Blockly.Python.definitions_['import_hcsr04_def_' + number_trig + number_echo] = '' +
- 'from HCSR04 import HCSR04\n' +
- 'from machine import Pin\n' +
- 'import time\n' +
- '\n' +
- '_ultrasonic_sensor_trig_' + number_trig + ' = Pin(' + number_trig + ',Pin.OUT)\n' +
- '_ultrasonic_sensor_echo_' + number_echo + ' = Pin(' + number_echo + ',Pin.IN)\n' +
- '_ultrasonic_sensor_read_' + dropdown_index + ' = HCSR04(_ultrasonic_sensor_trig_' + number_trig + ',_ultrasonic_sensor_echo_' + number_echo + ')\n' +
- '\n';
- var code = '';
- } else if (dropdown_type == "ai") {
- Blockly.Python.definitions_['import_hcsr04_def_' + number_trig + number_echo] = '' +
- 'from HCSR04 import HCSR04\n' +
- 'from fpioa_manager import *\n' +
- 'from Maix import GPIO\n' +
- 'import time\n' +
- '\n' +
- 'fm.register(' + number_trig + ',fm.fpioa.GPIOHS' + number_trig + ')\n' +
- 'fm.register(' + number_echo + ',fm.fpioa.GPIOHS' + number_echo + ')\n' +
- '\n' +
- '_ultrasonic_sensor_trig_' + number_trig + ' = GPIO(GPIO.GPIOHS' + number_trig + ', GPIO.OUT)\n' +
- '_ultrasonic_sensor_echo_' + number_echo + ' = GPIO(GPIO.GPIOHS' + number_echo + ', GPIO.IN)\n' +
- '_ultrasonic_sensor_read_' + dropdown_index + ' = HCSR04(_ultrasonic_sensor_trig_' + number_trig + ',_ultrasonic_sensor_echo_' + number_echo + ')\n' +
- '\n';
- var code = '';
- }
- return code;
- };
- Blockly.Blocks['3rd_party_sensor_hcsr04_read'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.third_party_hcsr04_read_text)
- .appendField(new Blockly.FieldDropdown([
- ["1", "1"],
- ["2", "2"],
- ["3", "3"]
- ]), "index")
- .appendField(Blockly.Msg.third_party_hcsr04_read_text_after);
- this.setOutput(true, null);
- this.setColour("#d72bd5");
- this.setTooltip(Blockly.Msg.Trd_Party_Sensor_Hcsr04_Read_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['3rd_party_sensor_hcsr04_read'] = function (block) {
- var dropdown_index = block.getFieldValue('index');
- // TODO: Assemble Python into code variable.
- var code = '_ultrasonic_sensor_read_' + dropdown_index + '.getDistance()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['Pedometer_Init'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.Pedometer_Init);
- this.appendValueInput("time")
- .setCheck(null)
- .appendField(Blockly.Msg.Pedometer_Init_time);
- this.appendValueInput("sensitivity")
- .setCheck(null)
- .appendField(Blockly.Msg.Pedometer_Init_sensitivity);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Pedometer_Init_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['Pedometer_Init'] = function (block) {
- var time = Blockly.Python.valueToCode(block, 'time', Blockly.Python.ORDER_ATOMIC);
- var sensitivity = Blockly.Python.valueToCode(block, 'sensitivity', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['Pedometer_Init'] = `from machine import I2C, Pin
- import math
- import QMI8658
- mpu_lib_version_is_latest = "QMI8658" in dir(QMI8658)
- if mpu_lib_version_is_latest == True:
- mpui2c = I2C(scl=Pin(22), sda=Pin(21))
- QMI8658 = QMI8658.QMI8658(mpui2c)
- mpucal = QMI8658.calibrate()
- elif mpu_lib_version_is_latest == False:
- #申明QMI8658传感器IIC通信引脚
- _SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))
- _MPU = QMI8658.accel(_SENSOR_MPU_PINS)
- _MPU_SHAKING_OFFSET = _MPU.get_values()['AcZ']
- _MPU_ACCL_X = _MPU.get_values()['AcX']
- _MPU_ACCL_Y = _MPU.get_values()['AcY']
- _MPU_ACCL_Z = _MPU.get_values()['AcZ']
- _MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE('X')
- _MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE('Y')
- _MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE('Z')
- threshold_time=`+ time + ` #时间筛选阈值
- threshold_hill=`+ sensitivity + ` #波峰波谷差值阈值
- xval=[None]*100 #初始X轴加速度原始数据采集列表
- yval=[None]*100 #初始Y轴加速度原始数据采集列表
- zval=[None]*100 #初始Z轴加速度原始数据采集列表
- xavg=0 #X轴方向初始加速度平均值
- yavg=0 #Y轴方向初始加速度平均值
- zavg=0 #Z轴方向初始加速度平均值
- xaccl=[None]*100 #X轴加速度实时采集列表
- yaccl=[None]*100 #Y轴加速度实时采集列表
- zaccl=[None]*100 #Z轴加速度实时采集列表
- totvect=[0.0]*100 #三个方向总加速度矢量值
- totave=[0.0]*100 #最大和最小加速度矢量值平均值
- peak=0 #波峰数据
- trough=0 #波谷数据
- data=[0]*15 #实时采集数据滚动递进列表
- steps=0 #行走步伐数
- flag0=0 #步数提取标志位
- flag1=0 #波峰提取标志位
- flag2=0 #波谷提取标志位
- hill=0 #波峰波谷差值
- starttime=0 #波峰时间点
- endtime=0 #波谷时间点
- lasttime=0 #波峰波谷间隔时间
- #获取三个方向加速度函数
- def QMI8658_get_accel(type):
- _MPU = QMI8658.accel(_SENSOR_MPU_PINS)
- _MPU_SHAKING_OFFSET = _MPU.get_values()['AcZ']
- _MPU_ACCL_X = _MPU.get_values()['AcX']
- _MPU_ACCL_Y = _MPU.get_values()['AcY']
- _MPU_ACCL_Z = _MPU.get_values()['AcZ']
- _MPU_OFFSET_TILT_X = _MPU_GET_TILT_ANGLE('X')
- _MPU_OFFSET_TILT_Y = _MPU_GET_TILT_ANGLE('Y')
- _MPU_OFFSET_TILT_Z = _MPU_GET_TILT_ANGLE('Z')
- if type == 'X':
- return _MPU_ACCL_X
- if type == 'Y':
- return _MPU_ACCL_Y
- if type == 'Z':
- return _MPU_ACCL_Z
- #初始化校准函数
- def calibration():
- sum0=0 #x轴加速度累计值
- sum1=0 #y轴加速度累计值
- sum2=0 #z轴加速度累计值
- for i in range(100):
- xval[i]= QMI8658.read(mpucal)['AcX']-345 if mpu_lib_version_is_latest == True else QMI8658_get_accel('X')-345
- sum0=xval[i]+sum0
- time.sleep_ms(100)
- xavg=sum0/100
- #print(xavg)
- for i in range(100):
- yval[i]= QMI8658.read(mpucal)['AcY']-346 if mpu_lib_version_is_latest == True else QMI8658_get_accel('Y')-346
- sum1=yval[i]+sum1
- time.sleep_ms(100)
- yavg=sum1/100
- #print(yavg)
- for i in range(100):
- zval[i]= QMI8658.read(mpucal)['AcZ']-416 if mpu_lib_version_is_latest == True else QMI8658_get_accel('Z')-416
- sum2=zval[i]+sum2
- time.sleep_ms(100)
- zavg=sum2/100
- #print(zavg)
- def getPedometer():
- global q, xaccl, yaccl, zaccl, totvect, totave, data, peak, flag0, flag1, flag2, starttime, lasttime, endtime, hill, trough, threshold_hill, hill, threshold_time, steps
- for a in range(100):
- xaccl[a]= QMI8658.read(mpucal)['AcX']-345 if mpu_lib_version_is_latest == True else QMI8658_get_accel('X')-345
- time.sleep_ms(1)
- yaccl[a]= QMI8658.read(mpucal)['AcY']-346 if mpu_lib_version_is_latest == True else QMI8658_get_accel('Y')-346
- time.sleep_ms(1)
- zaccl[a]= QMI8658.read(mpucal)['AcZ']-416 if mpu_lib_version_is_latest == True else QMI8658_get_accel('Z')-416
- time.sleep_ms(1)
- #计算X、Y、Z轴三个方向加速度值的平方根来计算总加速度矢量
- totvect[a]=math.sqrt(((xaccl[a]-xavg)*(xaccl[a]-xavg))+((yaccl[a]-yavg)*(yaccl[a]-yavg))+((zaccl[a]-zavg)*(zaccl[a]-zavg)))
- totave[a]=(totvect[a]+totave[a-1])/2
- q=totave[a]
- #数据轮替
- data[2]=data[1]
- data[1]=data[0]
- data[0]=totave[a]
- #检测是否到达一个波峰,如果是,将波峰数据采集标志位置1,并开始计时
- if ((data[1]>data[0])&(data[1]>data[2])&(flag1==0)):
- peak=data[1]
- flag1=1
- starttime=time.ticks_ms()
- #检测是否到达一个波谷,如果是,将波谷数据采集标志位置1,并读取当前时间
- if ((data[1]<data[0])&(data[1]<data[2])&(flag2==0)):
- trough=data[1]
- flag2=1
- endtime=time.ticks_ms()
- #如果采集到一组波峰波谷数据
- if((flag1==1)&(flag2==1)):
- flag1=0 #将波峰数据采集标志位置0
- flag2=0 #将波谷数据采集标指位置1
- hill=peak-trough #计算波峰波谷差值
- lasttime=endtime-starttime #计算波峰波谷间隔时间
- if(hill>threshold_hill)&(lasttime>threshold_time): #如果波峰波谷差值大于threshold_hill且间隔时间大于threshold_time
- steps=steps+1 #步数加一
- flag0=1 #将步数数据提取标志位置1
- lasttime=0 #将波峰波谷间隔时间清零
- hill=0 #将波峰波谷差值清零
- time.sleep_ms(20)
- return steps
- `;
- var code = "";
- return code;
- };
- Blockly.Blocks['Pedometer_Run'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.Pedometer_Run);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Pedometer_Run_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['Pedometer_Run'] = function (block) {
- var code = `
- calibration()
- `;
- return code;
- };
- Blockly.Blocks['Pedometer_Get'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.Pedometer_Get);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#d42b03");
- this.setTooltip(Blockly.Msg.Pedometer_Get_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['Pedometer_Get'] = function (block) {
- var code = `getPedometer()`;
- return [code, Blockly.Python.ORDER_NONE];
- };
- /* mlx90614 */
- Blockly.Blocks['mlx90614_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/body_temp_mlx.png", 220, 60, { alt: "*", flipRtl: "FALSE" }));
- let types = getLocalStorage("type")
- types == 1 ? this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_ai_module, "2"],
- [Blockly.Msg.time_iot_module, "1"]
- ]), "digital_get_type")
- .appendField(Blockly.Msg.MLX90614_SETUP_TEXT_1) :
- this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_iot_module, "1"],
- [Blockly.Msg.time_ai_module, "2"]
- ]), "digital_get_type")
- .appendField(Blockly.Msg.MLX90614_SETUP_TEXT_1);
- this.appendDummyInput()
- .appendField(Blockly.Msg.MLX90614_SETUP_TEXT_2);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setInputsInline(false);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('digital_get_type');
- var TOOLTIPS = {
- '2': Blockly.Msg.MLX90614_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module),
- '1': Blockly.Msg.MLX90614_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['mlx90614_setup'] = function (block) {
- var dropdown_type = block.getFieldValue('digital_get_type');
- if (dropdown_type == "1") {
- Blockly.Python.definitions_['import_iot_mlx90614_setup'] = '' +
- 'import mlx90614\n' +
- 'from machine import I2C, Pin\n' +
- 'import time\n' +
- '\n' +
- 'def mlx90614_init():\n' +
- ' global sensor_mlx90614\n' +
- ' i2c = I2C(scl=Pin(22), sda=Pin(21),freq=100000)\n' +
- ' while True:\n' +
- ' init_success = False\n' +
- ' try:\n' +
- ' sensor_mlx90614 = mlx90614.MLX90614(i2c)\n' +
- ' init_success = True\n' +
- ' except BaseException as e:\n' +
- ' print(e)\n' +
- ' if init_success == True:\n' +
- ' break\n' +
- '\n' +
- 'def mlx90614_read(datatype):\n' +
- ' read_from_mlx90614 = None\n' +
- ' try:\n' +
- ' if datatype == "read_ambient_tempC":\n' +
- ' read_from_mlx90614 = sensor_mlx90614.read_ambient_tempC()\n' +
- ' elif datatype == "read_ambient_tempF":\n' +
- ' read_from_mlx90614 = sensor_mlx90614.read_ambient_tempF()\n' +
- ' elif datatype == "read_object_tempC":\n' +
- ' read_from_mlx90614 = sensor_mlx90614.read_object_tempC()\n' +
- ' elif datatype == "read_object_tempF":\n' +
- ' read_from_mlx90614 = sensor_mlx90614.read_object_tempF()\n' +
- ' except BaseException as e:\n' +
- ' read_from_mlx90614 = None\n' +
- ' print(e)\n' +
- ' read_from_mlx90614 = round(read_from_mlx90614, 2)\n' +
- ' # print(type(read_from_mlx90614))\n' +
- ' return read_from_mlx90614\n' +
- '\n';
- var code = 'mlx90614_init()\n';
- } else if (dropdown_type == "2") {
- Blockly.Python.definitions_['import_ai_mlx90614_setup'] = '' +
- 'import mlx90614\n' +
- 'from machine import I2C\n' +
- 'import time\n' +
- '\n' +
- 'def mlx90614_init():\n' +
- ' global sensor_mlx90614\n' +
- ' i2c = I2C(I2C.I2C0, freq=100000, scl=30, sda=29)\n' +
- ' while True:\n' +
- ' init_success = False\n' +
- ' try:\n' +
- ' sensor_mlx90614 = mlx90614.MLX90614(i2c)\n' +
- ' init_success = True\n' +
- ' except BaseException as e:\n' +
- ' print(e)\n' +
- ' if init_success == True:\n' +
- ' break\n' +
- '\n' +
- 'def mlx90614_read(datatype):\n' +
- ' read_from_mlx90614 = None\n' +
- ' try:\n' +
- ' if datatype == "read_ambient_tempC":\n' +
- ' read_from_mlx90614 = sensor_mlx90614.read_ambient_tempC()\n' +
- ' elif datatype == "read_ambient_tempF":\n' +
- ' read_from_mlx90614 = sensor_mlx90614.read_ambient_tempF()\n' +
- ' elif datatype == "read_object_tempC":\n' +
- ' read_from_mlx90614 = sensor_mlx90614.read_object_tempC()\n' +
- ' elif datatype == "read_object_tempF":\n' +
- ' read_from_mlx90614 = sensor_mlx90614.read_object_tempF()\n' +
- ' except BaseException as e:\n' +
- ' read_from_mlx90614 = None\n' +
- ' print(e)\n' +
- ' read_from_mlx90614 = round(read_from_mlx90614, 2)\n' +
- ' # print(type(read_from_mlx90614))\n' +
- ' return read_from_mlx90614\n' +
- '\n';
- var code = 'mlx90614_init()\n';
- }
- return code;
- };
- Blockly.Blocks['mlx90614_read_ambient'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.MLX90614_READ_AMBIENT);
- this.appendDummyInput()
- .appendField(Blockly.Msg.MLX90614_READ_SET_TEMP)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.MLX90614_READ_SET_TEMP_C, "C"],
- [Blockly.Msg.MLX90614_READ_SET_TEMP_F, "F"]
- ]), "type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#5c42bd");
- this.setTooltip(Blockly.Msg.MLX90614_READ_AMBIENT_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['mlx90614_read_ambient'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- if (dropdown_type == "C") {
- var code = `mlx90614_read("read_ambient_tempC")`;
- } else if (dropdown_type == "F") {
- var code = `mlx90614_read("read_ambient_tempF")`;
- }
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['mlx90614_read_object'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.MLX90614_READ_OBJECT);
- this.appendDummyInput()
- .appendField(Blockly.Msg.MLX90614_READ_SET_TEMP)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.MLX90614_READ_SET_TEMP_C, "C"],
- [Blockly.Msg.MLX90614_READ_SET_TEMP_F, "F"]
- ]), "type");
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#5c42bd");
- this.setTooltip(Blockly.Msg.MLX90614_READ_OBJECT_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['mlx90614_read_object'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- if (dropdown_type == "C") {
- var code = `mlx90614_read("read_object_tempC")`;
- } else if (dropdown_type == "F") {
- var code = `mlx90614_read("read_object_tempF")`;
- }
- return [code, Blockly.Python.ORDER_NONE];
- };
- /* DHT11 or DHT22 */
- Blockly.Blocks['DHT_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/dht_header.png", 60, 60, { alt: "*", flipRtl: "FALSE" }));
- let types = getLocalStorage("type")
- types == 1 ? this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_ai_module, "2"],
- [Blockly.Msg.time_iot_module, "1"]
- ]), "digital_get_type")
- .appendField(Blockly.Msg.DHT_SETUP_TEXT_1) :
- this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_iot_module, "1"],
- [Blockly.Msg.time_ai_module, "2"]
- ]), "digital_get_type")
- .appendField(Blockly.Msg.DHT_SETUP_TEXT_1);
- this.appendDummyInput()
- .appendField(Blockly.Msg.DHT_SETUP_TEXT_2)
- .appendField(new Blockly.FieldDropdown([
- ["DHT11", "1"],
- ["DHT22", "2"]
- ]), "type")
- .appendField(Blockly.Msg.DHT_SETUP_TEXT_3);
- types == 1 ? this.appendDummyInput()
- .appendField(Blockly.Msg.DHT_SETUP_TEXT_4)
- .appendField(new Blockly.FieldDropdown([
- ["1", "0"],
- ["2", "1"],
- ["3", "2"],
- ["13", "3"],
- ["14", "4"],
- ["15", "5"],
- ["17", "6"],
- ["21", "7"],
- ["22", "8"],
- ["23", "9"],
- ["24", "10"],
- ["29", "11"],
- ["30", "12"]
- ]), "gpio") :
- this.appendDummyInput()
- .appendField(Blockly.Msg.DHT_SETUP_TEXT_4)
- .appendField(new Blockly.FieldDropdown([
- ["4", "0"],
- ["12", "1"],
- ["13", "2"],
- ["14", "3"],
- ["15", "4"],
- ["16", "5"],
- ["17", "6"],
- ["21", "7"],
- ["22", "8"],
- ["25", "9"],
- ["27", "10"]
- ]), "gpio");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setInputsInline(false);
- this.setColour("#629978");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('digital_get_type');
- var TOOLTIPS = {
- '2': Blockly.Msg.DHT_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module),
- '1': Blockly.Msg.DHT_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['DHT_setup'] = function (block) {
- var dropdown_type = block.getFieldValue('digital_get_type');
- var type = block.getFieldValue('type');
- var gpio = block.getFieldValue('gpio');
- var code = '';
- if (dropdown_type == "1") {
- if (type == "1") {
- Blockly.Python.definitions_['import_iot_DHT_setup'] = '' +
- 'import dht\n' +
- 'import machine\n' +
- 'import time\n' +
- '\n' +
- 'd = dht.DHT11(machine.Pin(' + iot_servo_pin[gpio] + '))\n' +
- '\n';
- Blockly.Python.definitions_['import_DHT_read_data'] = '' +
- 'def GetDHTTemperatureValue():\n' +
- ' try:\n' +
- ' d.measure()\n' +
- ' return d.temperature()\n' +
- ' except:\n' +
- ' return d.temperature()\n' +
- '\n' +
- 'def GetDHTHumidityValue():\n' +
- ' try:\n' +
- ' d.measure()\n' +
- ' return d.humidity()\n' +
- ' except:\n' +
- ' return d.humidity()\n' +
- '\n' +
- '';
- }
- else {
- Blockly.Python.definitions_['import_iot_DHT_setup'] = '' +
- 'import dht\n' +
- 'import machine\n' +
- 'import time\n' +
- '\n' +
- 'd = dht.DHT22(machine.Pin(' + iot_servo_pin[gpio] + '))\n' +
- '\n';
- Blockly.Python.definitions_['import_DHT_read_data'] = '' +
- 'def GetDHTTemperatureValue():\n' +
- ' try:\n' +
- ' d.measure()\n' +
- ' return d.temperature()\n' +
- ' except:\n' +
- ' return d.temperature()\n' +
- '\n' +
- 'def GetDHTHumidityValue():\n' +
- ' try:\n' +
- ' d.measure()\n' +
- ' return d.humidity()\n' +
- ' except:\n' +
- ' return d.humidity()\n' +
- '\n' +
- '';
- }
- } else if (dropdown_type == "2") {
- if (type == "1") {
- Blockly.Python.definitions_['import_ai_DHT_setup'] = '' +
- 'import dhtsensor\n' +
- 'import time\n' +
- '\n' +
- 'dhtsensor.DHT_IO_Register(' + ai_servo_pin[gpio] + ')\n' +
- 'time.sleep_ms(100)\n' +
- '\n' +
- 'temperature = None\n' +
- 'humidity = None\n' +
- '\n';
- Blockly.Python.definitions_['import_DHT_read_data'] = '' +
- 'def GetDHTTemperatureValue():\n' +
- ' global temperature,humidity\n' +
- ' if dhtsensor.measure() and temperature == None:\n' +
- ' temperature = dhtsensor.GetDHT11_temperature()\n' +
- ' humidity = dhtsensor.GetDHT11_humidity()\n' +
- ' time.sleep_ms(1000)\n' +
- ' return temperature\n' +
- ' elif temperature != None:\n' +
- ' temperature = dhtsensor.GetDHT11_temperature()\n' +
- ' humidity = None\n' +
- ' return temperature\n' +
- '\n' +
- 'def GetDHTHumidityValue():\n' +
- ' global temperature,humidity\n' +
- ' if dhtsensor.measure() and humidity == None:\n' +
- ' temperature = dhtsensor.GetDHT11_temperature()\n' +
- ' humidity = dhtsensor.GetDHT11_humidity()\n' +
- ' time.sleep_ms(1000)\n' +
- ' return humidity\n' +
- ' elif humidity != None:\n' +
- ' humidity = dhtsensor.GetDHT11_humidity()\n' +
- ' temperature = None\n' +
- ' return humidity\n' +
- '\n' +
- '';
- }
- else {
- Blockly.Python.definitions_['import_ai_DHT_setup'] = '' +
- 'import dhtsensor\n' +
- 'import time\n' +
- '\n' +
- 'dhtsensor.DHT_IO_Register(' + ai_servo_pin[gpio] + ')\n' +
- 'time.sleep_ms(100)\n' +
- '\n' +
- 'temperature = None\n' +
- 'humidity = None\n' +
- '\n';
- Blockly.Python.definitions_['import_DHT_read_data'] = '' +
- 'def GetDHTTemperatureValue():\n' +
- ' global temperature,humidity\n' +
- ' if dhtsensor.measure() and temperature == None:\n' +
- ' temperature = dhtsensor.GetDHT22_temperature()\n' +
- ' humidity = dhtsensor.GetDHT22_humidity()\n' +
- ' time.sleep_ms(1000)\n' +
- ' return temperature * 0.1\n' +
- ' elif temperature != None:\n' +
- ' temperature = dhtsensor.GetDHT22_temperature()\n' +
- ' humidity = None\n' +
- ' return temperature * 0.1\n' +
- '\n' +
- 'def GetDHTHumidityValue():\n' +
- ' global temperature,humidity\n' +
- ' if dhtsensor.measure() and humidity == None:\n' +
- ' temperature = dhtsensor.GetDHT22_temperature()\n' +
- ' humidity = dhtsensor.GetDHT22_humidity()\n' +
- ' time.sleep_ms(1000)\n' +
- ' return humidity * 0.1\n' +
- ' elif humidity != None:\n' +
- ' humidity = dhtsensor.GetDHT22_humidity()\n' +
- ' temperature = None\n' +
- ' return humidity * 0.1\n' +
- '\n' +
- '';
- }
- }
- return code;
- };
- Blockly.Blocks['DHT_read_data'] = {
- init: function () {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/env_get.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32_ENV_GET_TEXT)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.basic_temperature, "temperature"],
- [Blockly.Msg.basic_humidity, "humidity"]
- // [Blockly.Msg.basic_pressure, "pressure"]
- ]), "env_get_type")
- .appendField(Blockly.Msg.basic_env_value);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setColour("#629978");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('env_get_type');
- var TOOLTIPS = {
- 'temperature': Blockly.Msg.Esp32_Main_Controller_Get_Environmental_Value_TOOLTIP.replace('%1', Blockly.Msg.basic_temperature),
- 'humidity': Blockly.Msg.Esp32_Main_Controller_Get_Environmental_Value_TOOLTIP.replace('%1', Blockly.Msg.basic_humidity)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['DHT_read_data'] = function (block) {
- var env_get_type = block.getFieldValue('env_get_type');
- var code = "";
- if (env_get_type == "temperature") {
- code = 'GetDHTTemperatureValue()';
- } else if (env_get_type == "humidity") {
- code = 'GetDHTHumidityValue()';
- }
- return [code, Blockly.Python.ORDER_NONE];
- };
- /* PM2.5 */
- Blockly.Blocks['PM25_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/pm25sensor_setup.png", 220, 60, { alt: "*", flipRtl: "FALSE" }));
- let types = getLocalStorage("type")
- types == 1 ? this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_ai_module, "2"],
- [Blockly.Msg.time_iot_module, "1"]
- ]), "digital_get_type")
- .appendField(Blockly.Msg.PM25_SETUP_TEXT) :
- this.appendDummyInput()
- .appendField("")
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.time_iot_module, "1"],
- [Blockly.Msg.time_ai_module, "2"]
- ]), "digital_get_type")
- .appendField(Blockly.Msg.PM25_SETUP_TEXT);
- this.appendDummyInput()
- .appendField(Blockly.Msg.DHT_SETUP_TEXT_2)
- .appendField(new Blockly.FieldDropdown([
- ["GP2Y10", "1"]
- ]), "type")
- .appendField(Blockly.Msg.DHT_SETUP_TEXT_3);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setInputsInline(false);
- this.setColour("#386dc8");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('digital_get_type');
- var TOOLTIPS = {
- '2': Blockly.Msg.PM25_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_ai_module),
- '1': Blockly.Msg.PM25_SETUP_TOOLTIP.replace('%1', Blockly.Msg.time_iot_module)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['PM25_setup'] = function (block) {
- var dropdown_type = block.getFieldValue('digital_get_type');
- var type = block.getFieldValue('type');
- var code = '';
- if (dropdown_type == "1") {
- if (type == "1") {
- Blockly.Python.definitions_['import_iot_PM25_setup'] = '' +
- 'from machine import UART, Pin\n' +
- 'import pm25\n' +
- '\n' +
- 'uart = UART(2, baudrate=2400, rx=16, tx=17, timeout=10)\n' +
- '\n';
- }
- } else if (dropdown_type == "2") {
- if (type == "1") {
- Blockly.Python.definitions_['import_ai_PM25_setup'] = '' +
- 'import machine, time\n' +
- 'from fpioa_manager import fm\n' +
- 'import pm25\n' +
- '\n' +
- 'fm.register(13,fm.fpioa.UART2_TX)\n' +
- 'fm.register(14,fm.fpioa.UART2_RX)\n' +
- '\n' +
- 'uart = machine.UART(machine.UART.UART2,2400,bits=8,parity=None,stop=1)\n' +
- '\n';
- }
- }
- return code;
- };
- Blockly.Blocks['PM25_read_data'] = {
- init: function () {
- // this.appendDummyInput()
- // .appendField(new Blockly.FieldImage("blockly/media/env_get.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ESP32_ENV_GET_TEXT)
- .appendField("PM 2.5")
- .appendField(Blockly.Msg.basic_env_value);
- this.setInputsInline(false);
- this.setOutput(true, null);
- this.setTooltip(Blockly.Msg.PM25_REDD_DATA_TOOLTIP);
- this.setColour("#386dc8");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['PM25_read_data'] = function (block) {
- var code = "pm25.getPM25Value(uart)";
- return [code, Blockly.Python.ORDER_NONE];
- };
- /* RTC */
- var rtcColor = "#3c7683";
- Blockly.Blocks['RTC_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/sensors-rtc.png", 60, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField("")
- .appendField(Blockly.Msg.time_ai_module)
- .appendField(Blockly.Msg.RTC_SETUP);
- this.appendDummyInput()
- .appendField(Blockly.Msg.DHT_SETUP_TEXT_2)
- .appendField(new Blockly.FieldDropdown([
- ["DS3231", "1"]
- ]), "type");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setInputsInline(false);
- this.setColour(rtcColor);
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.RTC_setup_TOOLTIP);
- }
- };
- Blockly.Python['RTC_setup'] = function (block) {
- var type = block.getFieldValue('type');
- var code = '';
- if (type == "1") {
- Blockly.Python.definitions_['import_iot_RTC_setup'] = '' +
- 'import machine\n' +
- 'import DS3231micro\n' +
- '\n' +
- 'rtc = DS3231micro.DS3231(30, 29)\n' +
- '\n';
- }
- return code;
- };
- Blockly.Blocks['RTC_setDateTime'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.RTC_SETDATETIME);
- this.appendDummyInput()
- .appendField(Blockly.Msg.RTC_SETDATE)
- .appendField(new Blockly.FieldNumber(18, 0, 50), "year")
- .appendField("-")
- .appendField(new Blockly.FieldNumber(1, 0, 12), "month")
- .appendField("-")
- .appendField(new Blockly.FieldNumber(1, 0, 31), "day")
- this.appendDummyInput()
- .appendField(Blockly.Msg.RTC_SETTIME)
- .appendField(new Blockly.FieldNumber(12, 0, 24), "hour")
- .appendField(":")
- .appendField(new Blockly.FieldNumber(30, 0, 60), "minute")
- .appendField(":")
- .appendField(new Blockly.FieldNumber(10, 0, 60), "second")
- this.appendDummyInput()
- .appendField(Blockly.Msg.RTC_SETWEEK)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.Monday, "1"],
- [Blockly.Msg.Tuesday, "2"],
- [Blockly.Msg.Wednesday, "3"],
- [Blockly.Msg.Thursday, "4"],
- [Blockly.Msg.Friday, "5"],
- [Blockly.Msg.Saturday, "6"],
- [Blockly.Msg.Sunday, "7"]
- ]), "week")
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour(rtcColor);
- this.setTooltip(Blockly.Msg.RTC_setDateTime_TOOLTIP);
- this.setHelpUrl("");
- },
- /**
- * Called whenever anything on the workspace changes.
- * It checks the number of data and time settting
- * @this Blockly.Block
- */
- onchange: function () {
- if (this.getFieldValue("year") > 50) {
- this.setFieldValue("19", "year");
- }
- if (this.getFieldValue("month") > 12) {
- this.setFieldValue("12", "month");
- } else if (this.getFieldValue("month") < 0) {
- this.setFieldValue("1", "month");
- }
- if (this.getFieldValue("day") > 31) {
- this.setFieldValue("31", "day");
- } else if (this.getFieldValue("day") < 0) {
- this.setFieldValue("1", "day");
- }
- if (this.getFieldValue("hour") > 24) {
- this.setFieldValue("24", "hour");
- } else if (this.getFieldValue("hour") < 0) {
- this.setFieldValue("1", "hour");
- }
- if (this.getFieldValue("minute") > 60) {
- this.setFieldValue("60", "minute");
- } else if (this.getFieldValue("minute") < 0) {
- this.setFieldValue("1", "minute");
- }
- if (this.getFieldValue("second") > 60) {
- this.setFieldValue("60", "second");
- } else if (this.getFieldValue("second") < 0) {
- this.setFieldValue("1", "second");
- }
- }
- };
- Blockly.Python['RTC_setDateTime'] = function (block) {
- var year = block.getFieldValue("year");
- var month = block.getFieldValue("month");
- var day = block.getFieldValue("day");
- var hour = block.getFieldValue("hour");
- var minute = block.getFieldValue("minute");
- var second = block.getFieldValue("second");
- var week = block.getFieldValue('week');
- var code = 'rtc.setDateTime(' + year + ',' + month + ',' + day + ',' + week + ',' + hour + ',' + minute + ',' + second + ')\n';
- return code;
- };
- Blockly.Blocks['RTC_getDate'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.RTC_GETDATE)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.RTC_YEAR, "year"],
- [Blockly.Msg.RTC_MONTH, "month"],
- [Blockly.Msg.RTC_DAY, "day"],
- ]), "date_detail")
- this.setColour(rtcColor);
- this.setOutput(true, null);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('date_detail');
- var TOOLTIPS = {
- "year": Blockly.Msg.RTC_GETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_YEAR),
- "month": Blockly.Msg.RTC_GETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_MONTH),
- "day": Blockly.Msg.RTC_GETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_DAY),
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['RTC_getDate'] = function (block) {
- var date_detail = block.getFieldValue("date_detail");
- var code = '';
- if (date_detail == "year") {
- code = 'rtc.getYear()';
- }
- else if (date_detail == "month") {
- code = 'rtc.getMonth()';
- }
- else {
- code = 'rtc.getDay()';
- }
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['RTC_getTime'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.RTC_GETTIME)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.RTC_HOUR, "hour"],
- [Blockly.Msg.RTC_MINUTE, "minute"],
- [Blockly.Msg.RTC_SECOND, "second"],
- ]), "time_detail")
- this.setColour(rtcColor);
- this.setOutput(true, null);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('time_detail');
- var TOOLTIPS = {
- "hour": Blockly.Msg.RTC_GETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_HOUR),
- "minute": Blockly.Msg.RTC_GETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_MINUTE),
- "second": Blockly.Msg.RTC_GETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_SECOND),
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['RTC_getTime'] = function (block) {
- var time_detail = block.getFieldValue("time_detail");
- var code = '';
- if (time_detail == "hour") {
- code = 'rtc.getHour()';
- }
- else if (time_detail == "minute") {
- code = 'rtc.getMinutes()';
- }
- else {
- code = 'rtc.getSeconds()';
- }
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['RTC_getWeek'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.RTC_GETWEEK);
- this.setColour(rtcColor);
- this.setOutput(true, null);
- this.setTooltip(Blockly.Msg.RTC_getWeek_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['RTC_getWeek'] = function (block) {
- var code = 'rtc.getDayOfWeek()';
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['RTC_getDateTime'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.RTC_getDateTime);
- this.setColour(rtcColor);
- this.setOutput(true, null);
- this.setTooltip(Blockly.Msg.RTC_getDateTime_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['RTC_getDateTime'] = function (block) {
- var code = 'rtc.getDateTime()';
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['RTC_setDate'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.RTC_setDATE)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.RTC_YEAR, "year"],
- [Blockly.Msg.RTC_MONTH, "month"],
- [Blockly.Msg.RTC_DAY, "day"],
- ]), "date_detail")
- .appendField(Blockly.Msg.RTC_WEI)
- .appendField(new Blockly.FieldNumber(0, null, null), "date")
- this.setOutput(false, null);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setColour(rtcColor);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('date_detail');
- var TOOLTIPS = {
- "year": Blockly.Msg.RTC_SETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_YEAR),
- "month": Blockly.Msg.RTC_SETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_MONTH),
- "day": Blockly.Msg.RTC_SETDATE_TOOLTIP.replace('%1', Blockly.Msg.RTC_DAY),
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['RTC_setDate'] = function (block) {
- var date_detail = block.getFieldValue("date_detail");
- var date = block.getFieldValue("date");
- var code = '';
- if (date_detail == "year") {
- code = 'rtc.setYear(' + date + ')\n';
- }
- else if (date_detail == "month") {
- code = 'rtc.setMonth(' + date + ')\n';
- }
- else {
- code = 'rtc.setDay(' + date + ')\n';
- }
- return code;
- };
- Blockly.Blocks['RTC_setTime'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.RTC_setTIME)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.RTC_HOUR, "hour"],
- [Blockly.Msg.RTC_MINUTE, "minute"],
- [Blockly.Msg.RTC_SECOND, "second"],
- ]), "time_detail")
- .appendField(Blockly.Msg.RTC_WEI)
- .appendField(new Blockly.FieldNumber(0, 0, null), "time")
- this.setColour(rtcColor);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('time_detail');
- var TOOLTIPS = {
- "hour": Blockly.Msg.RTC_SETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_HOUR),
- "minute": Blockly.Msg.RTC_SETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_MINUTE),
- "second": Blockly.Msg.RTC_SETTIME_TOOLTIP.replace('%1', Blockly.Msg.RTC_SECOND),
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['RTC_setTime'] = function (block) {
- var time_detail = block.getFieldValue("time_detail");
- var time = block.getFieldValue("time");
- var code = '';
- if (time_detail == "hour") {
- code = 'rtc.setHour(' + time + ')\n';
- }
- else if (time_detail == "minute") {
- code = 'rtc.setMinutes(' + time + ')\n';
- }
- else {
- code = 'rtc.setSeconds(' + time + ')\n';
- }
- return code;
- };
- Blockly.Blocks['RTC_setWeek'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.RTC_setWEEK)
- .appendField(new Blockly.FieldNumber(1, 0, null), "week");
- this.setColour(rtcColor);
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip(Blockly.Msg.RTC_setWeek_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['RTC_setWeek'] = function (block) {
- var week = block.getFieldValue("week");
- var code = 'rtc.setDayOfWeek(' + week + ')\n';
- return code;
- };
|