Skip to content

Instantly share code, notes, and snippets.

@apparentlymart
Last active December 22, 2015 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apparentlymart/6408765 to your computer and use it in GitHub Desktop.
Save apparentlymart/6408765 to your computer and use it in GitHub Desktop.
Alamatic Examples
# External library declarations allow us to bridge to code written in other languages like
# C or assembler. They are similar in principle to a C header file, but expressed in
# Alamatic syntax and in terms of Alamatic's type system.
# However, since behind the scenes we're just creating symbols for the linker to resolve
# there is still the risk of conflicting symbols between libraries and nothing much we
# can do about that, so prefixes on symbols are strongly recommended just as in C.
# declares an external library that, if used, will be linked using "-lfoo".
# macros will be evaluated in the context of foo.h.
# The first time any attributes are accessed we'll run the compiler and linker
# to verify that the given library and headers are available so that we can give
# a better error message than the underlying toolchain could muster.
extlib foo from "foo" with "foo.h":
# on first use we'll run the C preprocessor with foo.h to find out what to replace this with,
# caching the result to reduce overhead.
exprmacro SOME_NUMBER as Int32
# on each call we'll run the C preprocessor with foo.h to find out what to replace this with,
# but in this case (since the macro has params) we won't cache it. To be used sparingly.
exprmacro SOMETHING_ELSE(a as Int32) as Int16
# a type that maps to an underlying C type with an identical name (probably a typedef in this case).
# Since Alamatic can't reason about these types in any detail, they are only compatible with themselves.
ctype Foo_Thingy
# a type that maps to a C type with a different name (up to the developer to get the C representation right)
ctype Foo_Thingy_Ptr "Foo_Thingy*"
# if called we'll generate an extern declaration for this function and then call it, leaving
# the linker to resolve the function. This is one case where the underlying linker error message
# will shine through if the developer gets this wrong.
func foo_get_number() as Int32
func foo_create_thingy() as Foo_Thingy
# return type declaration and parameter type declaration are mandatory for extlib functions
# and they must be concrete types, not type constraints.
func foo_init(flags as Int32) as Void
# on access we'll generate an extern declaration and then reference it by name in the code.
var foo_value as Int32
# Maybe we'll support C++ templates later, but not right now.
import arduino
# Pin 13 has an LED connected to it on most Arduino boards
var led_pin = arduino.gpio[13]
# Time we'll wait between changing brightnesses
var delay = arduino.delay.milliseconds(30)
# Configure the LED pin as an output
led_pin.set_direction(OUTPUT)
var change = 5 as Int8
const max_brightness = led_pin.max_pwm_value
const min_brightness = led_pin.min_pwm_value
var current_brightness = min_brightness # inherits the type of min_brightness automatically
loop:
led_pin.enable_pwm(current_brightness)
if current_brightness == min_brightness or current_brightness == max_brightness:
change = -change
current_brightness = current_brightness + change
delay.wait()
import arduino
# Pin 13 has an LED connected to it on most Arduino boards
var led_pin = arduino.gpio[13]
# Interval has an event that occurs periodically based on a timer
var interval = arduino.interval.milliseconds(1000)
# Configure the LED pin as an output
led_pin.set_direction(OUTPUT)
when interval.tick():
led_pin.toggle()
# Start the clock
interval.begin()
# Go to sleep forever, just waking up to handle events when they occur.
arduino.handle_events()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment