Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.20 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import os
  4.  
  5. import serial
  6. import serial.tools.list_ports
  7. import time
  8. import array as arr
  9. import pygame
  10. from pygame.locals import *
  11. from pygame.compat import as_bytes
  12. BytesIO = pygame.compat.get_BytesIO()
  13.  
  14. OS = 'ios'
  15.  
  16. # tty for Bluetooth device with baud
  17. SERIAL_TERMINAL = '/dev/ttyUSB0'
  18. #Use AT+BAUDRATE=115200 but make sure hardware flow control CTS/RTS works
  19. BAUD = 115200
  20.  
  21. # Translate keycodes from pygame to USB HID codes
  22. # uint8_t HIDCode[512]
  23. HIDCode = arr.array('B', [
  24. 0, # 0
  25. 0, # 1
  26. 0, # 2
  27. 0, # 3
  28. 0, # 4
  29. 0, # 5
  30. 0, # 6
  31. 0, # 7
  32. 0x2A, # 8
  33. 0x2B, # 9
  34. 0, # 10
  35. 0, # 11
  36. 0, # 12
  37. 0, # 13
  38. 0, # 14
  39. 0, # 15
  40. 0, # 16
  41. 0, # 17
  42. 0, # 18
  43. 0x48, # 19
  44. 0, # 20
  45. 0, # 21
  46. 0, # 22
  47. 0, # 23
  48. 0, # 24
  49. 0, # 25
  50. 0, # 26
  51. 0x29, # 27
  52. 0, # 28
  53. 0, # 29
  54. 0, # 30
  55. 0, # 31
  56. 0x2C, # 32
  57. 0, # 33
  58. 0, # 34
  59. 0, # 35
  60. 0, # 36
  61. 0, # 37
  62. 0, # 38
  63. 0x34, # 39
  64. 0, # 40
  65. 0, # 41
  66. 0, # 42
  67. 0, # 43
  68. 0x36, # 44
  69. 0x2D, # 45
  70. 0x37, # 46
  71. 0x38, # 47
  72. 39, # 48
  73. 30, # 49
  74. 31, # 50
  75. 32, # 51
  76. 33, # 52
  77. 34, # 53
  78. 35, # 54
  79. 36, # 55
  80. 37, # 56
  81. 38, # 57
  82. 0, # 58
  83. 0x33, # 59
  84. 0, # 60
  85. 0x2E, # 61
  86. 0, # 62
  87. 0, # 63
  88. 0, # 64
  89. 0, # 65
  90. 0, # 66
  91. 0, # 67
  92. 0, # 68
  93. 0, # 69
  94. 0, # 70
  95. 0, # 71
  96. 0, # 72
  97. 0, # 73
  98. 0, # 74
  99. 0, # 75
  100. 0, # 76
  101. 0, # 77
  102. 0, # 78
  103. 0, # 79
  104. 0, # 80
  105. 0, # 81
  106. 0, # 82
  107. 0, # 83
  108. 0, # 84
  109. 0, # 85
  110. 0, # 86
  111. 0, # 87
  112. 0, # 88
  113. 0, # 89
  114. 0, # 90
  115. 0x2F, # 91
  116. 0x31, # 92
  117. 0x30, # 93
  118. 0, # 94
  119. 0, # 95
  120. 0x35, # 96
  121. 4, # 97
  122. 5, # 98
  123. 6, # 99
  124. 7, # 100
  125. 8, # 101
  126. 9, # 102
  127. 10, # 103
  128. 11, # 104
  129. 12, # 105
  130. 13, # 106
  131. 14, # 107
  132. 15, # 108
  133. 16, # 109
  134. 17, # 110
  135. 18, # 111
  136. 19, # 112
  137. 20, # 113
  138. 21, # 114
  139. 22, # 115
  140. 23, # 116
  141. 24, # 117
  142. 25, # 118
  143. 26, # 119
  144. 27, # 120
  145. 28, # 121
  146. 29, # 122
  147. 0, # 123
  148. 0, # 124
  149. 0, # 125
  150. 0, # 126
  151. 0x4C, # 127
  152. 0, # 128
  153. 0, # 129
  154. 0, # 130
  155. 0, # 131
  156. 0, # 132
  157. 0, # 133
  158. 0, # 134
  159. 0, # 135
  160. 0, # 136
  161. 0, # 137
  162. 0, # 138
  163. 0, # 139
  164. 0, # 140
  165. 0, # 141
  166. 0, # 142
  167. 0, # 143
  168. 0, # 144
  169. 0, # 145
  170. 0, # 146
  171. 0, # 147
  172. 0, # 148
  173. 0, # 149
  174. 0, # 150
  175. 0, # 151
  176. 0, # 152
  177. 0, # 153
  178. 0, # 154
  179. 0, # 155
  180. 0, # 156
  181. 0, # 157
  182. 0, # 158
  183. 0, # 159
  184. 0, # 160
  185. 0, # 161
  186. 0, # 162
  187. 0, # 163
  188. 0, # 164
  189. 0, # 165
  190. 0, # 166
  191. 0, # 167
  192. 0, # 168
  193. 0, # 169
  194. 0, # 170
  195. 0, # 171
  196. 0, # 172
  197. 0, # 173
  198. 0, # 174
  199. 0, # 175
  200. 0, # 176
  201. 0, # 177
  202. 0, # 178
  203. 0, # 179
  204. 0, # 180
  205. 0, # 181
  206. 0, # 182
  207. 0, # 183
  208. 0, # 184
  209. 0, # 185
  210. 0, # 186
  211. 0, # 187
  212. 0, # 188
  213. 0, # 189
  214. 0, # 190
  215. 0, # 191
  216. 0, # 192
  217. 0, # 193
  218. 0, # 194
  219. 0, # 195
  220. 0, # 196
  221. 0, # 197
  222. 0, # 198
  223. 0, # 199
  224. 0, # 200
  225. 0, # 201
  226. 0, # 202
  227. 0, # 203
  228. 0, # 204
  229. 0, # 205
  230. 0, # 206
  231. 0, # 207
  232. 0, # 208
  233. 0, # 209
  234. 0, # 210
  235. 0, # 211
  236. 0, # 212
  237. 0, # 213
  238. 0, # 214
  239. 0, # 215
  240. 0, # 216
  241. 0, # 217
  242. 0, # 218
  243. 0, # 219
  244. 0, # 220
  245. 0, # 221
  246. 0, # 222
  247. 0, # 223
  248. 0, # 224
  249. 0, # 225
  250. 0, # 226
  251. 0, # 227
  252. 0, # 228
  253. 0, # 229
  254. 0, # 230
  255. 0, # 231
  256. 0, # 232
  257. 0, # 233
  258. 0, # 234
  259. 0, # 235
  260. 0, # 236
  261. 0, # 237
  262. 0, # 238
  263. 0, # 239
  264. 0, # 240
  265. 0, # 241
  266. 0, # 242
  267. 0, # 243
  268. 0, # 244
  269. 0, # 245
  270. 0, # 246
  271. 0, # 247
  272. 0, # 248
  273. 0, # 249
  274. 0, # 250
  275. 0, # 251
  276. 0, # 252
  277. 0, # 253
  278. 0, # 254
  279. 0, # 255
  280. 0x62, # 256
  281. 0x59, # 257
  282. 0x5A, # 258
  283. 0x5B, # 259
  284. 0x5C, # 260
  285. 0x5D, # 261
  286. 0x5E, # 262
  287. 0x5F, # 263
  288. 0x60, # 264
  289. 0x61, # 265
  290. 0x63, # 266
  291. 0x54, # 267
  292. 0x55, # 268
  293. 0x56, # 269
  294. 0x57, # 270
  295. 0x58, # 271
  296. 0, # 272
  297. 0x52, # 273
  298. 0x51, # 274
  299. 0x4F, # 275
  300. 0x50, # 276
  301. 0x49, # 277
  302. 0x4A, # 278
  303. 0x4D, # 279
  304. 0x4B, # 280
  305. 0x4E, # 281
  306. 58, # 282
  307. 59, # 283
  308. 60, # 284
  309. 61, # 285
  310. 62, # 286
  311. 63, # 287
  312. 64, # 288
  313. 65, # 289
  314. 66, # 290
  315. 67, # 291
  316. 68, # 292
  317. 69, # 293
  318. 0, # 294
  319. 0, # 295
  320. 0, # 296
  321. 0, # 297
  322. 0, # 298
  323. 0, # 299
  324. 0x53, # 300
  325. 0x39, # 301
  326. 0x47, # 302
  327. 0, # 303
  328. 0, # 304
  329. 0, # 305
  330. 0, # 306
  331. 0, # 307
  332. 0, # 308
  333. 0, # 309
  334. 0, # 310
  335. 0, # 311
  336. 0, # 312
  337. 0, # 313
  338. 0, # 314
  339. 0, # 315
  340. 0, # 316
  341. 0, # 317
  342. 0, # 318
  343. 0, # 319
  344. 0, # 320
  345. 0, # 321
  346. 0, # 322
  347. 0, # 323
  348. 0, # 324
  349. 0, # 325
  350. 0, # 326
  351. 0, # 327
  352. 0, # 328
  353. 0, # 329
  354. 0, # 330
  355. 0, # 331
  356. 0, # 332
  357. 0, # 333
  358. 0, # 334
  359. 0, # 335
  360. 0, # 336
  361. 0, # 337
  362. 0, # 338
  363. 0, # 339
  364. 0, # 340
  365. 0, # 341
  366. 0, # 342
  367. 0, # 343
  368. 0, # 344
  369. 0, # 345
  370. 0, # 346
  371. 0, # 347
  372. 0, # 348
  373. 0, # 349
  374. 0, # 350
  375. 0, # 351
  376. 0, # 352
  377. 0, # 353
  378. 0, # 354
  379. 0, # 355
  380. 0, # 356
  381. 0, # 357
  382. 0, # 358
  383. 0, # 359
  384. 0, # 360
  385. 0, # 361
  386. 0, # 362
  387. 0, # 363
  388. 0, # 364
  389. 0, # 365
  390. 0, # 366
  391. 0, # 367
  392. 0, # 368
  393. 0, # 369
  394. 0, # 370
  395. 0, # 371
  396. 0, # 372
  397. 0, # 373
  398. 0, # 374
  399. 0, # 375
  400. 0, # 376
  401. 0, # 377
  402. 0, # 378
  403. 0, # 379
  404. 0, # 380
  405. 0, # 381
  406. 0, # 382
  407. 0, # 383
  408. 0, # 384
  409. 0, # 385
  410. 0, # 386
  411. 0, # 387
  412. 0, # 388
  413. 0, # 389
  414. 0, # 390
  415. 0, # 391
  416. 0, # 392
  417. 0, # 393
  418. 0, # 394
  419. 0, # 395
  420. 0, # 396
  421. 0, # 397
  422. 0, # 398
  423. 0, # 399
  424. 0, # 400
  425. 0, # 401
  426. 0, # 402
  427. 0, # 403
  428. 0, # 404
  429. 0, # 405
  430. 0, # 406
  431. 0, # 407
  432. 0, # 408
  433. 0, # 409
  434. 0, # 410
  435. 0, # 411
  436. 0, # 412
  437. 0, # 413
  438. 0, # 414
  439. 0, # 415
  440. 0, # 416
  441. 0, # 417
  442. 0, # 418
  443. 0, # 419
  444. 0, # 420
  445. 0, # 421
  446. 0, # 422
  447. 0, # 423
  448. 0, # 424
  449. 0, # 425
  450. 0, # 426
  451. 0, # 427
  452. 0, # 428
  453. 0, # 429
  454. 0, # 430
  455. 0, # 431
  456. 0, # 432
  457. 0, # 433
  458. 0, # 434
  459. 0, # 435
  460. 0, # 436
  461. 0, # 437
  462. 0, # 438
  463. 0, # 439
  464. 0, # 440
  465. 0, # 441
  466. 0, # 442
  467. 0, # 443
  468. 0, # 444
  469. 0, # 445
  470. 0, # 446
  471. 0, # 447
  472. 0, # 448
  473. 0, # 449
  474. 0, # 450
  475. 0, # 451
  476. 0, # 452
  477. 0, # 453
  478. 0, # 454
  479. 0, # 455
  480. 0, # 456
  481. 0, # 457
  482. 0, # 458
  483. 0, # 459
  484. 0, # 460
  485. 0, # 461
  486. 0, # 462
  487. 0, # 463
  488. 0, # 464
  489. 0, # 465
  490. 0, # 466
  491. 0, # 467
  492. 0, # 468
  493. 0, # 469
  494. 0, # 470
  495. 0, # 471
  496. 0, # 472
  497. 0, # 473
  498. 0, # 474
  499. 0, # 475
  500. 0, # 476
  501. 0, # 477
  502. 0, # 478
  503. 0, # 479
  504. 0, # 480
  505. 0, # 481
  506. 0, # 482
  507. 0, # 483
  508. 0, # 484
  509. 0, # 485
  510. 0, # 486
  511. 0, # 487
  512. 0, # 488
  513. 0, # 489
  514. 0, # 490
  515. 0, # 491
  516. 0, # 492
  517. 0, # 493
  518. 0, # 494
  519. 0, # 495
  520. 0, # 496
  521. 0, # 497
  522. 0, # 498
  523. 0, # 499
  524. 0, # 500
  525. 0, # 501
  526. 0, # 502
  527. 0, # 503
  528. 0, # 504
  529. 0, # 505
  530. 0, # 506
  531. 0, # 507
  532. 0, # 508
  533. 0, # 509
  534. 0, # 510
  535. 0 # 511
  536. ])
  537.  
  538. # Six keys for USB keyboard HID report
  539. # uint8_t keys[6] = {0,0,0,0,0,0}
  540. keys = arr.array('B', [0, 0, 0, 0, 0, 0])
  541.  
  542. def mkHID(keycode, modifiers, down):
  543. # print (keycode, modifiers)
  544. hidmod = 0
  545. if modifiers & 0x0040:
  546. hidmod = hidmod | 0x01
  547. if modifiers & 0x0001:
  548. hidmod = hidmod | 0x02
  549. if modifiers & 0x0100:
  550. hidmod = hidmod | 0x04
  551. if modifiers & 0x0400:
  552. hidmod = hidmod | 0x08
  553. if modifiers & 0x0080:
  554. hidmod = hidmod | 0x10
  555. if modifiers & 0x0002:
  556. hidmod = hidmod | 0x20
  557. if modifiers & 0x0200:
  558. hidmod = hidmod | 0x40
  559. if modifiers & 0x0800:
  560. hidmod = hidmod | 0x80
  561. hidcode = HIDCode[keycode]
  562. if OS == 'ios' and keycode == 13:
  563. hidcode = 0x58
  564. # print (hidcode, hidmod)
  565. for i in range(0, 6):
  566. if keys[i] == 0:
  567. if down == True:
  568. keys[i] = hidcode
  569. break
  570. elif keys[i] == hidcode:
  571. if down == False:
  572. keys[i] = 0
  573. else:
  574. break
  575. atcmd = "AT+BLEKEYBOARDCODE={:02x}-00".format(hidmod)
  576. zerocmd = ""
  577. for i in range(0, 6):
  578. if keys[i] != 0:
  579. atcmd += "-{:02x}".format(keys[i])
  580. else:
  581. zerocmd += "-00"
  582. atcmd += zerocmd + "\r"
  583. # print (atcmd)
  584. ser.write(atcmd.encode());
  585.  
  586. pygame.init ()
  587. screen = pygame.display.set_mode ((200, 200))
  588. c = pygame.time.Clock ()
  589. going = True
  590.  
  591. # Look for Adafruit CP2104 break out board or Feather nRF52. Use the first
  592. # one found. Default is /dev/ttyUSB0
  593. for p in serial.tools.list_ports.comports():
  594. if "CP2104" in p.description:
  595. print(p)
  596. SERIAL_TERMINAL = p.device
  597. break
  598. elif "nRF52" in p.description:
  599. print(p)
  600. SERIAL_TERMINAL = p.device
  601. break
  602.  
  603. with serial.Serial(SERIAL_TERMINAL, BAUD, rtscts=1) as ser:
  604. # TODO check for OK or ERROR
  605. ser.write("AT\r".encode())
  606. time.sleep(1)
  607. ser.flushInput()
  608. # ser.write("ATI\r".encode())
  609. # time.sleep(1)
  610. ser.write("ATE=0\r".encode())
  611. time.sleep(1)
  612. ser.flushInput()
  613. ser.write("AT+BLEHIDEN=1\r".encode())
  614. time.sleep(1)
  615. ser.flushInput()
  616. ser.write("ATZ\r".encode())
  617. while going:
  618. for e in pygame.event.get ():
  619. # print (e)
  620. if e.type == QUIT:
  621. going = False
  622.  
  623. elif e.type == KEYDOWN:
  624. mkHID(e.key, e.mod, True)
  625.  
  626. elif e.type == KEYUP:
  627. mkHID(e.key, e.mod, False)
  628.  
  629. ser.flushInput()
  630. pygame.display.flip()
  631. c.tick(40)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement