Module: Aio::Helpers

Defined in:
lib/aio/helpers.rb

Overview

This module adds most of the Arduino Reference functionality.

Check the Arduino Reference for more information about them.

Instance Method Summary collapse

Instance Method Details

#abs(num) ⇒ Object



44
45
46
# File 'lib/aio/helpers.rb', line 44

def abs(num)
	num > 0 ? num : -num
end

#bit(n) ⇒ Object



16
17
18
# File 'lib/aio/helpers.rb', line 16

def bit(n)
	2**n
end

#bitClear(num, pos) ⇒ Object



20
21
22
# File 'lib/aio/helpers.rb', line 20

def bitClear(num, pos)
	num & ~(0x01 << pos)
end

#bitRead(num, pos) ⇒ Object



28
29
30
# File 'lib/aio/helpers.rb', line 28

def bitRead(num, pos)
	(num & (0x01 << pos)) >> pos
end

#bitSet(num, pos) ⇒ Object



24
25
26
# File 'lib/aio/helpers.rb', line 24

def bitSet(num, pos)
	num | (0x01 << pos)
end

#bitWrite(num, pos, value) ⇒ Object



32
33
34
# File 'lib/aio/helpers.rb', line 32

def bitWrite(num, pos, value)
	value == 0 ? bitClear(num, pos) : bitSet(num, pos)
end

#constrain(value, a, b) ⇒ Object



72
73
74
75
76
# File 'lib/aio/helpers.rb', line 72

def constrain(value, a, b)
	return a if value < a
	return b if value > b
	value
end

#cos(rad) ⇒ Object



60
61
62
# File 'lib/aio/helpers.rb', line 60

def cos(rad)
	Math.cos(rad)
end

#highByte(num) ⇒ Object



12
13
14
# File 'lib/aio/helpers.rb', line 12

def highByte(num)
	num & 0xff00
end

#lowByte(num) ⇒ Object



8
9
10
# File 'lib/aio/helpers.rb', line 8

def lowByte(num)
	num & 0xff
end

#map(value, min0, max0, min1, max1) ⇒ Object



68
69
70
# File 'lib/aio/helpers.rb', line 68

def map(value, min0, max0, min1, max1)
	((value - min0) * (max1 - min1) / (max0 - min0)) + min0
end

#max(x, y) ⇒ Object



40
41
42
# File 'lib/aio/helpers.rb', line 40

def max(x, y)
	x < y ? y : x
end

#min(x, y) ⇒ Object



36
37
38
# File 'lib/aio/helpers.rb', line 36

def min(x, y)
	x < y ? x : y
end

#pow(base, exponent) ⇒ Object



48
49
50
# File 'lib/aio/helpers.rb', line 48

def pow(base, exponent)
	base ** exponent
end

#random(a, b = nil) ⇒ Object



78
79
80
# File 'lib/aio/helpers.rb', line 78

def random(a, b = nil)
	(a and b) ? rand(a..b) : rand(a)
end

#randomSeed(seed) ⇒ Object



82
83
84
# File 'lib/aio/helpers.rb', line 82

def randomSeed(seed)
	srand(seed)
end

#sin(rad) ⇒ Object



56
57
58
# File 'lib/aio/helpers.rb', line 56

def sin(rad)
	Math.sin(rad)
end

#sqrt(num) ⇒ Object



52
53
54
# File 'lib/aio/helpers.rb', line 52

def sqrt(num)
	Math.sqrt(num)
end

#tan(rad) ⇒ Object



64
65
66
# File 'lib/aio/helpers.rb', line 64

def tan(rad)
	Math.tan(rad)
end