Skip to content

Commit

Permalink
Added support for Linear Poti, Line, and Piezo Speaker 2.0 Bricklets
Browse files Browse the repository at this point in the history
  • Loading branch information
feritarou committed Feb 9, 2020
1 parent df48de1 commit e9866f8
Show file tree
Hide file tree
Showing 13 changed files with 706 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/test.cr
/dl/
/obj/
/docs/
/lib/
Expand Down
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ license: MIT
repository: https://github.com/mathalaxy/tinkerforge
documentation: https://mathalaxy.github.io/tinkerforge/

scripts:
postinstall: mkdir -p dl obj && cd dl && curl -o tf.zip "https://download.tinkerforge.com/bindings/c/tinkerforge_c_bindings_2_1_26.zip" && unzip -uq tf.zip source/* && cd source && make && cd ../.. && cp dl/source/*.o obj
# scripts:
# postinstall: mkdir -p dl/source obj && cd dl && curl -o tf.zip "https://download.tinkerforge.com/bindings/c/tinkerforge_c_bindings_2_1_27.zip" && unzip -uq tf.zip source/* && cd source && make && cd ../.. && cp dl/source/*.o obj
58 changes: 58 additions & 0 deletions src/bindings/bricklets/line.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@[Link(
ldflags: "#{__DIR__}/../../../obj/bricklet_line.o"
)]

lib LibTF
alias Line = Entity

# Creates the device object \c line with the unique device ID \c uid and adds
# it to the IPConnection \c ipcon.
fun line_create(
line : Line*,
uid : LibC::Char*,
ipcon : IPConnection*
) : Void

# Removes the device object \c line from its IPConnection and destroys it.
# The device object cannot be used anymore afterwards.
fun line_destroy(
line : Line*
) : Void

# Returns the currently measured reflectivity. The reflectivity is a value between 0 (not reflective) and 4095 (very reflective).
#
# Usually black has a low reflectivity while white has a high reflectivity.
#
# If you want to get the reflectivity periodically, it is recommended to use the LINE_CALLBACK_REFLECTIVITY callback and set the period with line_set_reflectivity_callback_period().
fun line_get_reflectivity(
line : Line*,
ret_reflectivity : Int16*
) : LibC::Int

LINE_CALLBACK_REFLECTIVITY = 8
LINE_CALLBACK_REFLECTIVITY_REACHED = 9

fun line_register_callback(
line : Line*,
callback_id : Int16,
function : Void*,
user_data : Void*
) : Void

fun line_set_reflectivity_callback_period(
line : Line*,
period : UInt32
) : LibC::Int

fun line_set_reflectivity_callback_threshold(
line : Line*,
option : LibC::Char,
min : UInt16,
max : UInt16
) : LibC::Int

fun line_set_debounce_period(
line : Line*,
debounce : UInt32
) : LibC::Int
end
43 changes: 43 additions & 0 deletions src/bindings/bricklets/linear_poti.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@[Link(
ldflags: "#{__DIR__}/../../../obj/bricklet_linear_poti.o"
)]

lib LibTF
alias LinearPoti = Entity

# Creates the device object \c linear_poti with the unique device ID \c uid and adds
# it to the IPConnection \c ipcon.
fun linear_poti_create(
linear_poti : LinearPoti*,
uid : LibC::Char*,
ipcon : IPConnection*
) : Void

# Removes the device object \c linear_poti from its IPConnection and destroys it.
# The device object cannot be used anymore afterwards.
fun linear_poti_destroy(
linear_poti : LinearPoti*
) : Void

# Returns the position of the linear potentiometer. The value is between 0 (slider down) and 100 (slider up).
#
# If you want to get the position periodically, it is recommended to use the {@link LINEAR_POTI_CALLBACK_POSITION} callback and set the period with {@link linear_poti_set_position_callback_period}.
fun linear_poti_get_position(
linear_poti : LinearPoti*,
ret_position : Int16*
) : LibC::Int

fun linear_poti_set_position_callback_period(
linear_poti : LinearPoti*,
period : UInt32
) : LibC::Int

LINEAR_POTI_CALLBACK_POSITION = 13

fun linear_poti_register_callback(
linear_poti : LinearPoti*,
callback_id : Int16,
function : Void*,
user_data : Void*
) : Void
end
167 changes: 167 additions & 0 deletions src/bindings/bricklets/piezo_speaker_v2.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
@[Link(
ldflags: "#{__DIR__}/../../../obj/bricklet_piezo_speaker_v2.o"
)]

lib LibTF
alias PiezoSpeakerV2 = Entity

# Creates the device object \c piezo_speaker_v2 with the unique device ID \c uid and adds
# it to the IPConnection \c ipcon.
fun piezo_speaker_v2_create(
piezo_speaker_v2 : PiezoSpeakerV2*,
uid : LibC::Char*,
ipcon : IPConnection*
) : Void

# Removes the device object \c piezo_speaker_v2 from its IPConnection and destroys it.
# The device object cannot be used anymore afterwards.
fun piezo_speaker_v2_destroy(
piezo_speaker_v2 : PiezoSpeakerV2*
) : Void

PIEZO_SPEAKER_V2_BEEP_DURATION_OFF = 0
PIEZO_SPEAKER_V2_BEEP_DURATION_INFINITE = 4294967295

# Beeps with the given frequency and volume for the duration.
#
# A duration of 0 stops the current beep if any is ongoing. A duration of 4294967295 results in an infinite beep.
#
# The following constants are available for this function:
#
# For duration:
#
# PIEZO_SPEAKER_V2_BEEP_DURATION_OFF = 0
# PIEZO_SPEAKER_V2_BEEP_DURATION_INFINITE = 4294967295
fun piezo_speaker_v2_set_beep(
piezo_speaker_v2 : PiezoSpeakerV2*,
frequency : UInt16,
volume : UInt8,
duration : UInt32
) : LibC::Int

# Returns the last beep settings as set by piezo_speaker_v2_set_beep(). If a beep is currently running it also returns the remaining duration of the beep.
#
# If the frequency or volume is updated during a beep (with piezo_speaker_v2_update_frequency() or piezo_speaker_v2_update_volume()) this function returns the updated value.
#
# The following constants are available for this function:
#
# For ret_duration:
#
# PIEZO_SPEAKER_V2_BEEP_DURATION_OFF = 0
# PIEZO_SPEAKER_V2_BEEP_DURATION_INFINITE = 4294967295
fun piezo_speaker_v2_get_beep(
piezo_speaker_v2 : PiezoSpeakerV2*,
ret_frequency : UInt16*,
ret_volume : UInt8*,
ret_duration : UInt32*,
ret_duration_remaining : UInt32*
) : LibC::Int

PIEZO_SPEAKER_V2_ALARM_DURATION_OFF = 0
PIEZO_SPEAKER_V2_ALARM_DURATION_INFINITE = 4294967295

# Creates an alarm (a tone that goes back and force between two specified frequencies).
#
# The following parameters can be set:
#
# Start Frequency: Start frequency of the alarm.
# End Frequency: End frequency of the alarm.
# Step Size: Size of one step of the sweep between the start/end frequencies.
# Step Delay: Delay between two steps (duration of time that one tone is used in a sweep).
# Duration: Duration of the alarm.
#
# A duration of 0 stops the current alarm if any is ongoing. A duration of 4294967295 results in an infinite alarm.
#
# Below you can find two sets of example settings that you can try out. You can use these as a starting point to find an alarm signal that suits your application.
#
# Example 1: 10 seconds of loud annoying fast alarm
#
# Start Frequency = 800
# End Frequency = 2000
# Step Size = 10
# Step Delay = 1
# Volume = 10
# Duration = 10000
#
# Example 2: 10 seconds of soft siren sound with slow build-up
#
# Start Frequency = 250
# End Frequency = 750
# Step Size = 1
# Step Delay = 5
# Volume = 0
# Duration = 10000
#
# The following conditions must be met:
#
# Start Frequency: has to be smaller than end frequency
# End Frequency: has to be bigger than start frequency
# Step Size: has to be small enough to fit into the frequency range
# Step Delay: has to be small enough to fit into the duration
#
# The following constants are available for this function:
#
# For duration:
#
# PIEZO_SPEAKER_V2_ALARM_DURATION_OFF = 0
# PIEZO_SPEAKER_V2_ALARM_DURATION_INFINITE = 4294967295
fun piezo_speaker_v2_set_alarm(
piezo_speaker_v2 : PiezoSpeakerV2*,
start_frequency : UInt16,
end_frequency : UInt16,
step_size : UInt16,
step_delay : UInt16,
volume : UInt8,
duration : UInt32
) : LibC::Int

# Returns the last alarm settings as set by piezo_speaker_v2_set_alarm(). If an alarm is currently running it also returns the remaining duration of the alarm as well as the current frequency of the alarm.
#
# If the volume is updated during an alarm (with piezo_speaker_v2_update_volume()) this function returns the updated value.
#
# The following constants are available for this function:
#
# For ret_duration:
#
# PIEZO_SPEAKER_V2_ALARM_DURATION_OFF = 0
# PIEZO_SPEAKER_V2_ALARM_DURATION_INFINITE = 4294967295
#
# For ret_duration_remaining:
#
# PIEZO_SPEAKER_V2_ALARM_DURATION_OFF = 0
# PIEZO_SPEAKER_V2_ALARM_DURATION_INFINITE = 4294967295
fun piezo_speaker_v2_get_alarm(
piezo_speaker_v2 : PiezoSpeakerV2*,
ret_start_frequency : UInt16*,
ret_end_frequency : UInt16*,
ret_step_size : UInt16*,
ret_step_delay : UInt16*,
ret_volume : UInt8*,
ret_duration : UInt32*,
ret_duration_remaining : UInt32*,
ret_current_frequency : UInt16*
) : LibC::Int

# Updates the volume of an ongoing beep or alarm.
fun piezo_speaker_v2_update_volume(
piezo_speaker_v2 : PiezoSpeakerV2*,
volume : UInt8
) : LibC::Int

# Updates the frequency of an ongoing beep.
fun piezo_speaker_v2_update_frequency(
piezo_speaker_v2 : PiezoSpeakerV2*,
frequency : UInt16
) : LibC::Int


PIEZO_SPEAKER_V2_STATUS_LED_CONFIG_OFF = 0u8
PIEZO_SPEAKER_V2_STATUS_LED_CONFIG_ON = 1u8
PIEZO_SPEAKER_V2_STATUS_LED_CONFIG_SHOW_HEARTBEAT = 2u8
PIEZO_SPEAKER_V2_STATUS_LED_CONFIG_SHOW_STATUS = 3u8

fun piezo_speaker_v2_set_status_led_config(
piezo_speaker_v2 : PiezoSpeakerV2*,
config : UInt8
) : LibC::Int
end
31 changes: 30 additions & 1 deletion src/bindings/bricks/silent_stepper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

lib LibTF
alias SilentStepper = Entity

# Creates the device object \c silent_stepper with the unique device ID \c uid and adds
# it to the IPConnection \c ipcon.
fun silent_stepper_create(
Expand Down Expand Up @@ -116,6 +116,10 @@ lib LibTF
silent_stepper : SilentStepper*
) : LibC::Int

fun silent_stepper_reset(
silent_stepper : SilentStepper*
) : LibC::Int

SILENT_STEPPER_STEP_RESOLUTION_1 = 8
SILENT_STEPPER_STEP_RESOLUTION_2 = 7
SILENT_STEPPER_STEP_RESOLUTION_4 = 6
Expand All @@ -125,4 +129,29 @@ lib LibTF
SILENT_STEPPER_STEP_RESOLUTION_64 = 2
SILENT_STEPPER_STEP_RESOLUTION_128 = 1
SILENT_STEPPER_STEP_RESOLUTION_256 = 0

SILENT_STEPPER_CALLBACK_UNDER_VOLTAGE = 40
SILENT_STEPPER_CALLBACK_POSITION_REACHED = 41
SILENT_STEPPER_CALLBACK_ALL_DATA = 47
SILENT_STEPPER_CALLBACK_NEW_STATE = 48

fun silent_stepper_register_callback(
silent_stepper : SilentStepper*,
callback_id : Int16,
function : Void*,
user_data : Void*
) : Void

SILENT_STEPPER_STATE_STOP = 1
SILENT_STEPPER_STATE_ACCELERATION = 2
SILENT_STEPPER_STATE_RUN = 3
SILENT_STEPPER_STATE_DEACCELERATION = 4
SILENT_STEPPER_STATE_DIRECTION_CHANGE_TO_FORWARD = 5
SILENT_STEPPER_STATE_DIRECTION_CHANGE_TO_BACKWARD = 6

fun silent_stepper_set_all_data_period(
silent_stepper : SilentStepper*,
period : UInt32
) : LibC::Int

end
Loading

0 comments on commit e9866f8

Please sign in to comment.