LEGO Mindstorms EV3


Byte Code Examples

Handling Screen in Programs

  When a program is started the animated run screen will appear as default. First
  time a graphical byte code is used - the screen will stop and the program can
  take over and fully control the screen drawing and updating.


  USE SCREEN FOR GRAPHICS
  -----------------------
  
    opUI_DRAW,LC0(FILLWINDOW),LC0(0),LC0(0),LC0(0)
  
      (clear entire screen by filling it with zero - background color)
      
      
    opUI_DRAW, - - - - - - 
    opUI_DRAW, - - - - - - 
    opUI_DRAW, - - - - - - 
    
      (draw graphical stuff on the screen)
      
    
    opUI_DRAW,LC0(UPDATE)  
  
      (show the stuff by updating the screen)
  


  RESTORE RUN SCREEN
  ------------------
  
    opUI_WRITE,LC0(INIT_RUN)

      (enable the animated run screen)


Motor read Tacho and Speed

  It is possible to read and get informations from an output port in the same manner
  as for an input port - only the port number differs - they starts with 16.
  (PORTA = 16, PORTB = 17, PORTC = 18 and PORTD = 19)


  READ MOTOR TACHO COUNT
  ----------------------
  
    opINPUT_DEVICE,LC0(READY_SI),LC0(0),LC0(16),LC0(0),LC0(0),LC0(1),LV0(0)
                                           ----
      (reads si from "LAYER 0" "PORTA" "default type" "mode 0" into "local variable 0")                                          


  READ MOTOR SPEED
  ----------------

    opINPUT_DEVICE,LC0(READY_SI),LC0(0),LC0(19),LC0(0),LC0(2),LC0(1),LV0(0)
                                           ----           --- 
      (reads si from "LAYER 0" "PORTD" "default type" "mode 2" into "local variable 0")                                          

See type 7 and 8 in this link


Program variable change before start

  NORMAL START OF A PROGRAM
  -------------------------
  
    Direct commands:

    // Get image from file
    opFILE,LC0(LOAD_IMAGE),LC0(USER_SLOT),LCS,'.','.','/','a','p','p','s','/','t','s','t','/','t','s','t',0,LV0(0),LV0(4),
    
    // Prepare image for run and start VM threat automatically (object 1)
    opPROGRAM_START,LC0(USER_SLOT),LV0(0),LV0(4),LC0(0),
                                                    --- 

  CHANGE GLOBAL VARIABLE 2 BEFORE STARTING A PROGRAM
  --------------------------------------------------

    Direct commands:

    // Get image from file
    opFILE,LC0(LOAD_IMAGE),LC0(USER_SLOT),LCS,'.','.','/','a','p','p','s','/','t','s','t','/','t','s','t',0,LV0(0),LV0(4),
    
    // Prepare image for run and set program (USER_SLOT) waiting
    opPROGRAM_START,LC0(USER_SLOT),LV0(0),LV0(4),LC0(2),      
                                                    ---
    // Init direct command local variable 0 with 1
    opINIT_BYTES,LV0(0),LC0(1),LC0(1),
    
    // Write direct command local variable 0 to waiting program (USER_SLOT) global variable 2
    opMEMORY_WRITE,LC0(USER_SLOT),LC0(0),LC0(2),LC0(0),LV0(0),

    // Start VM thread (object 1) in waiting program (USER_SLOT)    
    opPROGRAM_INFO,LC0(OBJ_START),LC0(USER_SLOT),LC0(1),

String Initialisation and Functions

  STRINGS IN VM MEMORY
  --------------------
    Strings are located in the VM's normal global or local variabel space as other DATA8 
    variables and referenced by the variabel (start) number. Enough room must be ensured
    by allocating the next variabel on a suitable distance (string length) ahead.
  
    Examples:
  
      // Allocate space for variables
  
      #define String1 LV0(0)      // String 1 size is maximum 5  characters + zero termination
      #define String2 LV0(6)      // String 2 size is maximum 14 characters + zero termination 
      #define Dummy   LV0(21)     // Next variable 


      // Initialise string variabel 1 with a string constant

        opSTRING,LC0(DUPLICATE),LCS,'L','E','G','O',0,String1,    

      // Initialise string variabel 2 with a another string constant

        opSTRING,LC0(DUPLICATE),LCS,'M','i','n','d','s','t','o','r','m','s',0,String2,
      
      // Preform SUBCALL with three string parameters
    
        opCALL,LC0(MYSUB),LC0(3),String1,String2,String2,
      



      // MYSUB byte codes (checks first string if it is "LEGO" and then adds second string to it)
    
      #define In1     LV0(0)      // String In1 size is maximum 20 characters + zero termination
      #define In2     LV0(21)     // String In2 size is maximum 20 characters + zero termination 
      #define Out     LV1(42)     // String Out size is maximum 20 characters + zero termination 
      #define Result  LV1(63)     // Result for compare 


        0x03,IN_S,(21),IN_S,(21),OUT_S,(21),                      //  MYSUB(In1,In2,Out)
                                                                  //  {
        opSTRING,LC0(COMPARE),In1,LCS,'L','E','G','O',0,Result,   //    STRING(COMPARE,In1,"LEGO",Result)
        opJR_FALSE,Result,LC0(6),                                 //    if (Result != 0)
                                                                  //    {
        opSTRING,LC0(ADD),In1,In2,Out,                            //      STRING(ADD,In1,In2,Out)
                                                                  //    }
        opRETURN,                                                 //  }
        opOBJECT_END,                                  

      // Numbers in parantheses is the string size allocated in the SUBCALLs local variables !


  CHARACTERS IN ARRAYS
  --------------------
    Strings can be allocated as DATA8 arrays and referenced by the array handle. Enough room must
    be ensured making the array so large than it can hold the maximal string length inclusive zero 
    termination (it is possible to resize an array manually).
  
    Examples:
  
      // Allocate space for variables (array handle must use long encoding for later handle encoding)
  
      #define String1 LV1(0)                                      // String 1 is an array handle
      #define String2 LV1(1)                                      // String 2 is an array handle
      
        opARRAY,LC0(CREATE8),LC0(100),String1,                    // Length: 100 characters (including zero termination)
        opARRAY,LC0(CREATE8),LC0(100),String2,                    // Length: 100 characters (including zero termination)
      
      
      // Initialise string variabel 1 with a string constant through system call 

        opSTRING,LC0(DUPLICATE),LCS,'L','E','G','O',0,HND(String1),    


      // Preform SUBCALL with Two array handles
    
        opCALL,LC0(MY_STRING_COPY),LC0(2),String1,String2,


      // Defining SUBCALL for array handle parameter
      
        #define In1     LV1(0)
        #define Out1    LV1(1)
      
        0x02,IN_8,IO_8,                                           //  MY_STRING_COPY (out handles must be in/out 8)
                                                                  //  {
        opSTRING,LC0(DUPLICATE),HND(In1),HND(Out1),               //    STRING(DUPLICATE,&In1,&Out1)
        opRETURN,                                                 //  }
        opOBJECT_END,                                  

Program Example


LEGO® Robotics Firmware Documentation
Confidential Information © 2013 The LEGO Group