Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/python/hal.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def __new__(cls, item):
def _item_wrap(self, item):
for f in ['get', 'set', 'get_type', 'get_name', 'get_dir', 'is_pin', '__repr__']:
setattr(self, f, getattr(item, f))
# Only ports can use the extra methods
if _hal.HAL_PORT == item.get_type():
for f in ['write', 'read', 'peek', 'peek_commit', 'clear', 'size']:
setattr(self, f, getattr(item, f))
return self

def __init__(self, item):
Expand Down Expand Up @@ -69,4 +73,4 @@ def newparam(self, *a, **kw): return Param(_hal.component.newparam(self, *a, **k

def getpin(self, *a, **kw): return Pin(_hal.component.getpin(self, *a, **kw))
def getparam(self, *a, **kw): return Param(_hal.component.getparam(self, *a, **kw))
def getpins(self, *a, **kw): return _hal.component.getpins(self, *a, **kw)
def getpins(self, *a, **kw): return _hal.component.getpins(self, *a, **kw)
19 changes: 19 additions & 0 deletions lib/python/pyhal.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@
lib.hal_port_wait_readable.argtypes = [POINTER(POINTER(c_int)), c_uint, c_int]
lib.hal_port_wait_writable.argtypes = [POINTER(POINTER(c_int)), c_uint, c_int]

import functools
import inspect
import warnings

def pyhaldeprecate(reason):
def decorator(func):
@functools.wraps(func)
def warnfunc(*args, **kwargs):
warnings.simplefilter('always', DeprecationWarning)
warnings.warn("Use of deprecated class/function {} ({}).".format(func.__name__, reason),
category=DeprecationWarning, stacklevel=2)
warnings.simplefilter('default', DeprecationWarning)
return func(*args, **kwargs)
return warnfunc
return decorator


class HalException(Exception):
"""An exception raised by hal library functions"""
pass
Expand Down Expand Up @@ -95,6 +112,7 @@ def value(self, val):
self.data_ptr.contents.contents.value = val


@pyhaldeprecate("'pyhal' causes severe problems, has been replaced by 'hal' and will soon be removed")
class port(pin):
def __init__(self, component, name, type, dir, data_ptr):
pin.__init__(self, component, name, type, dir, data_ptr)
Expand Down Expand Up @@ -169,6 +187,7 @@ def waitWritable(self, count):



@pyhaldeprecate("'pyhal' causes severe problems, has been replaced by 'hal' and will soon be removed")
class component:
def __init__(self, name):
self.id = lib.hal_init(name.encode())
Expand Down
30 changes: 16 additions & 14 deletions lib/python/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from re import match
from enum import Enum
from pyhal import *
import hal
from struct import *
import time

Expand All @@ -28,7 +28,7 @@ class RasterProgrammer(object):
"""
The programmer component is used to program the realtime raster component

The raster component operates one line at a time.
The raster component operates one line at a time.

Example:
Assuming rastering is done across the X axis.
Expand All @@ -38,7 +38,7 @@ class RasterProgrammer(object):
Because positional data at the hal level is joint position the program must be relative
to the current position that the machine is at when programming the line.

Because a port can become full each command waits until the port is available for writing. The commands will
Because a port can become full each command waits until the port is available for writing. The commands will
raise a ProgrammerException if too much time passes before the write completes. This is usually an indication
that your PORT buffer isn't large enough
"""
Expand All @@ -53,41 +53,43 @@ def __init__(self, name):
enabled - BIT IN - link to the raster's enabled pin. Raster programmer uses this to wait on rasters activation
"""
self.__timeout = 5.0
self.component = component(name)
self.port = self.component.pinNew("program", halType.PORT, pinDir.OUT)
self.faultCode = self.component.pinNew("fault-code", halType.SIGNED, pinDir.IN)
self.enabled = self.component.pinNew("enabled", halType.BIT, pinDir.IN)
self.run = self.component.pinNew("run", halType.BIT, pinDir.OUT)
self.component = hal.component(name)
self.port = self.component.newpin("program", hal.HAL_PORT, hal.HAL_OUT)
self.faultCode = self.component.newpin("fault-code", hal.HAL_S32, hal.HAL_IN)
self.enabled = self.component.newpin("enabled", hal.HAL_BIT, hal.HAL_IN)
self.run = self.component.newpin("run", hal.HAL_BIT, hal.HAL_OUT)
self.component.ready()

self.run.value = False

def __del__(self):
self.component.exit()
if None != self.component:
self.component.exit()

def exit(self):
"""unloads this programmer component from hal"""
self.component.exit()

self.component = None

def __waitEnabled(self, enabled):
timeout = time.process_time() + self.__timeout
while self.enabled.value != enabled:
time.sleep(0.001)
if time.process_time() > timeout:
self.run.value = False
raise ProgrammerException("Raster failed to respond before the timeout was reached")

if self.faultCode.value != FaultCodes.OK.value:
self.run.value = False
raise ProgrammerException("Raster faulted with error {0}".format(FaultCodes(self.faultCode.value)))

def begin(self, offset, bpp, ppu, count):
"""
Sends the program begin command along with relevant parameters.

offset - The relative starting position that the incoming data starts. Data is always programmed from most negative on the axis to most positive.
bpp - Bits per pixel in increments of 4 bits, up to 32 bits
ppu - pixels per unit. The number of pixels per machine unit. count * dpu gives the total span of the raster line
count - the number of pixels that will be programmed on the line
count - the number of pixels that will be programmed on the line
"""
self.run.value = False
self.__waitEnabled(False)
Expand Down
1 change: 1 addition & 0 deletions src/hal/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ extern int hal_set_constructor(int comp_id, constructor make);
command.
*/

#define HAL_PORT_SIZE_MAX 65536

/** hal_port_read reads count bytes from the port into dest.
This function should only be called by the component that owns
Expand Down
Loading
Loading