common.bmx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. ' Copyright (c) .NET Foundation and Contributors
  2. ' Copyright (c) 2019 Bruce A Henderson
  3. '
  4. ' All rights reserved.
  5. '
  6. ' Permission is hereby granted, free of charge, to any person obtaining a copy
  7. ' of this software and associated documentation files (the "Software"), to deal
  8. ' in the Software without restriction, including without limitation the rights
  9. ' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. ' copies of the Software, and to permit persons to whom the Software is
  11. ' furnished to do so, subject to the following conditions:
  12. '
  13. ' The above copyright notice and this permission notice shall be included in all
  14. ' copies or substantial portions of the Software.
  15. '
  16. ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. ' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. ' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. ' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. ' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. ' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. ' SOFTWARE.
  23. '
  24. SuperStrict
  25. Import pub.stdc
  26. Import brl.event
  27. Import "glue.c"
  28. Rem
  29. bbdoc: Pin modes supported by the GPIO controllers and drivers.
  30. End Rem
  31. Enum EPinMode
  32. Input
  33. Output
  34. InputPullDown
  35. InputPullUp
  36. End Enum
  37. Rem
  38. bbdoc: Different numbering schemes supported by GPIO controllers and drivers.
  39. End Rem
  40. Enum EPinNumberingScheme
  41. Logical
  42. Board
  43. End Enum
  44. Rem
  45. bbdoc: Event types that can be triggered by the GPIO.
  46. End Rem
  47. Enum EPinEventTypes Flags
  48. None = 0
  49. Rising = 1
  50. Falling = 2
  51. End Enum
  52. Rem
  53. bbdoc: Represents a value for a pin.
  54. End Rem
  55. Enum EpinValue
  56. Low = 0
  57. High = 1
  58. End Enum
  59. Rem
  60. bbdoc: A motion started event.
  61. End Rem
  62. Global MOTION_START_EVENT:Int = AllocUserEventId("Motion started")
  63. Rem
  64. bbdoc: A motion stopped event.
  65. End Rem
  66. Global MOTION_STOP_EVENT:Int = AllocUserEventId("Motion stopped")
  67. Function defaultcomparator_compare:Int(a:EpinValue, b:EpinValue )
  68. Return a.Ordinal() - b.Ordinal()
  69. End Function
  70. Type TIntArrayList
  71. Field data:Int[0]
  72. Field size:Int
  73. Method New(initialCapacity:Int)
  74. data = New Int[initialCapacity]
  75. End Method
  76. Method Add(value:Int)
  77. CheckAndResize()
  78. data[size] = value
  79. size :+ 1
  80. End Method
  81. Method CheckAndResize()
  82. If size = data.length Then
  83. Local newSize:Int = size + 1 + size * (2/3.0)
  84. data = data[..newSize]
  85. End If
  86. End Method
  87. Method Operator[]:Int(index:Int)
  88. Return data[index]
  89. End Method
  90. Method Remove(value:Int)
  91. For Local i:Int = 0 Until size
  92. If data[i] = value Then
  93. data = data[..i] + data[i+1..]
  94. Exit
  95. End If
  96. Next
  97. End Method
  98. End Type
  99. Rem
  100. bbdoc: General Iot exception.
  101. End Rem
  102. Type TIotException Extends TBlitzException
  103. Field message:String
  104. Method ToString:String() Override
  105. Return message
  106. End Method
  107. End Type
  108. Type TPlatformNotSupportedException Extends TIotException
  109. Method New()
  110. message = "Platform not supported"
  111. End Method
  112. Method New(message:String)
  113. Self.message = message
  114. End Method
  115. End Type
  116. Type TArgumentException Extends TIotException
  117. Method New(message:String)
  118. Self.message = message
  119. End Method
  120. End Type
  121. Type TUnauthorizedAccessException Extends TIotException
  122. Method New(message:String)
  123. Self.message = message
  124. End Method
  125. End Type
  126. Type TInvalidOperationException Extends TIotException
  127. Method New(message:String)
  128. Self.message = message
  129. End Method
  130. End Type
  131. Type TArgumentOutOfRangeException Extends TIotException
  132. Method New(message:String)
  133. Self.message = message
  134. End Method
  135. End Type
  136. Type TArgumentNullException Extends TIotException
  137. Method New(message:String)
  138. Self.message = message
  139. End Method
  140. End Type
  141. Type TIOException Extends TIotException
  142. Method New(message:String)
  143. Self.message = message
  144. End Method
  145. End Type
  146. Type TNotImplementedException Extends TIotException
  147. End Type
  148. Type TNotSupportedException Extends TIotException
  149. Method New(message:String)
  150. Self.message = message
  151. End Method
  152. End Type
  153. Enum EI2cFunctionalityFlags Flags
  154. NONE = 0
  155. I2C_FUNC_I2C = $00000001
  156. I2C_FUNC_SMBUS_BLOCK_DATA = $03000000
  157. End Enum
  158. Enum EI2cSettings:UInt
  159. I2C_FUNCS = $0705
  160. I2C_SLAVE_FORCE = $0706
  161. I2C_RDWR = $0707
  162. I2C_SMBUS = $0720
  163. End Enum
  164. Enum EI2cMessageFlags:Short Flags
  165. I2C_M_WR = $0000
  166. I2C_M_RD = $0001
  167. I2C_M_TEN = $0010
  168. I2C_M_RECV_LEN = $0400
  169. I2C_M_NO_RD_ACK = $0800
  170. I2C_M_IGNORE_NAK = $1000
  171. I2C_M_REV_DIR_ADDR = $2000
  172. I2C_M_NOSTART = $4000
  173. End Enum
  174. Enum ESpiSettings:UInt
  175. SPI_IOC_WR_MODE = $40016b01
  176. SPI_IOC_RD_MODE = $80016b01
  177. SPI_IOC_WR_BITS_PER_WORD = $40016b03
  178. SPI_IOC_RD_BITS_PER_WORD = $80016b03
  179. SPI_IOC_WR_MAX_SPEED_HZ = $40046b04
  180. SPI_IOC_RD_MAX_SPEED_HZ = $80046b04
  181. End Enum
  182. Enum ESpiMode Flags
  183. None = $00
  184. SPI_CPHA = $01
  185. SPI_CPOL = $02
  186. SPI_CS_HIGH = $04
  187. SPI_LSB_FIRST = $08
  188. SPI_3WIRE = $10
  189. SPI_LOOP = $20
  190. SPI_NO_CS = $40
  191. SPI_READY = $80
  192. SPI_MODE_0 = None
  193. SPI_MODE_1 = SPI_CPHA
  194. SPI_MODE_2 = SPI_CPOL
  195. SPI_MODE_3 = SPI_CPOL | SPI_CPHA
  196. End Enum
  197. Struct i2c_msg
  198. Field addr:Short
  199. Field flags:Short
  200. Field length:Short
  201. Field buf:Byte Ptr
  202. Method New(addr:Short, flags:Short, length:Short, buf:Byte Ptr)
  203. Self.addr = addr
  204. Self.flags = flags
  205. Self.length = length
  206. Self.buf = buf
  207. End Method
  208. End Struct
  209. Struct i2c_rdwr_ioctl_data
  210. Field msgs:Byte Ptr
  211. Field nmsgs:UInt
  212. Method New(msgs:Byte Ptr, nmsgs:UInt)
  213. Self.msgs = msgs
  214. Self.nmsgs = nmsgs
  215. End Method
  216. End Struct
  217. Const O_RDWR:Int = $0002
  218. Struct spi_ioc_transfer
  219. Field tx_buf:Byte Ptr
  220. Field rx_buf:Byte Ptr
  221. Field length:UInt
  222. Field speed_hz:UInt
  223. Field delay_usecs:Short
  224. Field bits_per_word:Byte
  225. Field cs_change:Byte
  226. Field pad:UInt
  227. Method New(tx_buf:Byte Ptr, rx_buf:Byte Ptr, length:UInt, speed_hz:UInt, bits_per_word:Byte, delay_usecs:Short)
  228. Self.tx_buf = tx_buf
  229. Self.rx_buf = rx_buf
  230. Self.length = length
  231. Self.speed_hz = speed_hz
  232. Self.bits_per_word = bits_per_word
  233. Self.delay_usecs = delay_usecs
  234. End Method
  235. End Struct
  236. Enum EDataFlow
  237. MsbFirst
  238. LsbFirst
  239. End Enum
  240. Extern
  241. Function open_:Int(path:String, flags:Int)
  242. Function ioctl_:Int(fd:Int, request:UInt, data:Byte Ptr)'="void ioctl_(int, unsigned int, int *)!"
  243. Function ioctli_:Int(fd:Int, request:UInt, data:Int)="ioctli_"
  244. Function ioctl_:Int(fd:Int, request:UInt, data:i2c_rdwr_ioctl_data Var)="void ioctl_(int, unsigned int, int *)!"
  245. Function ioctl_:Int(fd:Int, request:UInt, data:spi_ioc_transfer Var)="void ioctl_(int, unsigned int, int *)!"
  246. Function ioctli2_:Int(fd:Int, request:UInt, data:EI2cFunctionalityFlags Var)="void ioctl_(int, unsigned int, int *)!"
  247. Function ioctlsp_:Int(fd:Int, request:UInt, data:ESpiMode Var)="void ioctl_(int, unsigned int, int *)!"
  248. Function write_:Long(fd:Int, buf:Byte Ptr, count:Size_T)
  249. Function read_:Long(fd:Int, buf:Byte Ptr, count:Size_T)
  250. Function close_(fd:Int)="close"
  251. End Extern
  252. Struct SPinValuePair
  253. Field pinNumber:Int
  254. Field pinValue:EPinValue
  255. Method New(pinNumber:Int, pinValue:EPinValue)
  256. Self.pinNumber = pinNumber
  257. Self.pinValue = pinValue
  258. End Method
  259. Method New(pinNumber:Int, pinValue:Int)
  260. Self.pinNumber = pinNumber
  261. If pinValue = 0 Then
  262. Self.pinValue = EPinValue.Low
  263. Else
  264. Self.pinValue = EpinValue.High
  265. End If
  266. End Method
  267. End Struct
  268. Struct SPinVector32
  269. Field pins:UInt
  270. Field values:UInt
  271. Method New(pins:UInt, values:UInt)
  272. Self.pins = pins
  273. Self.values = values
  274. End Method
  275. Method New(pinValues:SPinValuePair[])
  276. pins = 0
  277. values = 0
  278. For Local i:Int = 0 Until pinValues.length
  279. Local pin:Int = pinValues[i].pinNumber
  280. If pin < 0 Or pin >= 4 * 8 Then
  281. Throw New TArgumentOutOfRangeException("pinValues")
  282. End If
  283. Local bit:UInt = 1 Shl pin
  284. pins :| bit
  285. If pinValues[i].pinValue = EPinValue.High Then
  286. values :| bit
  287. End If
  288. Next
  289. End Method
  290. End Struct
  291. Function DelayMicroseconds(microseconds:Int, allowThreadYield:Int)
  292. End Function