Treehopper C# API
Sketch Class Referenceabstract

Base class that Arduino-like sketches can extend. More...

Public Member Functions

 Sketch (TreehopperUsb board, bool throwExceptions=true)
 Initialize and run a sketch More...
 
void Run ()
 Run the sketch synchronously More...
 
abstract void setup ()
 Implement this function in your sketch to provide one-time setup functionality More...
 
abstract void loop ()
 Implement this function in your sketch that gets called repeatedly until the program exits. More...
 
void pinMode (int pin, int mode)
 Configures the specified pin to behave either as an input or an output. More...
 
void digitalWrite (int pin, bool value)
 Write a HIGH or a LOW value to a digital pin. More...
 
bool digitalRead (int pin)
 Reads the value from a specified digital pin, either HIGH or LOW. More...
 
int analogRead (int pin)
 Reads the value from the specified analog pin. More...
 
void analogWrite (int pin, int value)
 Writes a PWM value to a pin. More...
 
void analogWriteResolution (int bits)
 Set the number of bits used for PWM resolution More...
 
void analogReadResolution (int bits)
 Sets the number of bits used for ADC resolution More...
 
long millis ()
 Returns the number of milliseconds since the current sketch started running More...
 
long micros ()
 Returns the number of microseconds since the current sketch started running More...
 
void delay (int ms)
 Pauses the program for the amount of time (in miliseconds) specified as parameter. More...
 
double map (double value, double fromLow, double fromHigh, double toLow, double toHigh)
 Re-maps a number from one range to another. More...
 
double constrain (double x, double a, double b)
 Constrains a number to be within a range More...
 

Public Attributes

const int INPUT = 1
 Input mode for pins. More...
 
const int OUTPUT = 2
 Output mode for pins. More...
 
const int INPUT_PULLUP = 3
 Input, with pullup, for pins. More...
 
const bool HIGH = true
 Logic high: 3.3V on most boards. More...
 
const bool LOW = false
 Logic low: 0V on most boards. More...
 
const int ledPin = int.MaxValue
 ledPin is pre-defined for use with digitalWrite(). More...
 
const bool MSBFIRST = true
 Shift data MSB-first. More...
 
const bool LSBFIRST = false
 Shift data LSB-first. More...
 
SerialShim Serial
 The Serial port to use with this sketch. More...
 

Properties

TreehopperUsb Board [get]
 The board to use with this sketch. More...
 

Detailed Description

Base class that Arduino-like sketches can extend.

Constructor & Destructor Documentation

◆ Sketch()

Sketch ( TreehopperUsb  board,
bool  throwExceptions = true 
)

Initialize and run a sketch

Parameters
boardThe Treehopper board to use
throwExceptionsWhether unimplemented or miscalled functions should throw exceptions or fail silently

Member Function Documentation

◆ Run()

void Run ( )

Run the sketch synchronously

◆ setup()

abstract void setup ( )
pure virtual

Implement this function in your sketch to provide one-time setup functionality

◆ loop()

abstract void loop ( )
pure virtual

Implement this function in your sketch that gets called repeatedly until the program exits.

◆ pinMode()

void pinMode ( int  pin,
int  mode 
)

Configures the specified pin to behave either as an input or an output.

Parameters
pinthe number of the pin whose mode you wish to set
modeINPUT, OUTPUT, or INPUT_PULLUP. Note that Treehopper inputs are always weakly pulled-up, so there is no difference between INPUT and INPUT_PULLUP

◆ digitalWrite()

void digitalWrite ( int  pin,
bool  value 
)

Write a HIGH or a LOW value to a digital pin.

To set the LED, use "LedPin"

Parameters
pinthe pin number (or ledPin)
valueHIGH or LOW

◆ digitalRead()

bool digitalRead ( int  pin)

Reads the value from a specified digital pin, either HIGH or LOW.

Parameters
pinthe number of the digital pin you want to read (int)
Returns
bool true or false (can compare with HIGH or LOW)

◆ analogRead()

int analogRead ( int  pin)

Reads the value from the specified analog pin.

Parameters
pinthe number of the analog input pin to read from
Returns
int (0 to 2^analogReadResolution – 1024 by default)

◆ analogWrite()

void analogWrite ( int  pin,
int  value 
)

Writes a PWM value to a pin.

Uses zero-jitter hardware PWM on pins 7, 9, and 9. Uses software PWM on all other pins.

Parameters
pinthe pin to write to
valuethe duty cycle between 0 (always off) and 2^analogWriteResolution (defaults to 255) (always on)

◆ analogWriteResolution()

void analogWriteResolution ( int  bits)

Set the number of bits used for PWM resolution

Parameters
bitsThe number of bits to use

◆ analogReadResolution()

void analogReadResolution ( int  bits)

Sets the number of bits used for ADC resolution

Parameters
bitsThe number of bits to use

◆ millis()

long millis ( )

Returns the number of milliseconds since the current sketch started running

Returns
the number of milliseconds since the program started

◆ micros()

long micros ( )

Returns the number of microseconds since the current sketch started running

Returns
the number of microseconds since the program started

◆ delay()

void delay ( int  ms)

Pauses the program for the amount of time (in miliseconds) specified as parameter.

Parameters
msthe number of milliseconds to pause

◆ map()

double map ( double  value,
double  fromLow,
double  fromHigh,
double  toLow,
double  toHigh 
)

Re-maps a number from one range to another.

Parameters
valuethe number to map
fromLowthe lower bound of the value's current range
fromHighthe upper bound of the value's current range
toLowthe lower bound of the value's target range
toHighthe upper bound of the value's target range
Returns
the mapped value

◆ constrain()

double constrain ( double  x,
double  a,
double  b 
)

Constrains a number to be within a range

Parameters
xthe number to constrain
athe lower end of the range
bthe upper end of the range
Returns
the constrained number

Member Data Documentation

◆ INPUT

const int INPUT = 1

Input mode for pins.

◆ OUTPUT

const int OUTPUT = 2

Output mode for pins.

◆ INPUT_PULLUP

const int INPUT_PULLUP = 3

Input, with pullup, for pins.

◆ HIGH

const bool HIGH = true

Logic high: 3.3V on most boards.

◆ LOW

const bool LOW = false

Logic low: 0V on most boards.

◆ ledPin

const int ledPin = int.MaxValue

ledPin is pre-defined for use with digitalWrite().

To avoid bus contention, Treehopper's LED pin is not attached to a user-accessible GPIO pin, so int.MaxValue is used as a hack. The digitalWrite(int, bool) function intercepts this value and directs write operations to the LED.

◆ MSBFIRST

const bool MSBFIRST = true

Shift data MSB-first.

◆ LSBFIRST

const bool LSBFIRST = false

Shift data LSB-first.

◆ Serial

SerialShim Serial

The Serial port to use with this sketch.

Property Documentation

◆ Board

TreehopperUsb Board
get

The board to use with this sketch.


The documentation for this class was generated from the following file: