A program in several files: a small weather station
The 4 files of examples/station/, and what zeno run examples/station/main.zeno prints.
# A program in several files: a small weather station.
#
# zeno run examples/station/main.zeno
#
# This is the file the program starts in. The other files are modules,
# and an `import` brings one in. `import sensors` reads `sensors.zeno`
# from the folder this file is in, and its names are then written with
# `sensors.` in front. A dot in a module's name is a folder:
# `drivers.uart` is the file `drivers/uart.zeno`.
import sensors
import report
import drivers.uart
# `from` brings in single names, to be written with nothing in front.
from sensors import Reading, Good
# The imported files have already run by the time this line does: each
# file runs once, before the file that imports it.
let readings = [
Reading("temperature", 21.5, "C"),
Reading("humidity", 104.0, "%"),
Reading("pressure", 1013.2, "hPa"),
Reading("wind", 12.0, "km/h"),
]
var good = 0
for reading in readings {
let health = sensors.check(reading)
# `Good` is a case of the enum `Health` in sensors.zeno.
if health == Good {
good += 1
}
drivers.uart.send(report.line(reading, health))
}
drivers.uart.send(report.summary(good, len(readings)))
print(f"{drivers.uart.sent_bytes()} bytes sent at {drivers.uart.speed} baud")# The module `drivers.uart`: the serial port the station reports over.
# On a real board `send` would write to the port's registers
# (examples/driver.zeno shows that). Here it prints the line.
let speed = 9600
# How many bytes went out. Only this file gives it a new value: another
# file can read `drivers.uart.sent`, and asks `send` to do the changing.
var sent = 0
function send(text: str) {
print(f"uart> {text}")
sent += len(text) + 1
}
function sent_bytes() -> int {
return sent
}# The module `report`: readings turned into lines of text.
#
# A module can import other modules. This one needs the types that
# `sensors` declares, and it imports them itself: a file sees what it
# declares and what it imports, and nothing else.
import sensors
from sensors import Reading, Health
function line(reading: Reading, health: Health) -> str {
let value = f"{reading.name:<12}{reading.value:>8.1f} {reading.unit}"
match health {
case sensors.Good { return value }
case sensors.Suspect(why) { return f"{value} suspect: {why}" }
}
}
function summary(good: int, all: int) -> str {
return f"{good} of {all} readings are good"
}# The module `sensors`: what a reading is, and whether one can be trusted.
#
# What is declared at the top of this file can be used by a file that
# imports it: the class, the enum and its cases, and the function.
# Inside this file they are written as they are declared. Another file
# writes `sensors.check`, or imports the name with `from`. The dict is
# marked `private`, so it is for this file only.
class Reading {
name: str
value: float
unit: str
}
# What `check` says about a reading.
enum Health {
Good
Suspect(why: str)
}
# The highest value each sensor can really give. Other files ask
# `check`, and have no use for the table itself.
private let highest = ["temperature": 60.0, "humidity": 100.0, "pressure": 1100.0]
function check(reading: Reading) -> Health {
if reading.name not in highest {
return Suspect("no limit is known for it")
}
if reading.value > highest[reading.name] {
return Suspect(f"over {highest[reading.name]}")
}
return Good
}zeno run examples/station/main.zeno
uart> temperature 21.5 C
uart> humidity 104.0 % suspect: over 100.0
uart> pressure 1013.2 hPa
uart> wind 12.0 km/h suspect: no limit is known for it
uart> 2 of 4 readings are good
180 bytes sent at 9600 baudSee it on GitHub: examples/station/.