<commit_before># TODO - rethink this approach
charMap = {
  a:      4
  b:      5
  c:      6
  d:      7
  e:      8
  f:      9
  g:      10
  h:      11
  i:      12
  j:      13
  k:      14
  l:      15
  m:      16
  n:      17
  o:      18
  p:      19
  q:      20
  r:      21
  s:      22
  t:      23
  u:      24
  v:      25
  w:      26
  x:      27
  y:      28
  z:      29
  ' ':    44  # Space
  right:  79  # Right
  left:   80  # Left
  down:   81  # Down
  up:     82  # Up
  enter:  88  # Return
  back:   42  # Backspace
}

class DeviceModel extends Backbone.Model

  connect: ->
    window.device = @ # TODO - remove
    Backbone.Radio.channel('bluetooth').request('connect', @)

  disconnect: ->
    Backbone.Radio.channel('bluetooth').request('disconnect', @)

  isConnected: ->
    Backbone.Radio.channel('bluetooth').request('is:connected', @)

  readRSSI: =>
    Backbone.Radio.channel('bluetooth').request('read:rssi', @)

  remember: ->
    Backbone.Radio.channel('known:device').trigger('add', @)

  forget: ->
    Backbone.Radio.channel('known:device').trigger('remove', @)

  writeFast: (data) => # TODO - use BB.Radio
    packet = new Uint8Array(data).buffer
    ble.writeWithoutResponse(window.device.id, "FFE0", "FFE1", packet)

  writeKeyup: =>
    @writeFast([2,0,0,0,0,0,0])

  writeKeydown: (char) =>
    @writeFast([2,charMap[char],0,0,0,0,0])

  clickMouseLeft: =>
    @writeFast([1,0,0,1])
    @writeFast([1,0,0,0])

  clickMouseRight: =>
    @writeFast([1,0,0,2])
    @writeFast([1,0,0,0])

  writeChar: (char) =>
    @writeKeydown(char)
    @writeKeyup()

  writeMousePos: (pos) =>
    @writeFast([1,pos.x,pos.y,0])

  sendText: (text = 'lyrebird') =>
    for char in text
      @writeFast([2,charMap[char],0,0,0,0,0])
      @writeFast([2,0,0,0,0,0,0])

    return true

# # # # #

module.exports = DeviceModel<commit_msg>Cleaned up device model.sendText() method<commit_after># TODO - rethink this approach
charMap = {
  a:      4
  b:      5
  c:      6
  d:      7
  e:      8
  f:      9
  g:      10
  h:      11
  i:      12
  j:      13
  k:      14
  l:      15
  m:      16
  n:      17
  o:      18
  p:      19
  q:      20
  r:      21
  s:      22
  t:      23
  u:      24
  v:      25
  w:      26
  x:      27
  y:      28
  z:      29
  ' ':    44  # Space
  right:  79  # Right
  left:   80  # Left
  down:   81  # Down
  up:     82  # Up
  enter:  88  # Return
  back:   42  # Backspace
}

class DeviceModel extends Backbone.Model

  connect: ->
    window.device = @ # TODO - remove
    Backbone.Radio.channel('bluetooth').request('connect', @)

  disconnect: ->
    Backbone.Radio.channel('bluetooth').request('disconnect', @)

  isConnected: ->
    Backbone.Radio.channel('bluetooth').request('is:connected', @)

  readRSSI: =>
    Backbone.Radio.channel('bluetooth').request('read:rssi', @)

  remember: ->
    Backbone.Radio.channel('known:device').trigger('add', @)

  forget: ->
    Backbone.Radio.channel('known:device').trigger('remove', @)

  writeFast: (data) => # TODO - use BB.Radio
    packet = new Uint8Array(data).buffer
    ble.writeWithoutResponse(window.device.id, "FFE0", "FFE1", packet)

  writeKeyup: =>
    @writeFast([2,0,0,0,0,0,0])

  writeKeydown: (char) =>
    @writeFast([2,charMap[char],0,0,0,0,0])

  clickMouseLeft: =>
    @writeFast([1,0,0,1])
    @writeFast([1,0,0,0])

  clickMouseRight: =>
    @writeFast([1,0,0,2])
    @writeFast([1,0,0,0])

  writeChar: (char) =>
    @writeKeydown(char)
    @writeKeyup()

  writeMousePos: (pos) =>
    @writeFast([1,pos.x,pos.y,0])

  sendText: (text = 'lyrebird') =>
    @writeChar(char) for char in text
    return true

# # # # #

module.exports = DeviceModel
{'fraction_non_alphanumeric': 0.1654054054054054, 'fraction_numerical': 0.054324324324324325, 'mean_word_length': 2.2238675958188154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4038209', 'n_tokens_mistral': 1528, 'n_tokens_neox': 1444, 'n_words': 282}