Skip to content

Commit

Permalink
read in priority words from external file
Browse files Browse the repository at this point in the history
  • Loading branch information
dupontgu committed May 13, 2021
1 parent c9e8acd commit 9ca1a42
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
4 changes: 2 additions & 2 deletions fw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ tested natively on a Mac. *For key press detection on Mac, you'll need to give a

1. Run `python3 bundle.py` from this directory. This will create a `bundle` directory, which will copy all required files into folders to be loaded onto each board.
2. Find the directory with your board's name in the `bundle` directory. Copy all files/subdirectories onto your CircuitPython device.
3. If your board's `bundle` directory contains a `lib` subdirectory, you may have to manually copy some third-party libraries into it. The `lib` subdirectory will contain a README listing all of the required dependencies. Official CircuitPython libraries available [here](https://circuitpython.org/libraries). As of May 2021, I have been using Bundle version 6.x.
4. If you're using the `macos` version, execute `python3 code.py` from the `bundle/macos` directory.
3. If your board requires third-party libraries to run this code (most do), you may have to manually copy them. Each board's `bundle` directory will contain a README listing all of the required dependencies. Official CircuitPython libraries available [here](https://circuitpython.org/libraries). As of May 2021, I have been using Bundle version 6.x.
4. If you're using the `macos` version, execute `python3 code.py` from the `bundle/macos` directory. You'll have to `pip3 install pynput` as well.
59 changes: 40 additions & 19 deletions fw/core/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,6 @@ def __init__(self, keys, words, pres):
def __str__(self):
return f'keys: {self.keys}, words: {self.words}, pres: {self.pres}'

# Choose mode based on held keys at startup
if ('1' in keypad.pressed_keys):
leds.show_red()
run_numeric_mode()
elif ('2' in keypad.pressed_keys):
leds.show_green()
run_macro_mode()
else:
leds.show_blue()

# Flag to indicate to our main loop that we want to start a new word
force_break_word = False

keypad_dict = {
'1' : ['1'],
'2' : ['a', 'b', 'c'],
Expand All @@ -116,11 +103,45 @@ def __str__(self):
'#' : ['.', ',', '?', '!']
}

def error_mode():
print("ERROR!")
while True:
time.sleep(0.01)
pass

reverse_key_dict = { }
for k, l in keypad_dict.items():
for c in l:
reverse_key_dict[c] = k

# TODO - read in word list from txt file, build this dict dynamically.
priority_words = {
4: "i",
46: "in"
}
priority_words = { }
try:
with open("priority_words.txt", "r") as fp:
for line in fp.readlines():
line = line.strip().lower()
key_seq = ""
for c in line:
key = reverse_key_dict.get(c)
if key is None:
error_mode()
key_seq = key_seq + key
priority_words[key_seq] = line
except IOError:
pass

# Choose mode based on held keys at startup
if ('1' in keypad.pressed_keys):
leds.show_red()
run_numeric_mode()
elif ('2' in keypad.pressed_keys):
leds.show_green()
run_macro_mode()
else:
leds.show_blue()

# Flag to indicate to our main loop that we want to start a new word
force_break_word = False

# given a file and location in that file, read a 24bit unsigned int
def read_int(file, offset):
Expand Down Expand Up @@ -163,7 +184,7 @@ def get_words(file, input, last_result):
output_prefixes.append(test_word)
elif result == WORD:
output_words.append(test_word)
return Results((last_result.keys * 10) + int(input), output_words, output_prefixes)
return Results(last_result.keys + input, output_words, output_prefixes)

# if old_word has been typed, and new_word should replace it - how many chars to we need to replace?
# ex: uncommon_chars("catch", "cab") == 3
Expand Down Expand Up @@ -284,7 +305,7 @@ def poll_keys():
while True:
word_index = 0
current_word = ""
last_result = Results(0, [""], [])
last_result = Results("", [""], [])
results = [ last_result ]
result_index = 0
while True:
Expand Down
11 changes: 11 additions & 0 deletions fw/core/priority_words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
i
in
to
my
is
no
of
be
as
it
so
3 changes: 3 additions & 0 deletions fw/macos/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ def __init__(self):
self.color = None

def show_red(self):
print("set led to red")
self.color = 'r'

def show_green(self):
print("set led to green")
self.color = 'g'

def show_blue(self):
print("set led to blue")
self.color = 'b'
2 changes: 1 addition & 1 deletion fw/tiny2040/lib/README.md → fw/tiny2040/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Required (Circuit/Micro)Python Libraries:
**Make sure the following files are in this folder, on your microcontroller**
**Make sure the following files are in a folder called `lib`, on your microcontroller**
* adafruit_hid
* `adafruit_hid/__init__.mpy`
* `adafruit_hid/consumer_control_code.mpy`
Expand Down

0 comments on commit 9ca1a42

Please sign in to comment.