Resource

class wpilib.Resource(size)[source]

Bases: builtins.object

Tracks resources in the program.

The Resource class is a convenient way of keeping track of allocated arbitrary resources in the program. Resources are just indices that have an lower and upper bound that are tracked by this class. In the library they are used for tracking allocation of hardware channels but this is purely arbitrary. The resource class does not do any actual allocation, but simply tracks if a given index is currently in use.

Allocate storage for a new instance of Resource. Allocate a bool array of values that will get initialized to indicate that no resources have been allocated yet. The indicies of the resources are 0..size-1.

Parameters:size – The number of blocks to allocate
allocate(obj, index=None)[source]

Allocate a resource.

When index is None or unspecified, a free resource value within the range is located and returned after it is marked allocated. Otherwise, it is verified unallocated, then returned.

Parameters:
  • obj – The object requesting the resource.
  • index – The resource to allocate
Returns:

The index of the allocated block.

Raises IndexError:
 

If there are no resources available to be allocated or the specified index is already used.

free(index)[source]

Force-free an allocated resource. After a resource is no longer needed, for example a destructor is called for a channel assignment class, free will release the resource value so it can be reused somewhere else in the program.

Parameters:index – The index of the resource to free.