Hardware & industrial
Read sensors, drive motors and displays, talk to PLCs.
Overview
You write a driver once, against the hal interfaces. hardware-gpio provides those interfaces on Linux, for example on a Raspberry Pi: GPIO through libgpiod, I2C and SPI through /dev/i2c-* and /dev/spidev*, PWM through sysfs, serial through termios.
Component drivers depend on hal only: you hand them the backend's objects. Their tests run without hardware, against simulated buses.
amc also compiles for bare-metal Cortex-M (--target=cortex-m3, cortex-m4 or cortex-m7, since v0.8.89). The packages on this page are built and tested on Linux.
Hardware
hal v0.2.0
The interfaces every driver programs against: digital output and input, PWM, I2C and SPI buses, serial port, clock. Pure Amalgame, no C.
amc package add hal · amalgame-lang/amalgame-hal ↗
hardware-gpio v0.7.0
The Linux backend, for Raspberry Pi 1 to 5: GPIO and edge events through libgpiod, I2C, SPI, PWM and UART. It needs libgpiod 2.x, which Raspberry Pi OS Bookworm does not ship: build it from source.
amc package add hardware-gpio · amalgame-lang/amalgame-hardware-gpio ↗
hardware-sensor v0.6.1
Sensors: HC-SR04 distance; BME280, SHT31, AHT20 and DS18B20 temperature, humidity and pressure; MPU-6050 IMU; BH1750 light; INA219 current; MCP3008, ADS1115 and HX711 converters; MAX6675 thermocouple.
amc package add hardware-sensor · amalgame-lang/amalgame-hardware-sensor ↗
hardware-display v0.4.0
Displays: SSD1306 128×64 OLED, HD44780 text LCD on an I2C backpack, MAX7219 (8×8 matrix or digits), TM1637 4-digit display. The OLED font covers digits and a few letters, not the whole alphabet yet.
amc package add hardware-display · amalgame-lang/amalgame-hardware-display ↗
hardware-motor v0.4.0
Motors: servo and ESC, DC motor through an H-bridge, 28BYJ-48 stepper, A4988 and DRV8825 step/dir drivers, acceleration ramp, relay, PCA9685 (16 PWM outputs over I2C).
amc package add hardware-motor · amalgame-lang/amalgame-hardware-motor ↗
hardware-led v0.3.0
Plain LED, RGB LED over PWM, APA102 and WS2812 addressable strips driven over SPI.
amc package add hardware-led · amalgame-lang/amalgame-hardware-led ↗
hardware-input v0.2.0
Debounced button, rotary encoder, matrix keypad.
amc package add hardware-input · amalgame-lang/amalgame-hardware-input ↗
hardware-io v0.2.0
I/O expanders: PCF8574 (8 pins) and MCP23017 (16 pins) over I2C, 74HC595 shift register. Each added pin works like a regular pin in the other drivers.
amc package add hardware-io · amalgame-lang/amalgame-hardware-io ↗
hardware-comms v0.1.0
GPS over a serial port: reads NMEA sentences and gives position, altitude and satellite count.
amc package add hardware-comms · amalgame-lang/amalgame-hardware-comms ↗
hardware-control v0.1.0
PID controller, complementary filter for an IMU, moving average, map and clamp. No hardware, no dependency.
amc package add hardware-control · amalgame-lang/amalgame-hardware-control ↗
hardware-rtc v0.1.0
DS3231 (with thermometer) and DS1307 real-time clocks over I2C: their battery keeps the time through power cuts.
amc package add hardware-rtc · amalgame-lang/amalgame-hardware-rtc ↗
Industrial
Two protocols to read from and write to PLCs.
modbus v0.1.0
Modbus TCP client: reads coils, inputs and registers, writes (function codes 01 to 06 and 16), decodes int16, int32 and float32 in both word orders, timeouts, automatic reconnect. A server plays the PLC for your tests. Written in C, no libmodbus. IPv4 addresses only, plain-text traffic, no Modbus RTU yet. Tested on Linux and Windows.
amc package add modbus · amalgame-lang/amalgame-modbus ↗
opcua v0.3.1
OPC UA client: reads many nodes in one request, writes each value as the node's own type, browses the address space with engineering units and ranges. Signed, or signed and encrypted connections (Basic256Sha256 and newer), with certificates and users; the server certificate can be pinned by its fingerprint. A small server is there for tests. Built on open62541 v1.4.22 (vendored, MPL-2.0) and OpenSSL. Linux for now.
amc package add opcua · amalgame-lang/amalgame-opcua ↗
Examples
On a Raspberry Pi: read a BME280 on I2C bus 1, turn on an LED on GPIO17 above 25 °C.
amc package add hardware-gpio amc package add hardware-sensor amc package add hardware-led
namespace Demo
import Amalgame.Hardware
import Amalgame.Hardware.Sensor
import Amalgame.Hardware.Led
public class Program {
public static void Main(string[] args) {
let env = new Bme280(new I2c(1), 0x76) // /dev/i2c-1, address 0x76
let led = new Led(new GpioOut(17)) // GPIO17
if (!env.IsPresent()) {
Console.WriteLine("No BME280 on /dev/i2c-1")
return
}
env.Read()
let t: int = env.TempCenti() // °C × 100
let p: int = env.PressurePa()
Console.WriteLine(String_FromInt(t / 100) + " °C")
Console.WriteLine(String_FromInt(p) + " Pa")
led.Set(t > 2500) // on above 25 °C
Gpio.Close()
}
}
A Modbus TCP client talking to the package's simulator, on the same machine.
amc package add modbus
namespace Demo
import Amalgame.Modbus
public class Program {
public static void Main(string[] args) {
// A simulated PLC, on this machine
Modbus.ServerStart("127.0.0.1", 1502)
Modbus.ServerSetHolding(0, 1234)
Modbus.ServerSetFloat32(3, 10, 21.5, false) // registers 10 and 11
// Host, port, timeout in ms
let h: int = Modbus.Connect("127.0.0.1", 1502, 1000)
// Unit 1, address 0, 1 register
if (Modbus.ReadHolding(h, 1, 0, 1) < 0) {
Console.WriteLine("Failed: " + Modbus.LastError(h))
return
}
let r0: int = Modbus.Value(h, 0)
Console.WriteLine("Register 0: " + String_FromInt(r0))
if (Modbus.ReadHolding(h, 1, 10, 2) == 2) {
let f: float = Modbus.Float32(h, 0, false)
Console.WriteLine("Registers 10-11: " + String_FromFloat(f))
}
Modbus.WriteRegister(h, 1, 100, 42)
let w: int = Modbus.ServerGetHolding(100)
Console.WriteLine("Written: " + String_FromInt(w))
Modbus.Close(h)
}
}
At a glance
- License: Apache-2.0 (
opcuavendors open62541, under MPL-2.0) - Install:
amc package add <name>takes the latest compatible version;halcomes with the drivers - Versions: as listed in the registry
- Official index: amalgame-lang/packages-index