12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320 |
- // Serial
- Blockly.Blocks['serial_write_data'] = {
- init: function () {
- let types = getLocalStorage("type")
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/uart_serial_send_header.png", 110, 35, { alt: "*", flipRtl: "FALSE" }));
- types == 1 ? this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_write_on)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.serialcomm_write_on_ai, "ai"],
- [Blockly.Msg.serialcomm_write_on_iot, "iot"]
- ]), "type") :
- this.appendDummyInput()
- .appendField(Blockly.Msg.serialcomm_write_on)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.serialcomm_write_on_iot, "iot"],
- [Blockly.Msg.serialcomm_write_on_ai, "ai"]
- ]), "type");
- 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("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.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.Python['serial_write_data'] = function (block) {
- var api = block.getFieldValue('t_api');
- var number_uart_bps = block.getFieldValue('uart_bps');
- var dropdown_type = block.getFieldValue('type');
- if (dropdown_type == "ai") {
- Blockly.Python.definitions_['import_machine_uart'] = 'import machine, time\n' +
- 'from fpioa_manager import fm\n' +
- '\n';
- Blockly.Python.addVariable('uart_data', 'uart_data = ""', true);
- Blockly.Python.definitions_['uart_setup'] = '' +
- 'fm.register(13,fm.fpioa.UART2_TX)\n' +
- 'fm.register(14,fm.fpioa.UART2_RX)\n' +
- 'ai_uart = machine.UART(machine.UART.UART2,' + number_uart_bps + ',bits=8,parity=None,stop=1)\n' +
- '';
- // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
- 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 + "";
- var code = '' +
- 'uart_data = ' + ai_uart_data_final.slice(0, -8) + '\n' +
- 'ai_uart.write(b"SOF|" + str(uart_data) + "|\\r\\n")\n' +
- 'time.sleep_ms(20)\n' +
- '';
- } else if (dropdown_type == "iot") {
- Blockly.Python.definitions_['import_machine_uart'] = 'from machine import UART, Pin\n' +
- 'import time\n' +
- '\n';
- Blockly.Python.definitions_['uart_setup'] = '' +
- '_iot_uart = UART(2, tx=17, rx=16)\n' +
- '_iot_uart.init(115200, bits=8, parity=None, stop=1)\n' +
- '';
- Blockly.Python.addVariable('_iot_uart_data', '_iot_uart_data = ""', true);
- item_value = '';
- var iot_uart_data = "";
- for (var n = 0; n < this.itemCount_; n++) {
- item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
- iot_uart_data += 'str(' + item_value + ') + \"|\" + ';
- }
- var iot_uart_data_final = iot_uart_data + "";
- var code = '' +
- '_iot_uart_data = ' + iot_uart_data_final.slice(0, -8) + '\n' +
- '_iot_uart.write("SOF|" + str(_iot_uart_data) + "|\\r\\n")\n' +
- 'time.sleep_ms(20)\n' +
- '';
- }
- return code;
- };
- Blockly.Blocks['serial_send_data_to_microbit'] = {
- init: function () {
- let types = getLocalStorage("type")
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/microbit_data_send_header.png", 90, 70, { alt: "*", flipRtl: "FALSE" }));
- types == 1 ? this.appendValueInput("NAME")
- .appendField(Blockly.Msg.serial_send_data_on)
- .setCheck(null)
- .appendField(new Blockly.FieldDropdown([
- ["A.I.", "ai"],
- ["IoT", "iot"]
- ]), "module_type_microbit")
- .appendField(Blockly.Msg.serial_send_data_on_send_data) :
- this.appendValueInput("NAME")
- .appendField(Blockly.Msg.serial_send_data_on)
- .setCheck(null)
- .appendField(new Blockly.FieldDropdown([
- ["IoT", "iot"],
- ["A.I.", "ai"]
- ]), "module_type_microbit")
- .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("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('module_type_microbit');
- var TOOLTIPS = {
- 'iot': Blockly.Msg.Serial_Send_Data_To_Microbit_TOOLTIP.replace('%1', "IoT"),
- 'ai': Blockly.Msg.Serial_Send_Data_To_Microbit_TOOLTIP.replace('%1', "A.I.")
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['serial_send_data_to_microbit'] = function (block) {
- var dropdown_module_type = block.getFieldValue('module_type_microbit');
- var value_name = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC) || "";
- // TODO: Assemble Python into code variable.
- var code = '';
- if (dropdown_module_type == 'iot') {
- Blockly.Python.definitions_['import_microbit_iot_uart'] = "from machine import UART, Pin \nimport time\n";
- let iot = `
- _iot_microbit_uart = UART(2, tx=17, rx=16)
- _iot_microbit_uart.init(115200, bits=8, parity=None, stop=1)`;
- Blockly.Python.addVariable('_iot_microbit_uart', iot, true);
- code = '\n' +
- "_iot_microbit_uart_data =" + value_name + "\n" +
- '_iot_microbit_uart.write(str(_iot_uart_data))\n' +
- "time.sleep_ms(20)\n" +
- '';
- } else {
- Blockly.Python.definitions_['import_microbit_ai_uart'] = "import machine, time \nfrom fpioa_manager import fm\n";
- let ai = `
- fm.register(13,fm.fpioa.UART2_TX)
- fm.register(14,fm.fpioa.UART2_RX)
- ai_microbit_uart = machine.UART(machine.UART.UART2,115200,bits=8,parity=None,stop=1)`;
- Blockly.Python.addVariable('ai_microbit_uart', ai, true);
- code = '\n' +
- "uart_microbit_data =" + value_name + "\n" +
- 'ai_microbit_uart.write(b""+str(uart_microbit_data)+"")\n' +
- "time.sleep_ms(20)\n" +
- '';
- }
- 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_.extension_servo_import = "from machine import Timer,PWM";
- Blockly.Python.addSetup("extension_servo_setup", '' +
- 'def _ai_servo_write(servo,angle):\n' +
- '\tangle = angle - 90\n' +
- '\tleftSpan = 180 - 0\n' +
- '\trightSpan = 175 - 5\n' +
- '\tvalueScaled = float(angle - 0) / float(leftSpan)\n' +
- '\tservo.duty(((10 + (valueScaled * rightSpan))+90)/180*10+2.5)\n' +
- '');
- var code = '\n';
- return code;
- };
- Blockly.Blocks['extension_servo_write_on_ai'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.servo_set_gpio_ai)
- .appendField(new Blockly.FieldDropdown([
- // ["0", "0"],
- // ["1", "1"],
- // ["2", "2"],
- // ["3", "3"],
- // ["13", "4"],
- // ["14", "5"],
- // ["15", "6"],
- // ["17", "7"],
- // ["21", "8"],
- // ["22", "9"],
- // ["23", "10"],
- // ["24", "11"],
- // ["29", "12"],
- // ["30", "13"]
- ["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.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("");
- }
- };
- 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.addSetup("extension_servo_write_ai_" + value_gpio, '' +
- '_servo_tim1 = Timer(' + ai_servo_timer[value_gpio] + ')\n' +
- '_servo_gpio' + ai_servo_pin[value_gpio] + ' = PWM(_servo_tim1, freq=50, duty=0, pin=' + ai_servo_pin[value_gpio] + ')\n' +
- '');
- var real_servo_degree = value_degree;
- var code = '' +
- '_ai_servo_write(_servo_gpio' + ai_servo_pin[value_gpio] + ',' + real_servo_degree + ')\n' +
- '';
- return code;
- };
- let servoCloor = "#386dc8"
- 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' +
- '';
- var code = '\n';
- return code;
- };
- Blockly.Blocks['esp32_main_controller_servo_set'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.servo_set_gpio);
- this.appendValueInput("pin_select")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.servo_rotate_to);
- this.appendValueInput("degree")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.servo_degree);
- 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_pin_select = Blockly.Python.valueToCode(block, 'pin_select', Blockly.Python.ORDER_ATOMIC);
- var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- Blockly.Python.addVariable('S' + value_pin_select, 'S' + value_pin_select + ' = PWM(Pin(' + value_pin_select + '), freq=50, duty=0)', true);
- Blockly.Python.definitions_['definition_servo_set'] = '' +
- 'def Servo(servo,angle):\n' +
- ' angle = angle - 90\n' +
- ' servo.duty(int(((angle+90)*2/180+0.5)/20*1023))\n' +
- '';
- var code = '' +
- 'Servo(S' + value_pin_select + ',' + 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, 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 (dropdown_direction == "pos") {
- code = '_iot_motor.speed(' + dropdown_motor_type + ', (' + value_speed + '*16))\n';
- } else if (dropdown_direction == "neg") {
- code = '_iot_motor.speed(' + dropdown_motor_type + ', -(' + 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(MOTOR_BLOCK_COLOR);
- 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_['ai_controller_set_motor'] = '' +
- 'import pca9685, motor\n' +
- 'from machine import I2C\n' +
- '\n' +
- '_ai_motor_scl = 30\n' +
- '_ai_motor_sda = 29\n' +
- '\n' +
- '_ai_motor_i2c = I2C(I2C.I2C0, freq=100000, scl=_ai_motor_scl, sda=_ai_motor_sda)\n' +
- '_ai_motor = motor.DCMotors(_ai_motor_i2c)\n' +
- '';
- 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([
- ["A", "0"],
- ["B", "1"],
- ["C", "3"],
- ["D", "2"]
- ]), "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(MOTOR_BLOCK_COLOR);
- 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');
- if (dropdown_direction == "pos") {
- var code = '_ai_motor.speed(' + dropdown_motor_type + ', (' + value_speed + '*16))\n';
- } else if (dropdown_direction == "neg") {
- var code = '_ai_motor.speed(' + dropdown_motor_type + ', -(' + value_speed + '*16))\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 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 code = '_a = _ai_led_matrix.set_led((((' + cord_y + '*6)//6)-1)*6+(' + cord_x + '-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((((i*6)//6)-1)*6+(j-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_led_draw_rectangle(' + cord_y + ',' + cord_x + ',' + size_width + ',' + size_height + ',rgba_to_rgb_conversion' + value_color + ')\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_NONE];
- };
- 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 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 code = '_6x6_led_matrix[(((' + cord_y + '*6)//6)-1)*6+(' + cord_x + '-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_led_draw_rectangle(' + cord_y + ',' + cord_x + ',' + size_width + ',' + size_height + ',rgba_to_rgb_conversion' + value_color + ')\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;
- };
- /*
- _ _____ ____ ____ _ _
- | | | ____| _ \ / ___|| |_ _ __(_)_ __
- | | | _| | | | | \___ \| __| '__| | '_ \
- | |___| |___| |_| | ___) | |_| | | | |_) |
- |_____|_____|____/ |____/ \__|_| |_| .__/
- |_|
- */
- var ESP32_LED_STRIP_COLOR = AILEDCOLOR;
- Blockly.Blocks['esp32_main_controller_led_strip_setup'] = {
- init: function () {
- let types = getLocalStorage("type")
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/led_strip_setup.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- types == 1 ? this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ledstrip_setup_text_ai, "ai"],
- [Blockly.Msg.ledstrip_setup_text_iot, "iot"]
- ]), "type")
- .appendField(Blockly.Msg.ledstrip_setup_text)
- .appendField(new Blockly.FieldVariable("STRIP"), "varitem")
- .appendField(Blockly.Msg.ledstrip_setup) :
- this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ledstrip_setup_text_iot, "iot"],
- [Blockly.Msg.ledstrip_setup_text_ai, "ai"]
- ]), "type")
- .appendField(Blockly.Msg.ledstrip_setup_text)
- .appendField(new Blockly.FieldVariable("STRIP"), "varitem")
- .appendField(Blockly.Msg.ledstrip_setup);
- this.appendValueInput("io")
- .setCheck(null)
- .appendField(Blockly.Msg.ledstrip_set_gpio);
- this.appendValueInput("count")
- .setCheck(null)
- .appendField(Blockly.Msg.ledstrip_set_total_led);
- 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("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'ai': Blockly.Msg.Esp32_Main_Controller_Led_Strip_Setup_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_ai),
- 'iot': Blockly.Msg.Esp32_Main_Controller_Led_Strip_Setup_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_iot)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_led_strip_setup'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_io = Blockly.Python.valueToCode(block, 'io', Blockly.Python.ORDER_ATOMIC);
- var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
- var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.definitions_['esp32_main_controller_led_strip_setup_def'] = '' +
- '_ai_brightness = ' + value_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' +
- '';
- if (dropdown_type == "iot") {
- Blockly.Python.definitions_['led_strip_definition'] = 'import machine, neopixel\n';
- Blockly.Python.addVariable(variable_name, variable_name + ' = neopixel.NeoPixel(machine.Pin(' + value_io + '), ' + value_count + ')', true);
- var code = '\n';
- } else if (dropdown_type == "ai") {
- Blockly.Python.definitions_['led_strip_definition'] = 'from modules import ws2812\n';
- Blockly.Python.addVariable(variable_name, '' +
- variable_name + ' = ws2812(' + value_io + ',' + value_count + ')\n' +
- '\n', true);
- var code = '\n';
- }
- 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 () {
- let types = getLocalStorage("type")
- types == 1 ? this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ledstrip_setup_text_ai, "ai"],
- [Blockly.Msg.ledstrip_setup_text_iot, "iot"]
- ]), "type")
- .appendField(Blockly.Msg.ledstrip_set_light_up)
- .appendField(new Blockly.FieldVariable("STRIP"), "varitem") :
- this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ledstrip_setup_text_iot, "iot"],
- [Blockly.Msg.ledstrip_setup_text_ai, "ai"]
- ]), "type")
- .appendField(Blockly.Msg.ledstrip_set_light_up)
- .appendField(new Blockly.FieldVariable("STRIP"), "varitem");
- this.appendValueInput("location")
- .setCheck(null)
- .appendField(Blockly.Msg.ledstrip_set_no);
- this.appendValueInput("color")
- .setCheck(null)
- .appendField(Blockly.Msg.ledstrip_set_color);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(ESP32_LED_STRIP_COLOR);
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'iot': Blockly.Msg.Esp32_Main_Controller_Led_Strip_Set_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_iot),
- 'ai': Blockly.Msg.Esp32_Main_Controller_Led_Strip_Set_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_ai)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['esp32_main_controller_led_strip_set'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- var value_location = Blockly.Python.valueToCode(block, 'location', Blockly.Python.ORDER_ATOMIC);
- var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- if (dropdown_type == "iot") {
- if (value_color.charAt(0) != '#') {
- var code = '' +
- variable_name + '[' + value_location + '-1] = rgba_to_rgb_conversion' + value_color + '\n';
- } else if (value_color.charAt(0) == '#') {
- 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) { }
- var code = '' +
- variable_name + '[' + value_location + '-1] = rgba_to_rgb_conversion(' + d + ',' + e + ',' + f + ')\n';
- }
- } else if (dropdown_type == "ai") {
- if (value_color.charAt(0) != '#') {
- var code = '' +
- '_a = ' + variable_name + '.set_led(' + value_location + '-1, rgba_to_rgb_conversion' + value_color + ')\n';
- } else if (value_color.charAt(0) == '#') {
- 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) { }
- var code = '' +
- '_a = ' + variable_name + '.set_led(' + value_location + '-1,rgba_to_rgb_conversion(' + d + ',' + e + ',' + f + '))\n';
- }
- }
- return code;
- };
- Blockly.Blocks['led_strip_show_above'] = {
- init: function () {
- let types = getLocalStorage("type")
- types == 1 ? this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ledstrip_setup_text_ai, "ai"],
- [Blockly.Msg.ledstrip_setup_text_iot, "iot"]
- ]), "type")
- .appendField(Blockly.Msg.ledstrip_setup_text_show_above_set)
- .appendField(new Blockly.FieldVariable("STRIP"), "varitem")
- .appendField(Blockly.Msg.ledstrip_setup_text_show_above) :
- this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ledstrip_setup_text_iot, "iot"],
- [Blockly.Msg.ledstrip_setup_text_ai, "ai"]
- ]), "type")
- .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("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- 'iot': Blockly.Msg.Led_Strip_Show_Above_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_iot),
- 'ai': Blockly.Msg.Led_Strip_Show_Above_TOOLTIP.replace('%1', Blockly.Msg.ledstrip_setup_text_ai)
- };
- return TOOLTIPS[mode];
- });
- }
- };
- Blockly.Python['led_strip_show_above'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
- // TODO: Assemble Python into code variable.
- if (dropdown_type == "ai") {
- var code = variable_name + '.display()\n';
- } else if (dropdown_type == "iot") {
- var code = variable_name + '.write()\n';
- }
- 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=17, tx=16, 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(14,fm.fpioa.UART2_TX)\n' +
- 'fm.register(13,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("ENTET_YOUT_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.cn/\"\\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");
- 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.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 mpu6050
- #申明mpu6050传感器IIC通信引脚
- _SENSOR_MPU_PINS = I2C(scl=Pin(22), sda=Pin(21))
- 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 #三个方向总加速度矢量值
- totave=[0.0]*2 #最大和最小加速度矢量值平均值
- peak=0 #波峰数据
- trough=0 #波谷数据
- data=[0]*15 #实时采集数据滚动递进列表
- steps=0 #行走步伐数
- flag0=0 #步数提取标志位
- flag1=0 #波峰提取标志位
- flag2=0 #波谷提取标志位
- hill=0 #波峰波谷差值
- starttime=0 #波峰时间点
- endtime=0 #波谷时间点
- lasttime=0 #波峰波谷间隔时间
- _MPU = mpu6050.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')
- #获取三个方向加速度函数
- def mpu6050_get_accel(type):
- _MPU = mpu6050.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]=mpu6050_get_accel('X')-345
- sum0=xval[i]+sum0
- time.sleep_ms(100)
- xavg=sum0/100
- #print(xavg)
- for i in range(100):
- yval[i]=mpu6050_get_accel('Y')-346
- sum1=yval[i]+sum1
- time.sleep_ms(100)
- yavg=sum1/100
- #print(yavg)
- for i in range(100):
- zval[i]=mpu6050_get_accel('Z')-416
- sum2=zval[i]+sum2
- time.sleep_ms(100)
- zavg=sum2/100
- #print(zavg)
- def getPedometer():
- global q, a, 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=mpu6050_get_accel('X')-345
- time.sleep_ms(1)
- yaccl=mpu6050_get_accel('Y')-346
- time.sleep_ms(1)
- zaccl=mpu6050_get_accel('Z')-416
- time.sleep_ms(1)
- #计算X、Y、Z轴三个方向加速度值的平方根来计算总加速度矢量
- totvect=math.sqrt(((xaccl-xavg)*(xaccl-xavg))+((yaccl-yavg)*(yaccl-yavg))+((zaccl-zavg)*(zaccl-zavg)))
- totave[0]=totave[1]
- totave[1]=(totvect+totave[0])/2
- #数据轮替
- data[2]=data[1]
- data[1]=data[0]
- data[0]=totave[1]
- #检测是否到达一个波峰,如果是,将波峰数据采集标志位置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 #将波峰波谷差值清零
- return steps
- time.sleep_ms(20)
- `;
- 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];
- };
|