Documentation ¶
Overview ¶
Package physic declares types for physical input, outputs and measurement units.
This includes temperature, humidity, pressure, tension, current, etc.
SI units ¶
The supported S.I. units is a subset of the official ones.
T tera 10¹² 1000000000000 G giga 10⁹ 1000000000 M mega 10⁶ 1000000 k kilo 10³ 1000 m milli 10⁻³ 0.001 µ,u micro 10⁻⁶ 0.000001 n nano 10⁻⁹ 0.000000001 p pico 10⁻¹² 0.000000000001
Index ¶
- type Angle
- type Distance
- type ElectricCurrent
- type ElectricPotential
- type ElectricResistance
- type ElectricalCapacitance
- type Energy
- type Env
- type Force
- type Frequency
- type LuminousFlux
- type LuminousIntensity
- type Mass
- type Power
- type Pressure
- type RelativeHumidity
- type SenseEnv
- type Speed
- type Temperature
Examples ¶
- Angle
- Angle (Flag)
- Angle (Float64)
- Angle.Set
- Distance
- Distance (Flag)
- Distance (Float64)
- Distance.Set
- ElectricCurrent
- ElectricCurrent (Flag)
- ElectricCurrent (Float64)
- ElectricCurrent.Set
- ElectricPotential
- ElectricPotential (Flag)
- ElectricPotential (Float64)
- ElectricPotential.Set
- ElectricResistance
- ElectricResistance (Flag)
- ElectricResistance (Float64)
- ElectricResistance.Set
- ElectricalCapacitance
- ElectricalCapacitance (Flag)
- ElectricalCapacitance (Float64)
- ElectricalCapacitance.Set
- Energy
- Energy (Flag)
- Energy (Float64)
- Energy.Set
- Force
- Force (Flag)
- Force (Float64)
- Force.Set
- Frequency
- Frequency (Flag)
- Frequency (Float64)
- Frequency.Period
- Frequency.Set
- LuminousFlux
- LuminousFlux (Flag)
- LuminousFlux (Float64)
- LuminousFlux.Set
- LuminousIntensity
- LuminousIntensity (Flag)
- LuminousIntensity (Float64)
- LuminousIntensity.Set
- Mass
- Mass (Flag)
- Mass (Float64)
- Mass.Set
- PeriodToFrequency
- Power
- Power (Flag)
- Power (Float64)
- Power.Set
- Pressure
- Pressure (Flag)
- Pressure (Float64)
- Pressure.Set
- RelativeHumidity
- RelativeHumidity (Flag)
- RelativeHumidity (Float64)
- RelativeHumidity.Set
- Speed
- Speed (Flag)
- Speed (Float64)
- Speed.Set
- Temperature
- Temperature (Flag)
- Temperature (Float64)
- Temperature.Celsius
- Temperature.Fahrenheit
- Temperature.Set
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Angle ¶
type Angle int64
Angle is the measurement of the difference in orientation between two vectors stored as an int64 nano radian.
A negative angle is valid.
The highest representable value is a bit over 9.223GRad or 500,000,000,000°.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(physic.Degree) fmt.Println(physic.Pi) fmt.Println(physic.Theta) }
Output: 1.000° 180.0° 360.0°
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var a physic.Angle flag.Var(&a, "angle", "angle to set the servo to") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // A 45° angle. The +2 here is to help integer based rounding. v := (physic.Pi + 2) / 4 // Convert to float64 as degree. fd := float64(v) / float64(physic.Degree) // Convert to float64 as radian. fr := float64(v) / float64(physic.Radian) fmt.Println(v) fmt.Printf("%.1fdeg\n", fd) fmt.Printf("%frad\n", fr) }
Output: 45.00° 45.0deg 0.785398rad
const ( NanoRadian Angle = 1 MicroRadian Angle = 1000 * NanoRadian MilliRadian Angle = 1000 * MicroRadian Radian Angle = 1000 * MilliRadian // Theta is 2π. This is equivalent to 360°. Theta Angle = 6283185307 * NanoRadian Pi Angle = 3141592653 * NanoRadian Degree Angle = 17453293 * NanoRadian )
Well known Angle constants.
func (*Angle) Set ¶
Set sets the Angle to the value represented by s. Units are to be provided in "rad", "deg" or "°" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var a physic.Angle if err := a.Set("2°"); err != nil { log.Fatal(a) } fmt.Println(a) if err := a.Set("90deg"); err != nil { log.Fatal(a) } fmt.Println(a) if err := a.Set("1rad"); err != nil { log.Fatal(a) } fmt.Println(a) }
Output: 2.000° 90.00° 57.296°
type Distance ¶
type Distance int64
Distance is a measurement of length stored as an int64 nano metre.
This is one of the base unit in the International System of Units.
The highest representable value is 9.2Gm.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(physic.Inch) fmt.Println(physic.Foot) fmt.Println(physic.Mile) }
Output: 25.400mm 304.800mm 1.609km
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var d physic.Distance flag.Var(&d, "distance", "x axis travel length") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // Distance between the Earth and the Moon. v := 384400 * physic.KiloMetre // Convert to float64 as meter. fm := float64(v) / float64(physic.Metre) // Convert to float64 as inches. fi := float64(v) / float64(physic.Inch) fmt.Println(v) fmt.Printf("%.0fm\n", fm) fmt.Printf("%.0fin\n", fi) }
Output: 384.400Mm 384400000m 15133858268in
const ( NanoMetre Distance = 1 MicroMetre Distance = 1000 * NanoMetre MilliMetre Distance = 1000 * MicroMetre Metre Distance = 1000 * MilliMetre KiloMetre Distance = 1000 * Metre MegaMetre Distance = 1000 * KiloMetre GigaMetre Distance = 1000 * MegaMetre // Conversion between Metre and imperial units. Thou Distance = 25400 * NanoMetre Inch Distance = 1000 * Thou Foot Distance = 12 * Inch Yard Distance = 3 * Foot Mile Distance = 1760 * Yard )
Well known Distance constants.
func (*Distance) Set ¶
Set sets the Distance to the value represented by s. Units are to be provided in "m", "Mile", "Yard", "in", or "ft" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var d physic.Distance if err := d.Set("1ft"); err != nil { log.Fatal(err) } fmt.Println(d) if err := d.Set("1m"); err != nil { log.Fatal(err) } fmt.Println(d) if err := d.Set("9Mile"); err != nil { log.Fatal(err) } fmt.Println(d) }
Output: 304.800mm 1m 14.484km
type ElectricCurrent ¶
type ElectricCurrent int64
ElectricCurrent is a measurement of a flow of electric charge stored as an int64 nano Ampere.
This is one of the base unit in the International System of Units.
The highest representable value is 9.2GA.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(10010 * physic.MilliAmpere) fmt.Println(10 * physic.Ampere) fmt.Println(-10 * physic.MilliAmpere) }
Output: 10.010A 10A -10mA
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var m physic.ElectricCurrent flag.Var(&m, "motor", "rated motor current") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // The maximum current that can be drawn from all Raspberry Pi GPIOs combined. v := 51 * physic.MilliAmpere // Convert to float64 as ampere. f := float64(v) / float64(physic.Ampere) fmt.Println(v) fmt.Printf("%.3fA\n", f) }
Output: 51mA 0.051A
const ( NanoAmpere ElectricCurrent = 1 MicroAmpere ElectricCurrent = 1000 * NanoAmpere MilliAmpere ElectricCurrent = 1000 * MicroAmpere Ampere ElectricCurrent = 1000 * MilliAmpere KiloAmpere ElectricCurrent = 1000 * Ampere MegaAmpere ElectricCurrent = 1000 * KiloAmpere GigaAmpere ElectricCurrent = 1000 * MegaAmpere )
Well known ElectricCurrent constants.
func (*ElectricCurrent) Set ¶
func (c *ElectricCurrent) Set(s string) error
Set sets the ElectricCurrent to the value represented by s. Units are to be provided in "A" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var e physic.ElectricCurrent if err := e.Set("12.5mA"); err != nil { log.Fatal(err) } fmt.Println(e) if err := e.Set("2.4kA"); err != nil { log.Fatal(err) } fmt.Println(e) if err := e.Set("2A"); err != nil { log.Fatal(err) } fmt.Println(e) }
Output: 12.500mA 2.400kA 2A
func (ElectricCurrent) String ¶
func (c ElectricCurrent) String() string
String returns the current formatted as a string in Ampere.
type ElectricPotential ¶
type ElectricPotential int64
ElectricPotential is a measurement of electric potential stored as an int64 nano Volt.
The highest representable value is 9.2GV.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(10010 * physic.MilliVolt) fmt.Println(10 * physic.Volt) fmt.Println(-10 * physic.MilliVolt) }
Output: 10.010V 10V -10mV
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var v physic.ElectricPotential flag.Var(&v, "cutout", "battery full charge voltage") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // The level of Raspberry Pi GPIO when high. v := 3300 * physic.MilliVolt // Convert to float64 as volt. f := float64(v) / float64(physic.Volt) fmt.Println(v) fmt.Printf("%.1fV\n", f) }
Output: 3.300V 3.3V
const ( // Volt is W/A, kg⋅m²/s³/A. NanoVolt ElectricPotential = 1 MicroVolt ElectricPotential = 1000 * NanoVolt MilliVolt ElectricPotential = 1000 * MicroVolt Volt ElectricPotential = 1000 * MilliVolt KiloVolt ElectricPotential = 1000 * Volt MegaVolt ElectricPotential = 1000 * KiloVolt GigaVolt ElectricPotential = 1000 * MegaVolt )
Well known ElectricPotential constants.
func (*ElectricPotential) Set ¶
func (p *ElectricPotential) Set(s string) error
Set sets the ElectricPotential to the value represented by s. Units are to be provided in "V" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var v physic.ElectricPotential if err := v.Set("250uV"); err != nil { log.Fatal(err) } fmt.Println(v) if err := v.Set("100kV"); err != nil { log.Fatal(err) } fmt.Println(v) if err := v.Set("12V"); err != nil { log.Fatal(err) } fmt.Println(v) }
Output: 250µV 100kV 12V
func (ElectricPotential) String ¶
func (p ElectricPotential) String() string
String returns the tension formatted as a string in Volt.
type ElectricResistance ¶
type ElectricResistance int64
ElectricResistance is a measurement of the difficulty to pass an electric current through a conductor stored as an int64 nano Ohm.
The highest representable value is 9.2GΩ.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(10010 * physic.MilliOhm) fmt.Println(10 * physic.Ohm) fmt.Println(24 * physic.MegaOhm) }
Output: 10.010Ω 10Ω 24MΩ
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var r physic.ElectricResistance flag.Var(&r, "shunt", "shunt resistor value") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // A common resistor value. v := 4700 * physic.Ohm // Convert to float64 as ohm. f := float64(v) / float64(physic.Ohm) fmt.Println(v) fmt.Printf("%.0fOhm\n", f) }
Output: 4.700kΩ 4700Ohm
const ( // Ohm is V/A, kg⋅m²/s³/A². NanoOhm ElectricResistance = 1 MicroOhm ElectricResistance = 1000 * NanoOhm MilliOhm ElectricResistance = 1000 * MicroOhm Ohm ElectricResistance = 1000 * MilliOhm KiloOhm ElectricResistance = 1000 * Ohm MegaOhm ElectricResistance = 1000 * KiloOhm GigaOhm ElectricResistance = 1000 * MegaOhm )
Well known ElectricResistance constants.
func (*ElectricResistance) Set ¶
func (r *ElectricResistance) Set(s string) error
Set sets the ElectricResistance to the value represented by s. Units are to be provided in "Ohm", or "Ω" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var r physic.ElectricResistance if err := r.Set("33.3kOhm"); err != nil { log.Fatal(err) } fmt.Println(r) if err := r.Set("1Ohm"); err != nil { log.Fatal(err) } fmt.Println(r) if err := r.Set("5MOhm"); err != nil { log.Fatal(err) } fmt.Println(r) }
Output: 33.300kΩ 1Ω 5MΩ
func (ElectricResistance) String ¶
func (r ElectricResistance) String() string
String returns the resistance formatted as a string in Ohm.
type ElectricalCapacitance ¶
type ElectricalCapacitance int64
ElectricalCapacitance is a measurement of capacitance stored as a pico farad.
The highest representable value is 9.2MF.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(1 * physic.Farad) fmt.Println(22 * physic.PicoFarad) }
Output: 1F 22pF
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var c physic.ElectricalCapacitance flag.Var(&c, "mintouch", "minimum touch sensitivity") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // A typical condensator. v := 4700 * physic.NanoFarad // Convert to float64 as microfarad. f := float64(v) / float64(physic.MicroFarad) fmt.Println(v) fmt.Printf("%.1fµF\n", f) }
Output: 4.700µF 4.7µF
const ( // Farad is a unit of capacitance. kg⁻¹⋅m⁻²⋅s⁴A² PicoFarad ElectricalCapacitance = 1 NanoFarad ElectricalCapacitance = 1000 * PicoFarad MicroFarad ElectricalCapacitance = 1000 * NanoFarad MilliFarad ElectricalCapacitance = 1000 * MicroFarad Farad ElectricalCapacitance = 1000 * MilliFarad KiloFarad ElectricalCapacitance = 1000 * Farad MegaFarad ElectricalCapacitance = 1000 * KiloFarad )
Well known ElectricalCapacitance constants.
func (*ElectricalCapacitance) Set ¶
func (c *ElectricalCapacitance) Set(s string) error
Set sets the ElectricalCapacitance to the value represented by s. Units are to be provided in "F" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var c physic.ElectricalCapacitance if err := c.Set("1F"); err != nil { log.Fatal(err) } fmt.Println(c) if err := c.Set("22pF"); err != nil { log.Fatal(err) } fmt.Println(c) }
Output: 1F 22pF
func (ElectricalCapacitance) String ¶
func (c ElectricalCapacitance) String() string
String returns the energy formatted as a string in Farad.
type Energy ¶
type Energy int64
Energy is a measurement of work stored as a nano joules.
The highest representable value is 9.2GJ.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(1 * physic.Joule) fmt.Println(1 * physic.WattSecond) fmt.Println(1 * physic.KiloWattHour) }
Output: 1J 1J 3.600MJ
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var e physic.Energy flag.Var(&e, "capacity", "capacity of battery") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // BTU is used in barbecue rating. It is a measure of the thermal energy // content of the of fuel consumed by the barbecue at its maximum rate. A // 35000 BTU barbecue is an average power. v := 35000 * physic.BTU // Convert to float64 as joule. f := float64(v) / float64(physic.Joule) fmt.Println(v) fmt.Printf("%.0f\n", f) }
Output: 36.927MJ 36927100
const ( // Joule is a unit of work. kg⋅m²⋅s⁻² NanoJoule Energy = 1 MicroJoule Energy = 1000 * NanoJoule MilliJoule Energy = 1000 * MicroJoule Joule Energy = 1000 * MilliJoule KiloJoule Energy = 1000 * Joule MegaJoule Energy = 1000 * KiloJoule GigaJoule Energy = 1000 * MegaJoule // BTU (British thermal unit) is the heat required to raise the temperature // of one pound of water by one degree Fahrenheit. This is the ISO value. BTU Energy = 1055060 * MilliJoule WattSecond Energy = Joule WattHour Energy = 3600 * Joule KiloWattHour Energy = 3600 * KiloJoule )
Well known Energy constants.
func (*Energy) Set ¶
Set sets the Energy to the value represented by s. Units are to be provided in "J" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var e physic.Energy if err := e.Set("2.6kJ"); err != nil { log.Fatal(err) } fmt.Println(e) if err := e.Set("45mJ"); err != nil { log.Fatal(err) } fmt.Println(e) }
Output: 2.600kJ 45mJ
type Env ¶
type Env struct { Temperature Temperature Pressure Pressure Humidity RelativeHumidity }
Env represents measurements from an environmental sensor.
type Force ¶
type Force int64
Force is a measurement of interaction that will change the motion of an object stored as an int64 nano Newton.
A measurement of Force is a vector and has a direction but this unit only represents the magnitude. The orientation needs to be stored as a Quaternion independently.
The highest representable value is 9.2TN.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(10 * physic.MilliNewton) fmt.Println(physic.EarthGravity) fmt.Println(physic.PoundForce) }
Output: 10mN 9.807N 4.448N
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var f physic.Force flag.Var(&f, "force", "load cell wakeup force") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // The gravity of Earth. v := physic.EarthGravity // Convert to float64 as newton. fn := float64(v) / float64(physic.Newton) // Convert to float64 as lbf. fl := float64(v) / float64(physic.PoundForce) fmt.Println(v) fmt.Printf("%fN\n", fn) fmt.Printf("%flbf\n", fl) }
Output: 9.807N 9.806650N 2.204623lbf
const ( // Newton is kg⋅m/s². NanoNewton Force = 1 MicroNewton Force = 1000 * NanoNewton MilliNewton Force = 1000 * MicroNewton Newton Force = 1000 * MilliNewton KiloNewton Force = 1000 * Newton MegaNewton Force = 1000 * KiloNewton GigaNewton Force = 1000 * MegaNewton EarthGravity Force = 9806650 * MicroNewton // Conversion between Newton and imperial units. // Pound is both a unit of mass and weight (force). The suffix Force is added // to disambiguate the measurement it represents. PoundForce Force = 4448221615 * NanoNewton )
Well known Force constants.
func (*Force) Set ¶
Set sets the Force to the value represented by s. Units are to be provided in "N", or "lbf" (Pound force) with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var f physic.Force if err := f.Set("9.8N"); err != nil { log.Fatal(f) } fmt.Println(f) if err := f.Set("20lbf"); err != nil { log.Fatal(f) } fmt.Println(f) }
Output: 9.800N 88.964N
type Frequency ¶
type Frequency int64
Frequency is a measurement of cycle per second, stored as an int64 micro Hertz.
The highest representable value is 9.2THz.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(10 * physic.MilliHertz) fmt.Println(101010 * physic.MilliHertz) fmt.Println(10 * physic.MegaHertz) fmt.Println(60 * physic.RPM) }
Output: 10mHz 101.010Hz 10MHz 1Hz
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var pwm physic.Frequency flag.Var(&pwm, "pwm", "pwm frequency") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // NTSC color subcarrier. v := (315*physic.MegaHertz + 44) / 88 // Convert to float64 as hertz. f := float64(v) / float64(physic.Hertz) fmt.Println(v) fmt.Printf("%fHz\n", f) }
Output: 3.580MHz 3579545.454545Hz
const ( // Hertz is 1/s. MicroHertz Frequency = 1 MilliHertz Frequency = 1000 * MicroHertz Hertz Frequency = 1000 * MilliHertz KiloHertz Frequency = 1000 * Hertz MegaHertz Frequency = 1000 * KiloHertz GigaHertz Frequency = 1000 * MegaHertz TeraHertz Frequency = 1000 * GigaHertz // RPM is revolutions per minute. It is used to quantify angular frequency. RPM Frequency = 16667 * MicroHertz )
Well known Frequency constants.
func PeriodToFrequency ¶
PeriodToFrequency returns the frequency for a period of this interval.
A 0s period returns a 0Hz frequency.
Example ¶
package main import ( "fmt" "time" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(physic.PeriodToFrequency(time.Microsecond)) fmt.Println(physic.PeriodToFrequency(time.Minute)) }
Output: 1MHz 16.667mHz
func (Frequency) Period ¶
Period returns the duration of one cycle at this frequency.
Frequency above GigaHertz cannot be represented as Duration.
A 0Hz frequency returns a 0s period.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(physic.MilliHertz.Period()) fmt.Println(physic.MegaHertz.Period()) }
Output: 16m40s 1µs
func (*Frequency) Set ¶
Set sets the Frequency to the value represented by s. Units are to be provided in "Hz" or "rpm" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Unlike most Set() functions, "Hz" is assumed by default.
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var f physic.Frequency if err := f.Set("10MHz"); err != nil { log.Fatal(err) } fmt.Println(f) if err := f.Set("10mHz"); err != nil { log.Fatal(err) } fmt.Println(f) if err := f.Set("1kHz"); err != nil { log.Fatal(err) } fmt.Println(f) }
Output: 10MHz 10mHz 1kHz
type LuminousFlux ¶
type LuminousFlux int64
LuminousFlux is a measurement of total quantity of visible light energy emitted with wavelength power weighted by a luminosity function which represents a model of the human eye's response to different wavelengths. The CIE 1931 luminosity function is the standard for lumens.
LuminousFlux is stored as nano lumens.
The highest representable value is 9.2Glm.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(18282 * physic.Lumen) }
Output: 18.282klm
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var l physic.LuminousFlux flag.Var(&l, "low", "mood light level") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // Typical output of a 7W LED. v := 450 * physic.Lumen // Convert to float64 as lumen. f := float64(v) / float64(physic.Lumen) fmt.Println(v) fmt.Printf("%.0f\n", f) }
Output: 450lm 450
const ( // Lumen is a unit of luminous flux. cd⋅sr NanoLumen LuminousFlux = 1 MicroLumen LuminousFlux = 1000 * NanoLumen MilliLumen LuminousFlux = 1000 * MicroLumen Lumen LuminousFlux = 1000 * MilliLumen KiloLumen LuminousFlux = 1000 * Lumen MegaLumen LuminousFlux = 1000 * KiloLumen GigaLumen LuminousFlux = 1000 * MegaLumen )
Well known LuminousFlux constants.
func (*LuminousFlux) Set ¶
func (f *LuminousFlux) Set(s string) error
Set sets the LuminousFlux to the value represented by s. Units are to be provided in "lm" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var l physic.LuminousFlux if err := l.Set("25mlm"); err != nil { log.Fatal(err) } fmt.Println(l) if err := l.Set("2.5Mlm"); err != nil { log.Fatal(err) } fmt.Println(l) }
Output: 25mlm 2.500Mlm
func (LuminousFlux) String ¶
func (f LuminousFlux) String() string
String returns the energy formatted as a string in Lumens.
type LuminousIntensity ¶
type LuminousIntensity int64
LuminousIntensity is a measurement of the quantity of visible light energy emitted per unit solid angle with wavelength power weighted by a luminosity function which represents the human eye's response to different wavelengths. The CIE 1931 luminosity function is the SI standard for candela.
LuminousIntensity is stored as nano candela.
This is one of the base unit in the International System of Units.
The highest representable value is 9.2Gcd.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(12 * physic.Candela) }
Output: 12cd
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var l physic.LuminousIntensity flag.Var(&l, "dusk", "light level to turn on light") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // A 7W LED generating 450lm in all directions (4π steradian) will have an // intensity of 450lm/4π = ~35.8cd. v := 35800 * physic.MilliCandela // Convert to float64 as candela. f := float64(v) / float64(physic.Candela) fmt.Println(v) fmt.Printf("%.1f\n", f) }
Output: 35.800cd 35.8
const ( // Candela is a unit of luminous intensity. cd NanoCandela LuminousIntensity = 1 MicroCandela LuminousIntensity = 1000 * NanoCandela MilliCandela LuminousIntensity = 1000 * MicroCandela Candela LuminousIntensity = 1000 * MilliCandela KiloCandela LuminousIntensity = 1000 * Candela MegaCandela LuminousIntensity = 1000 * KiloCandela GigaCandela LuminousIntensity = 1000 * MegaCandela )
Well known LuminousIntensity constants.
func (*LuminousIntensity) Set ¶
func (i *LuminousIntensity) Set(s string) error
Set sets the LuminousIntensity to the value represented by s. Units are to be provided in "cd" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var l physic.LuminousIntensity if err := l.Set("16cd"); err != nil { log.Fatal(err) } fmt.Println(l) }
Output: 16cd
func (LuminousIntensity) String ¶
func (i LuminousIntensity) String() string
String returns the energy formatted as a string in Candela.
type Mass ¶
type Mass int64
Mass is a measurement of mass stored as an int64 nano gram.
This is one of the base unit in the International System of Units.
The highest representable value is 9.2Gg.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(10 * physic.MilliGram) fmt.Println(physic.OunceMass) fmt.Println(physic.PoundMass) fmt.Println(physic.Slug) }
Output: 10mg 28.350g 453.592g 14.594kg
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var m physic.Mass flag.Var(&m, "weight", "amount of cat food to dispense") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // Weight of a Loonie (Canadian metal piece). v := 6270 * physic.MilliGram // Convert to float64 as gram. f := float64(v) / float64(physic.Gram) fmt.Println(v) fmt.Printf("%.2fg\n", f) }
Output: 6.270g 6.27g
const ( NanoGram Mass = 1 MicroGram Mass = 1000 * NanoGram MilliGram Mass = 1000 * MicroGram Gram Mass = 1000 * MilliGram KiloGram Mass = 1000 * Gram MegaGram Mass = 1000 * KiloGram GigaGram Mass = 1000 * MegaGram Tonne Mass = MegaGram // Conversion between Gram and imperial units. // Ounce is both a unit of mass, weight (force) or volume depending on // context. The suffix Mass is added to disambiguate the measurement it // represents. OunceMass Mass = 28349523125 * NanoGram // Pound is both a unit of mass and weight (force). The suffix Mass is added // to disambiguate the measurement it represents. PoundMass Mass = 16 * OunceMass Slug Mass = 14593903 * MilliGram )
Well known Mass constants.
func (*Mass) Set ¶
Set sets the Mass to the value represented by s. Units are to be provided in "g", "lb" or "oz" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var m physic.Mass if err := m.Set("10mg"); err != nil { log.Fatal(err) } fmt.Println(m) if err := m.Set("16.5kg"); err != nil { log.Fatal(err) } fmt.Println(m) if err := m.Set("2.2oz"); err != nil { log.Fatal(err) } fmt.Println(m) if err := m.Set("16lb"); err != nil { log.Fatal(err) } fmt.Println(m) }
Output: 10mg 16.500kg 62.369g 7.257kg
type Power ¶
type Power int64
Power is a measurement of power stored as a nano watts.
The highest representable value is 9.2GW.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(1 * physic.Watt) fmt.Println(16 * physic.MilliWatt) fmt.Println(1210 * physic.MegaWatt) }
Output: 1W 16mW 1.210GW
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var p physic.Power flag.Var(&p, "power", "heater maximum power") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // Maximum emitted power by Bluetooth class 2 device. v := 2500 * physic.MicroWatt // Convert to float64 as watt. f := float64(v) / float64(physic.Watt) fmt.Println(v) fmt.Printf("%.4f\n", f) }
Output: 2.500mW 0.0025
const ( // Watt is unit of power J/s, kg⋅m²⋅s⁻³ NanoWatt Power = 1 MicroWatt Power = 1000 * NanoWatt MilliWatt Power = 1000 * MicroWatt Watt Power = 1000 * MilliWatt KiloWatt Power = 1000 * Watt MegaWatt Power = 1000 * KiloWatt GigaWatt Power = 1000 * MegaWatt )
Well known Power constants.
func (*Power) Set ¶
Set sets the Power to the value represented by s. Units are to be provided in "W" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var p physic.Power if err := p.Set("25mW"); err != nil { log.Fatal(err) } fmt.Println(p) if err := p.Set("1W"); err != nil { log.Fatal(err) } fmt.Println(p) if err := p.Set("1.21GW"); err != nil { log.Fatal(err) } fmt.Println(p) }
Output: 25mW 1W 1.210GW
type Pressure ¶
type Pressure int64
Pressure is a measurement of force applied to a surface per unit area (stress) stored as an int64 nano Pascal.
The highest representable value is 9.2GPa.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(101010 * physic.Pascal) fmt.Println(101 * physic.KiloPascal) }
Output: 101.010kPa 101kPa
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var p physic.Pressure flag.Var(&p, "setpoint", "pressure for pump to maintain") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // A typical tire pressure in North America (33psi). v := 227526990 * physic.MicroPascal // Convert to float64 as pascal. f := float64(v) / float64(physic.Pascal) fmt.Println(v) fmt.Printf("%f\n", f) }
Output: 227.527Pa 227.526990
const ( // Pascal is N/m², kg/m/s². NanoPascal Pressure = 1 MicroPascal Pressure = 1000 * NanoPascal MilliPascal Pressure = 1000 * MicroPascal Pascal Pressure = 1000 * MilliPascal KiloPascal Pressure = 1000 * Pascal MegaPascal Pressure = 1000 * KiloPascal GigaPascal Pressure = 1000 * MegaPascal )
Well known Pressure constants.
func (*Pressure) Set ¶
Set sets the Pressure to the value represented by s. Units are to be provided in "Pa" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var p physic.Pressure if err := p.Set("300kPa"); err != nil { log.Fatal(err) } fmt.Println(p) if err := p.Set("16MPa"); err != nil { log.Fatal(err) } fmt.Println(p) }
Output: 300kPa 16MPa
type RelativeHumidity ¶
type RelativeHumidity int32
RelativeHumidity is a humidity level measurement stored as an int32 fixed point integer at a precision of 0.00001%rH.
Valid values are between 0% and 100%.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(506 * physic.MilliRH) fmt.Println(20 * physic.PercentRH) }
Output: 50.6%rH 20%rH
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var h physic.RelativeHumidity flag.Var(&h, "humidity", "green house humidity high alarm level") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // Yearly humidity average of Nadi, Fiji. v := 800 * physic.MilliRH // Convert to float64 as %. f := float64(v) / float64(physic.PercentRH) fmt.Println(v) fmt.Printf("%.0f\n", f) }
Output: 80%rH 80
const ( TenthMicroRH RelativeHumidity = 1 // 0.00001%rH MicroRH RelativeHumidity = 10 * TenthMicroRH // 0.0001%rH MilliRH RelativeHumidity = 1000 * MicroRH // 0.1%rH PercentRH RelativeHumidity = 10 * MilliRH // 1%rH )
Well known RelativeHumidity constants.
func (*RelativeHumidity) Set ¶
func (r *RelativeHumidity) Set(s string) error
Set sets the RelativeHumidity to the value represented by s. Units are to be provided in "%rH" or "%" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var r physic.RelativeHumidity if err := r.Set("50.6%rH"); err != nil { log.Fatal(err) } fmt.Println(r) if err := r.Set("20%"); err != nil { log.Fatal(err) } fmt.Println(r) }
Output: 50.6%rH 20%rH
func (RelativeHumidity) String ¶
func (r RelativeHumidity) String() string
String returns the humidity formatted as a string.
type SenseEnv ¶
type SenseEnv interface { conn.Resource // Sense returns the value read from the sensor. Unsupported metrics are not // modified. Sense(env *Env) error // SenseContinuous initiates a continuous sensing at the specified interval. // // It is important to call Halt() once done with the sensing, which will turn // the device off and will close the channel. SenseContinuous(interval time.Duration) (<-chan Env, error) // Precision returns this sensor's precision. // // The env values are set to the number of bits that are significant for each // items that this sensor can measure. // // Precision is not accuracy. The sensor may have absolute and relative // errors in its measurement, that are likely well above the reported // precision. Accuracy may be improved on some sensor by using oversampling, // or doing oversampling in software. Refer to its datasheet if available. Precision(env *Env) }
SenseEnv represents an environmental sensor.
type Speed ¶
type Speed int64
Speed is a measurement of magnitude of velocity stored as an int64 nano Metre per Second.
The highest representable value is 9.2Gm/s.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(10 * physic.MilliMetrePerSecond) fmt.Println(physic.LightSpeed) fmt.Println(physic.KilometrePerHour) fmt.Println(physic.MilePerHour) fmt.Println(physic.FootPerSecond) }
Output: 10mm/s 299.792Mm/s 277.778mm/s 447.040mm/s 304.800mm/s
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var s physic.Speed flag.Var(&s, "speed", "window shutter closing speed") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // Speed of light in vacuum. v := physic.LightSpeed // Convert to float64 as m/s. fms := float64(v) / float64(physic.MetrePerSecond) // Convert to float64 as km/h. fkh := float64(v) / float64(physic.KilometrePerHour) // Convert to float64 as mph. fmh := float64(v) / float64(physic.MilePerHour) fmt.Println(v) fmt.Printf("%.0fm/s\n", fms) fmt.Printf("%.1fkm/h\n", fkh) fmt.Printf("%.1fmph\n", fmh) }
Output: 299.792Mm/s 299792458m/s 1079252847.9km/h 670616629.4mph
const ( // MetrePerSecond is m/s. NanoMetrePerSecond Speed = 1 MicroMetrePerSecond Speed = 1000 * NanoMetrePerSecond MilliMetrePerSecond Speed = 1000 * MicroMetrePerSecond MetrePerSecond Speed = 1000 * MilliMetrePerSecond KiloMetrePerSecond Speed = 1000 * MetrePerSecond MegaMetrePerSecond Speed = 1000 * KiloMetrePerSecond GigaMetrePerSecond Speed = 1000 * MegaMetrePerSecond LightSpeed Speed = 299792458 * MetrePerSecond KilometrePerHour Speed = 277777778 * NanoMetrePerSecond MilePerHour Speed = 447040 * MicroMetrePerSecond FootPerSecond Speed = 304800 * MicroMetrePerSecond )
Well known Speed constants.
func (*Speed) Set ¶
Set sets the Speed to the value represented by s. Units are to be provided in "mps"(meters per second), "m/s", "kph", "fps", or "mph" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var s physic.Speed if err := s.Set("10m/s"); err != nil { log.Fatal(err) } fmt.Println(s) if err := s.Set("100kph"); err != nil { log.Fatal(err) } fmt.Println(s) if err := s.Set("2067fps"); err != nil { log.Fatal(err) } fmt.Println(s) if err := s.Set("55mph"); err != nil { log.Fatal(err) } fmt.Println(s) }
Output: 10m/s 27.778m/s 630.022m/s 24.587m/s
type Temperature ¶
type Temperature int64
Temperature is a measurement of hotness stored as a nano kelvin.
Negative values are invalid.
The highest representable value is 9.2GK.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { fmt.Println(0 * physic.Kelvin) fmt.Println(23010*physic.MilliCelsius + physic.ZeroCelsius) fmt.Println(80*physic.Fahrenheit + physic.ZeroFahrenheit) }
Output: -273.150°C 23.010°C 26.667°C
Example (Flag) ¶
package main import ( "flag" "periph.io/x/periph/conn/physic" ) func main() { var t physic.Temperature flag.Var(&t, "temp", "thermostat setpoint") flag.Parse() }
Output:
Example (Float64) ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // Normal average human body temperature. v := 37*physic.Celsius + physic.ZeroCelsius // Convert to float64 as Kelvin. f := float64(v) / float64(physic.Kelvin) fmt.Println(v) fmt.Printf("%.1fK\n", f) }
Output: 37°C 310.1K
const ( NanoKelvin Temperature = 1 MicroKelvin Temperature = 1000 * NanoKelvin MilliKelvin Temperature = 1000 * MicroKelvin Kelvin Temperature = 1000 * MilliKelvin KiloKelvin Temperature = 1000 * Kelvin MegaKelvin Temperature = 1000 * KiloKelvin GigaKelvin Temperature = 1000 * MegaKelvin // Conversion between Kelvin and Celsius. ZeroCelsius Temperature = 273150 * MilliKelvin MilliCelsius Temperature = MilliKelvin Celsius Temperature = Kelvin // Conversion between Kelvin and Fahrenheit. ZeroFahrenheit Temperature = 255372222222 * NanoKelvin MilliFahrenheit Temperature = 555555 * NanoKelvin Fahrenheit Temperature = 555555555 * NanoKelvin )
Well known Temperature constants.
func (Temperature) Celsius ¶
func (t Temperature) Celsius() float64
Celsius returns the temperature as a floating number of °Celsius.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // Normal average human body temperature. v := 37*physic.Celsius + physic.ZeroCelsius // Convert to float64 as Celsius. f := v.Celsius() fmt.Println(v) fmt.Printf("%.1f°C\n", f) }
Output: 37°C 37.0°C
func (Temperature) Fahrenheit ¶
func (t Temperature) Fahrenheit() float64
Fahrenheit returns the temperature as a floating number of °Fahrenheit.
Example ¶
package main import ( "fmt" "periph.io/x/periph/conn/physic" ) func main() { // Normal average human body temperature. v := 37*physic.Celsius + physic.ZeroCelsius // Convert to float64 as Fahrenheit. f := v.Fahrenheit() fmt.Println(v) fmt.Printf("%.1f°F\n", f) }
Output: 37°C 98.6°F
func (*Temperature) Set ¶
func (t *Temperature) Set(s string) error
Set sets the Temperature to the value represented by s. Units are to be provided in "C", "°C", "F", "°F" or "K" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".
Example ¶
package main import ( "fmt" "log" "periph.io/x/periph/conn/physic" ) func main() { var t physic.Temperature if err := t.Set("0°C"); err != nil { log.Fatal(err) } fmt.Println(t) if err := t.Set("1C"); err != nil { log.Fatal(err) } fmt.Println(t) if err := t.Set("5MK"); err != nil { log.Fatal(err) } fmt.Println(t) if err := t.Set("0°F"); err != nil { log.Fatal(err) } fmt.Println(t) if err := t.Set("32F"); err != nil { log.Fatal(err) } fmt.Println(t) }
Output: 0°C 1°C 5M°C -17.778°C 0°C
func (Temperature) String ¶
func (t Temperature) String() string
String returns the temperature formatted as a string in °Celsius.