Skip to content

Commit

Permalink
Add docs, make a few things throw exceptions on precision loss.
Browse files Browse the repository at this point in the history
  • Loading branch information
BenFrantzDale committed Jul 21, 2016
1 parent 3a75620 commit fb4ce22
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
18 changes: 14 additions & 4 deletions OpenFL/FLP.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,16 @@ class LaserPowerLevel(LaserCommand):
dtype = 'H'

def __init__(self, power_ticks = 0):
self.data = int(power_ticks)
self.power_ticks = power_ticks

@property
def power(self):
return self.data
@power.setter
def power(self, power_ticks)
if int(power_ticks) != power_ticks:
raise ValueError("Precision loss. Cast argument to int if that's what you want.")
self.data = int(power_ticks)


class XYMoveClockRate(LaserCommand):
Expand All @@ -226,16 +231,16 @@ class MotorMoveCommand(MotorCommand):
"""
dtype = 'i'
def __init__(self, usteps=0):
if not int(usteps) == usteps:
raise TypeError('usteps must be an integer, otherwise you are asking for round')
self.usteps = int(usteps)
self.usteps = usteps

@property
def usteps(self):
return self.data

@usteps.setter
def usteps(self, usteps):
if not int(usteps) == usteps:
raise ValueError("usteps must be an integer, otherwise you are asking for precision loss. Cast argument to int if that's what you want. But remember, precision loss in number of steps adds up!")
self.data = int(usteps)

def _reprContents(self):
Expand All @@ -257,6 +262,8 @@ def usteps_per_s(self):

@usteps_per_s.setter
def usteps_per_s(self, usteps_per_s):
if not int(usteps_per_s) == usteps_per_s:
raise ValueError("usteps_per_s must be an integer, otherwise you are asking for precision loss. Cast argument to int if that's what you want.")
self.data = int(usteps_per_s)

def _reprContents(self):
Expand All @@ -281,6 +288,8 @@ def current(self):

@current.setter
def current(self, current):
if not int(current) == current:
raise ValueError("current must be an integer, otherwise you are asking for precision loss. Cast argument to int if that's what you want.")
self.data = int(current)

class ZMove(MotorMoveCommand):
Expand Down Expand Up @@ -533,6 +542,7 @@ def parsePacket(fh):
class Packets(list):
"""
A list of packets. This represents a file or collection of files.
Packets inherits list so you can delete and insert entries, just like in a list.
"""
def __init__(self, *a, **k):
super(Packets, self).__init__(*a, **k)
Expand Down
4 changes: 3 additions & 1 deletion OpenFL/Printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ def delete_block(self, block, end=None):
expect_success=True)

def read_block(self, block):
""" Reads a block by number
""" Reads a block by number.
The result is an FLP.Packets object, which is a Python list,
meaning you can delete and insert entries, just like in a list.
"""
data = self._command(Command.CMD_READ_BLOCK,
bytearray(struct.pack('<I', block)),
Expand Down
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,49 @@ pip install -r requirements.txt

Then, have a look through the `examples` subfolder.

# Modifying prints
A print can be read from the printer. Each layer is a "block" on the printer, which can be read as a `FLP.Packets` object, which is a Python `list`:
```
>>> from OpenFL import Printer, FLP
>>> p=Printer.Printer()
>>> layer = p.read_block(0)
>>> assert isinstance(layer, FLP.Packets)
>>> assert isinstance(layer, list)
>>> layer[:11]
[<XYMoveClockRate(<function moverate_Hz at 0x106eac5f0> Hz) at 0x106f41610>,
<LayerDone() at 0x106f415d0>,
<ZCurrent(80) at 0x106f41650>,
<TiltCurrent(80) at 0x106f416d0>,
<ZFeedRate(4000 usteps/s) at 0x106f41710>,
<ZMove(2000 usteps) at 0x106f41790>,
<WaitForMovesToComplete() at 0x106f417d0>,
<WaitForMovesToComplete() at 0x106f41750>,
<ZFeedRate(4000 usteps/s) at 0x106f41810>,
<ZMove(-1960 usteps) at 0x106f41850>,
<WaitForMovesToComplete() at 0x106f41890>]
>>> print layer[9]
0x03 ZMove -1960
>>> layer[9].usteps
-1960
>>> layer[9].usteps = 42
>>> layer[9]
<ZMove(42 usteps) at 0x106f41850>
```
alternately, you could do:
```
>>> layer[9] = FLP.ZMove(usteps=42) # Overwrite packet
```
or
```
del layer[9] # Delete packet from list
layer.insert(9, FLP.ZMove(usteps=42)) # Insert packet
```
because FLP.Packets is a Python list (i.e., it inherits list) so you can append, insert, concatenate, etc.

Finally, the block can be pushed back to the printer:
```
p.write_block(0, layer)
```

# Serial Output Commands
OpenFL provides commands for bidirectional communication with the printer while it is printing.
Expand Down

0 comments on commit fb4ce22

Please sign in to comment.