aiyboard.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Generating Python for colour blocks.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Python.AIYBoard');
  26. goog.require('Blockly.Python');
  27. Blockly.Python['AIYBoard_from'] = function(block) {
  28. var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_NONE) || '__'
  29. var code = 'from aiy.board '+ input+'\n';
  30. return code
  31. };
  32. Blockly.Python['AIYBoard_import'] = function(block) {
  33. var board = block.getFieldValue("BOARD")
  34. var code = "import "+ board
  35. return [code,Blockly.Python.ORDER_ATOMIC]
  36. };
  37. Blockly.Python['AIYBoard_Board'] = function(block) {
  38. var code = "Board()"
  39. return [code,Blockly.Python.ORDER_ATOMIC];
  40. }
  41. Blockly.Python['AIYBoard_LED_state'] = function(block) {
  42. var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_ATOMIC) || '__'
  43. var code = input+ ".led.state";
  44. return [code,Blockly.Python.ORDER_ATOMIC];
  45. }
  46. Blockly.Python['AIYBoard_LED_status'] = function(block) {
  47. var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_ATOMIC) || '__'
  48. var code = input+ ".led.status";
  49. return [code,Blockly.Python.ORDER_ATOMIC];
  50. }
  51. Blockly.Python['AIYBoard_LED_setting'] = function(block) {
  52. var setting = block.getFieldValue("SETTING")
  53. var code = "Led."+ setting;
  54. return [code,Blockly.Python.ORDER_ATOMIC];
  55. }
  56. Blockly.Python['AIYBoard_board'] = function(block) {
  57. var code = "board";
  58. return [code, Blockly.Python.ORDER_ATOMIC]
  59. }
  60. Blockly.Python['AIYBoard_button'] = function(block) {
  61. var code = "button";
  62. return [code, Blockly.Python.ORDER_ATOMIC]
  63. }
  64. Blockly.Python['AIYBoard_button_press'] = function(block) {
  65. var input_a = Blockly.Python.statementToCode(block,"STACK");
  66. var code = "def _on_button_pressed(self):\n"+
  67. input_a;
  68. return code;
  69. }
  70. Blockly.Python['AIYBoard_board_led_status'] = function(block) {
  71. var input_a = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_ATOMIC);
  72. var code = "self._board.led.status = "+ input_a +"\n";
  73. return code;
  74. }
  75. Blockly.Python['AIYBoard_board_led_state'] = function(block) {
  76. var input_a = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_ATOMIC);
  77. var code = "self._board.led.state = "+ input_a +"\n";
  78. return code;
  79. }