Skip to content

Commit

Permalink
update to 1.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Zontex committed Mar 18, 2021
1 parent adf3654 commit 260af76
Show file tree
Hide file tree
Showing 11 changed files with 1,472 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Eduponics/__pycache__/*
dist/*
micropython_eduponics.egg-info/*
.DS_Store
1 change: 1 addition & 0 deletions eduponics/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "1.0.7"
206 changes: 206 additions & 0 deletions eduponics/ads1x15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
# The MIT License (MIT)
#
# Copyright (c) 2016 Radomir Dopieralski (@deshipu),
# 2017 Robert Hammelrath (@robert-hh),
# 2020 STEMinds (@STEMinds)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import utime as time
from eduponics import mcp23017

_REGISTER_CONVERT = const(0x00)
_REGISTER_CONFIG = const(0x01)
_REGISTER_LOWTHRESH = const(0x02)
_REGISTER_HITHRESH = const(0x03)

_OS_SINGLE = const(0x8000) # Write: Set to start a single-conversion
_OS_NOTBUSY = const(0x8000) # Read: Bit=1 when no conversion is in progress

_MUX_DIFF_0_1 = const(0x0000) # Differential P = AIN0, N = AIN1 (default)
_MUX_DIFF_0_3 = const(0x1000) # Differential P = AIN0, N = AIN3
_MUX_DIFF_1_3 = const(0x2000) # Differential P = AIN1, N = AIN3
_MUX_DIFF_2_3 = const(0x3000) # Differential P = AIN2, N = AIN3
_MUX_SINGLE_0 = const(0x4000) # Single-ended AIN0
_MUX_SINGLE_1 = const(0x5000) # Single-ended AIN1
_MUX_SINGLE_2 = const(0x6000) # Single-ended AIN2
_MUX_SINGLE_3 = const(0x7000) # Single-ended AIN3

_PGA_6_144V = const(0x0000) # +/-6.144V range = Gain 2/3
_PGA_4_096V = const(0x0200) # +/-4.096V range = Gain 1
_PGA_2_048V = const(0x0400) # +/-2.048V range = Gain 2 (default)
_PGA_1_024V = const(0x0600) # +/-1.024V range = Gain 4
_PGA_0_512V = const(0x0800) # +/-0.512V range = Gain 8
_PGA_0_256V = const(0x0A00) # +/-0.256V range = Gain 16

_MODE_CONTIN = const(0x0000) # Continuous conversion mode
_MODE_SINGLE = const(0x0100) # Power-down single-shot mode (default)

_DR_128SPS = const(0x0000) # 128 /8 samples per second
_DR_250SPS = const(0x0020) # 250 /16 samples per second
_DR_490SPS = const(0x0040) # 490 /32 samples per second
_DR_920SPS = const(0x0060) # 920 /64 samples per second
_DR_1600SPS = const(0x0080) # 1600/128 samples per second (default)
_DR_2400SPS = const(0x00A0) # 2400/250 samples per second
_DR_3300SPS = const(0x00C0) # 3300/475 samples per second
_DR_860SPS = const(0x00E0) # - /860 samples per Second

_CMODE_TRAD = const(0x0000) # Traditional comparator with hysteresis (default)

_CPOL_ACTVLOW = const(0x0000) # ALERT/RDY pin is low when active (default)

_CLAT_NONLAT = const(0x0000) # Non-latching comparator (default)
_CLAT_LATCH = const(0x0004) # Latching comparator

_CQUE_1CONV = const(0x0000) # Assert ALERT/RDY after one conversions
# Disable the comparator and put ALERT/RDY in high state (default)
_CQUE_NONE = const(0x0003)

_GAINS = (
_PGA_6_144V, # 2/3x
_PGA_4_096V, # 1x
_PGA_2_048V, # 2x
_PGA_1_024V, # 4x
_PGA_0_512V, # 8x
_PGA_0_256V # 16x
)

_GAINS_V = (
6.144, # 2/3x
4.096, # 1x
2.048, # 2x
1.024, # 4x
0.512, # 8x
0.256 # 16x
)

_CHANNELS = {
(0, None): _MUX_SINGLE_0,
(1, None): _MUX_SINGLE_1,
(2, None): _MUX_SINGLE_2,
(3, None): _MUX_SINGLE_3,
(0, 1): _MUX_DIFF_0_1,
(0, 3): _MUX_DIFF_0_3,
(1, 3): _MUX_DIFF_1_3,
(2, 3): _MUX_DIFF_2_3,
}

_RATES = (
_DR_128SPS, # 128/8 samples per second
_DR_250SPS, # 250/16 samples per second
_DR_490SPS, # 490/32 samples per second
_DR_920SPS, # 920/64 samples per second
_DR_1600SPS, # 1600/128 samples per second (default)
_DR_2400SPS, # 2400/250 samples per second
_DR_3300SPS, # 3300/475 samples per second
_DR_860SPS # - /860 samples per Second
)


class ADS1115:
def __init__(self, i2c, address=0x48, gain=1, mcp_address=0x20):
self.i2c = i2c
self.address = address
self.mcp_address = mcp_address
self.gain = gain
self.temp2 = bytearray(2)
# define MCP for activating MOSFET pins
self.mcp = mcp23017.MCP23017(i2c=self.i2c, address=mcp_address)
# define all the pins for the mosfets
self.mcp_pins_sheet = {
0:8,
1:9,
2:10,
3:11
}

def _write_register(self, register, value):
self.temp2[0] = value >> 8
self.temp2[1] = value & 0xff
self.i2c.writeto_mem(self.address, register, self.temp2)

def _read_register(self, register):
self.i2c.readfrom_mem_into(self.address, register, self.temp2)
return (self.temp2[0] << 8) | self.temp2[1]

def raw_to_v(self, raw):
v_p_b = _GAINS_V[self.gain] / 32767
return raw * v_p_b

def set_conv(self, rate=4, channel1=0, channel2=None):
"""Set mode for read_rev"""
self.mode = (_CQUE_NONE | _CLAT_NONLAT |
_CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
_MODE_SINGLE | _OS_SINGLE | _GAINS[self.gain] |
_CHANNELS[(channel1, channel2)])

def read_raw(self, rate=4, channel1=0, channel2=None):
"""Read voltage between a channel and GND.
Time depends on conversion rate."""
self._write_register(_REGISTER_CONFIG, (_CQUE_NONE | _CLAT_NONLAT |
_CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
_MODE_SINGLE | _OS_SINGLE | _GAINS[self.gain] |
_CHANNELS[(channel1, channel2)]))
while not self._read_register(_REGISTER_CONFIG) & _OS_NOTBUSY:
time.sleep_ms(1)
res = self._read_register(_REGISTER_CONVERT)
return res if res < 32768 else res - 65536

def read_rev(self):
"""Read voltage between a channel and GND. and then start
the next conversion."""
res = self._read_register(_REGISTER_CONVERT)
self._write_register(_REGISTER_CONFIG, self.mode)
return res if res < 32768 else res - 65536

def alert_start(self, rate=4, channel1=0, channel2=None,
threshold_high=0x4000, threshold_low=0, latched=False) :
"""Start continuous measurement, set ALERT pin on threshold."""
self._write_register(_REGISTER_LOWTHRESH, threshold_low)
self._write_register(_REGISTER_HITHRESH, threshold_high)
self._write_register(_REGISTER_CONFIG, _CQUE_1CONV |
_CLAT_LATCH if latched else _CLAT_NONLAT |
_CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
_MODE_CONTIN | _GAINS[self.gain] |
_CHANNELS[(channel1, channel2)])

def conversion_start(self, rate=4, channel1=0, channel2=None):
"""Start continuous measurement, trigger on ALERT/RDY pin."""
self._write_register(_REGISTER_LOWTHRESH, 0)
self._write_register(_REGISTER_HITHRESH, 0x8000)
self._write_register(_REGISTER_CONFIG, _CQUE_1CONV | _CLAT_NONLAT |
_CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
_MODE_CONTIN | _GAINS[self.gain] |
_CHANNELS[(channel1, channel2)])

def alert_read(self):
"""Get the last reading from the continuous measurement."""
res = self._read_register(_REGISTER_CONVERT)
return res if res < 32768 else res - 65536

def read(self,pin):
# activate the mosfet
self.mcp.pin(self.mcp_pins_sheet[pin], mode=0, value=1)
time.sleep(0.1)
# read the data
raw = self.read_raw(channel1=pin)
voltage = self.raw_to_v(raw)
# deactivate mosfet after use
self.mcp.pin(self.mcp_pins_sheet[pin], mode=0, value=0)
return {"raw":raw,"voltage":voltage}
57 changes: 57 additions & 0 deletions eduponics/at24c02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
MicroPython TinyRTC I2C Module, DS1307 RTC + AT24C32N EEPROM
https://github.com/mcauser/micropython-tinyrtc
MIT License
Copyright (c) 2018 Mike Causer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

# AT24C32A, 32K (32768 kbit / 4 KB), 128 pages, 32 bytes per page, i2c addr 0x50
import machine
import time

class AT24C32N(object):
"""Driver for the AT24C32N 32K EEPROM."""

def __init__(self, i2c, i2c_addr=0x50, pages=128, bpp=32):
self.i2c = i2c
self.i2c_addr = i2c_addr
self.pages = pages
self.bpp = bpp # bytes per page

def capacity(self):
"""Storage capacity in bytes"""
return self.pages * self.bpp

def read(self, addr, nbytes):
"""Read one or more bytes from the EEPROM starting from a specific address"""
return self.i2c.readfrom_mem(self.i2c_addr, addr, nbytes, addrsize=16)

def write(self, addr, buf):
"""Write one or more bytes to the EEPROM starting from a specific address"""
offset = addr % self.bpp
partial = 0
# partial page write
if offset > 0:
partial = self.bpp - offset
self.i2c.writeto_mem(self.i2c_addr, addr, buf[0:partial], addrsize=16)
time.sleep_ms(5)
addr += partial
# full page write
for i in range(partial, len(buf), self.bpp):
self.i2c.writeto_mem(self.i2c_addr, addr+i-partial, buf[i:i+self.bpp], addrsize=16)
time.sleep_ms(5)
67 changes: 67 additions & 0 deletions eduponics/bh1750.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
MicroPython BH1750 light sensor module
https://github.com/STEMinds/eduponics-mini
MIT License
Copyright (c) 2020 STEMinds
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

import machine
import time

class BH1750():

def __init__(self):

# Define some constants from the datasheet

self.DEVICE = 0x5c # Default device I2C address

self.POWER_DOWN = 0x00 # No active state
self.POWER_ON = 0x01 # Power on
self.RESET = 0x07 # Reset data register value

# Start measurement at 4lx resolution. Time typically 16ms.
self.CONTINUOUS_LOW_RES_MODE = 0x13
# Start measurement at 1lx resolution. Time typically 120ms
self.CONTINUOUS_HIGH_RES_MODE_1 = 0x10
# Start measurement at 0.5lx resolution. Time typically 120ms
self.CONTINUOUS_HIGH_RES_MODE_2 = 0x11
# Start measurement at 1lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
self.ONE_TIME_HIGH_RES_MODE_1 = 0x20
# Start measurement at 0.5lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
self.ONE_TIME_HIGH_RES_MODE_2 = 0x21
# Start measurement at 1lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
self.ONE_TIME_LOW_RES_MODE = 0x23
# setup I2C
self.i2c = machine.I2C(scl=machine.Pin(15), sda=machine.Pin(4))

def convertToNumber(self, data):

# Simple function to convert 2 bytes of data
# into a decimal number
return ((data[1] + (256 * data[0])) / 1.2)

def readLight(self):

data = self.i2c.readfrom_mem(self.DEVICE,self.ONE_TIME_HIGH_RES_MODE_1,2)
return self.convertToNumber(data)
Loading

0 comments on commit 260af76

Please sign in to comment.