Not eXactly C (NXC) Programmer's Guide Version 1.0.1 b33 October 10, 2007 by Jo

Not eXactly C (NXC) Programmer's Guide Version 1.0.1 b33 October 10, 2007 by John Hansen Page i Contents 1 Introduction................................................................................................................. 1 2 The NXC Language .................................................................................................... 2 2.1 Lexical Rules ...................................................................................................... 2 2.1.1 Comments ................................................................................................... 2 2.1.2 Whitespace.................................................................................................. 2 2.1.3 Numerical Constants................................................................................... 3 2.1.4 Identifiers and Keywords............................................................................ 3 2.2 Program Structure............................................................................................... 3 2.2.1 Tasks ........................................................................................................... 3 2.2.2 Functions..................................................................................................... 4 2.2.3 Variables ..................................................................................................... 6 2.2.4 Structs ......................................................................................................... 7 2.2.5 Arrays.......................................................................................................... 8 2.3 Statements........................................................................................................... 9 2.3.1 Variable Declaration................................................................................... 9 2.3.2 Assignment ............................................................................................... 10 2.3.3 Control Structures..................................................................................... 10 2.3.4 The asm Statement.................................................................................... 13 2.3.5 Other Statements....................................................................................... 14 2.4 Expressions ....................................................................................................... 15 2.4.1 Conditions................................................................................................. 16 2.5 The Preprocessor............................................................................................... 16 2.5.1 #include..................................................................................................... 16 2.5.2 #define....................................................................................................... 17 2.5.3 ## (Concatenation).................................................................................... 17 2.5.4 Conditional Compilation........................................................................... 17 3 NXC API................................................................................................................... 18 3.1 General Features ............................................................................................... 18 3.1.1 Timing Functions...................................................................................... 18 3.1.2 Program Control Functions....................................................................... 19 3.1.3 String Functions........................................................................................ 21 3.1.4 Array Functions ........................................................................................ 22 3.1.5 Numeric Functions.................................................................................... 23 3.1.6 Low-level System Functions..................................................................... 24 3.2 Input Module..................................................................................................... 42 3.2.1 Types and Modes...................................................................................... 43 3.2.2 Sensor Information.................................................................................... 46 3.2.3 IOMap Offsets .......................................................................................... 49 3.3 Output Module.................................................................................................. 49 3.3.1 Convenience Calls .................................................................................... 53 3.3.2 Primitive Calls .......................................................................................... 58 3.3.3 IOMap Offsets .......................................................................................... 60 3.4 IO Map Addresses............................................................................................. 61 3.5 Sound Module................................................................................................... 62 Page ii 3.5.1 High-level functions.................................................................................. 62 3.5.2 Low-level functions .................................................................................. 63 3.5.3 IOMap Offsets .......................................................................................... 65 3.6 IOCtrl Module................................................................................................... 66 3.6.1 IOMap Offsets .......................................................................................... 66 3.7 Display module................................................................................................. 66 3.7.1 High-level functions.................................................................................. 67 3.7.2 Low-level functions .................................................................................. 68 3.7.3 IOMap Offsets .......................................................................................... 70 3.8 Loader Module.................................................................................................. 70 3.8.1 IOMap Offsets .......................................................................................... 75 3.9 Command Module ............................................................................................ 75 3.9.1 IOMap Offsets .......................................................................................... 75 3.10 Button Module .................................................................................................. 75 3.10.1 High-level functions.................................................................................. 76 3.10.2 Low-level functions .................................................................................. 76 3.10.3 IOMap Offsets .......................................................................................... 78 3.11 UI Module......................................................................................................... 78 3.11.1 IOMap Offsets .......................................................................................... 82 3.12 LowSpeed Module............................................................................................ 83 3.12.1 High-level functions.................................................................................. 84 3.12.2 Low-level functions .................................................................................. 86 3.12.3 IOMap Offsets .......................................................................................... 90 3.13 Comm Module .................................................................................................. 90 3.13.1 High-level functions.................................................................................. 91 3.13.2 Low-level functions .................................................................................. 95 3.13.3 IOMap Offsets ........................................................................................ 105 3.14 HiTechnic API Functions ............................................................................... 106 3.15 Mindsensors API Functions............................................................................ 117 Page iii Tables Table 1. NXC Keywords..................................................................................................... 3 Table 2. Variable Types...................................................................................................... 6 Table 3. Operators............................................................................................................. 10 Table 4. ASM Keywords .................................................................................................. 14 Table 5. Expressions......................................................................................................... 15 Table 6. Conditions........................................................................................................... 16 Table 7. Input Module Constants...................................................................................... 42 Table 8. Sensor Type Constants........................................................................................ 43 Table 9. Sensor Mode Constants ...................................................................................... 43 Table 10. Sensor Configuration Constants ....................................................................... 44 Table 11. Sensor Field Constants...................................................................................... 44 Table 12. Input Module IOMap Offsets ........................................................................... 49 Table 13. Output Module Constants................................................................................. 49 Table 14. Output Field Constants ..................................................................................... 52 Table 15. UpdateFlag Constants....................................................................................... 53 Table 16. OutputMode Constants ..................................................................................... 53 Table 17. RunState Constants........................................................................................... 53 Table 18. RegMode Constants.......................................................................................... 53 Table 19. Reset Constants................................................................................................. 54 Table 20. Output Port Constants....................................................................................... 54 Table 21. Output Module IOMap Offsets......................................................................... 61 Table 22. IOMA Constants............................................................................................... 61 Table 23. Sound Module Constants.................................................................................. 62 Table 24. Sound Flags Constants...................................................................................... 63 Table 25. Sound State Constants ...................................................................................... 63 Table 26. Sound Mode Constants..................................................................................... 63 Table 27. Miscellaneous Sound Constants ....................................................................... 64 Table 28. Sound Module IOMap Offsets.......................................................................... 65 Table 29. IOCtrl Module Constants.................................................................................. 66 Table 30. IOCtrl Module IOMap Offsets ......................................................................... 66 Table 31. Display Module Constants................................................................................ 66 Table 32. Display Flags Constants.................................................................................... 68 Table 33. Display Module IOMap Offsets ....................................................................... 70 Table 34. Loader Module Constants................................................................................. 71 Table 35. Loader Result Codes......................................................................................... 71 Table 36. Loader Module IOMap Offsets......................................................................... 75 Table 37. Command Module Constants............................................................................ 75 Table 38. Command Module IOMap Offsets ................................................................... 75 Table 39. Button Module Constants ................................................................................. 75 Table 40. Button Constants............................................................................................... 76 Table 41. Button State Constants...................................................................................... 76 Table 42. Button Module IOMap Offsets......................................................................... 78 Table 43. UI Module Constants........................................................................................ 78 Table 44. UI Command Flags Constants.......................................................................... 79 Page iv Table 45. UI State Constants ............................................................................................ 79 Table 46. UI Button Constants.......................................................................................... 79 Table 47. UI Bluetooth State Constants............................................................................ 79 Table 48. UI Module IOMap Offsets................................................................................ 83 Table 49. LowSpeed Module Constants........................................................................... 83 Table 50. Lowspeed (I2C) Return Value Constants......................................................... 84 Table 51. Low Speed State Constants............................................................................... 86 Table 52. Low Speed Channel State Constants ................................................................ 86 Table 53. Low Speed Mode Constants ............................................................................. 86 Table 54. Low Speed Error Type Constants..................................................................... 87 Table 55. LowSpeed Module IOMap Offsets................................................................... 90 Table 56. Comm Module Constants ................................................................................. 90 Table 57. Comm Miscellaneous Constants....................................................................... 95 Table 58. Comm BtState Constants.................................................................................. 96 Table 59. Comm BtStateStatus Constants ........................................................................ 96 Table 60. Comm BtHwStatus Constants .......................................................................... 96 Table 61. Comm HsFlags Constants................................................................................. 96 Table 62. Comm HsState Constants ................................................................................. 96 Table 63. Comm DeviceStatus Constants......................................................................... 96 Table 64. Comm Module Interface Constants.................................................................. 97 Table 65. Comm Module IOMap Offsets....................................................................... 106 NXC Programmer's Guide Page 1 1 Introduction NXC stands for Not eXactly C. It is a simple language for programming the LEGO MINDSTORMS NXT product. The NXT has a bytecode interpreter (provided by LEGO), which can be used to execute programs. The NXC compiler translates a source program into NXT bytecodes, which can then be executed on the target itself. Although the preprocessor and control structures of NXC are very similar to C, NXC is not a general-purpose programming language - there are many restrictions that stem from limitations of the NXT bytecode interpreter. Logically, NXC is defined as two separate pieces. The NXC language describes the syntax to be used in writing programs. The NXC Application Programming Interface (API) describes the system functions, constants, and macros that can be used by programs. This API is defined in a special file known as a "header file" which is, by default, automatically included when compiling a program. This document describes both the NXC language and the NXC API. In short, it provides the information needed to write NXC programs. Since there are different interfaces for NXC, this document does not describe how to use any specific NXC implementation (such as the command-line compiler or Bricx Command Center). Refer to the documentation provided with the NXC tool, such as the NXC User Manual, for information specific to that implementation. For up-to-date information and documentation for NXC, visit the NXC website at http://bricxcc.sourceforge.net/nxc/. NXC Programmer's Guide Page 2 2 The NXC Language This section describes the NXC language itself. This includes the lexical rules used by the compiler, the structure programs, statements, and expressions, and the operation of the preprocessor. NXC is a case-sensitive language just like C and C++. That means that the identifier "xYz" is not the same identifier as "Xyz". Similarly, the "if" statement begins with the keyword "if" but "iF", "If", or "IF" are all just valid identifiers – not keywords. 2.1 Lexical Rules The lexical rules describe how NXC breaks a source file into individual tokens. This includes the way comments are written, the handling of whitespace, and valid characters for identifiers. 2.1.1 Comments Two forms of comments are supported in NXC. The first form (traditional C comments) begin with /* and end with */. They may span multiple lines, but do not nest: /* this is a comment */ /* this is a two line comment */ /* another comment... /* trying to nest... ending the inner comment...*/ this text is no longer a comment! */ The second form of comments begins with // and ends with a newline (sometimes known as C++ style comments). // a single line comment The compiler ignores comments. Their only purpose is to allow the programmer to document the source code. 2.1.2 Whitespace Whitespace (spaces, tabs, and newlines) is used to separate tokens and to make programs more readable. As long as the tokens are distinguishable, adding or subtracting whitespace has no effect on the meaning of a program. For example, the following lines of code both have the same meaning: x=2; x = 2 ; Some of the C++ operators consist of multiple characters. In order to preserve these tokens whitespace must not be inserted within them. In the example below, the first line NXC Programmer's Guide Page 3 uses a right shift operator ('>>'), but in the second line the added space causes the '>' symbols to be interpreted as two separate tokens and thus generate an error. x = 1 >> 4; // set x to 1 right shifted by 4 bits x = 1 > > 4; // error 2.1.3 Numerical Constants Numerical constants may be written in either decimal or hexadecimal form. Decimal constants consist of one or more decimal digits. Hexadecimal constants start with 0x or 0X followed by one or more hexadecimal digits. x = 10; // set x to 10 x = 0x10; // set x to 16 (10 hex) 2.1.4 Identifiers and Keywords Identifiers are used for variable, task, function, and subroutine names. The first character of an identifier must be an upper or lower case letter or the underscore ('_'). Remaining characters may be letters, numbers, and an underscore. A number of potential identifiers are reserved for use in the NXC language itself. These reserved words are call keywords and may not be used as identifiers. A complete list of keywords appears below: __RETURN__ __RETVAL__ __STRRETVAL__ __TMPBYTE__ __TMPWORD__ __TMPLONG__ abs asm bool uploads/Geographie/ nxc-guide.pdf

  • 14
  • 0
  • 0
Afficher les détails des licences
Licence et utilisation
Gratuit pour un usage personnel Attribution requise
Partager