Skip to content

Instantly share code, notes, and snippets.

@ChinYikMing
Last active November 15, 2023 10:17
Show Gist options
  • Save ChinYikMing/0f4060ee74a13c5566b99c3f1f0baf51 to your computer and use it in GitHub Desktop.
Save ChinYikMing/0f4060ee74a13c5566b99c3f1f0baf51 to your computer and use it in GitHub Desktop.
python_light_sensor.py
import time, sys, math
from grove.adc import ADC
__all__ = ["GroveLightSensor"]
st = 1#sleep time
class GroveLightSensor(object):
'''
Grove Light Sensor class
Args:
pin(int): number of analog pin/channel the sensor connected.
'''
def __init__(self, channel):
self.channel = channel
self.adc = ADC()
@property
def light(self):
'''
Get the light strength value, maximum value is 100.0%
Returns:
(int): ratio, 0(0.0%) - 1000(100.0%)
'''
value = self.adc.read(self.channel)
return value
Grove = GroveLightSensor
# detect unicode
one_byte_mask = 0b00000000
two_byte_mask = 0b11000000
three_byte_mask = 0b11100000
four_byte_mask = 0b11110000
def used_four_byte(bin_byte):
return (bin_byte & four_byte_mask) == four_byte_mask
def used_three_byte(bin_byte):
return (bin_byte & three_byte_mask) == three_byte_mask
def used_two_byte(bin_byte):
return (bin_byte & two_byte_mask) == two_byte_mask
def used_one_byte(bin_byte):
return (bin_byte & one_byte_mask) == one_byte_mask
def detect_bytes_n(sensor, n):
byte_arr = []
for _ in range(0, n):
one_byte = detect_one_byte(sensor)
byte_arr.append(int(format(int(one_byte, 2), '08b'), 2))
return byte_arr
def detect_bytes(sensor):
byte_msg = ""
byte_arr = []
#print('Detecting light...')
one_byte_str = detect_one_byte(sensor)
one_byte = int(format(int(one_byte_str, 2), '08b'), 2)
byte_arr.append(one_byte)
if used_four_byte(one_byte):
byte_arr.extend(detect_bytes_n(sensor, 3))
elif used_three_byte(one_byte):
byte_arr.extend(detect_bytes_n(sensor, 2))
elif used_two_byte(one_byte):
byte_arr.extend(detect_bytes_n(sensor, 1))
elif used_one_byte(one_byte):
pass
# ascii
if len(byte_arr) == 1 :
return byte_decode(one_byte_str)
# return ''.join(chr(b) for b in byte_arr)
# unicode
return bytearray(byte_arr).decode(encoding = 'utf-8')
def detect_one_byte(sensor):
byte_msg = ""
#print('Detecting light...')
for _ in range(0,8) :
if sensor.light >= 50:
byte_msg += '1'
else :
byte_msg += '0'
print('Light value: {0}'.format(sensor.light))
time.sleep(st)
return byte_msg
# return byte_decode(byte_msg)
def byte_decode(byte_msg):
#ord(A) == 65
#chr(65) == 'A'
# add bin format to string ex : 0b01000001
byte_msg = '0b' + byte_msg
c = int(byte_msg,2) # change string for binary to int decimal
return chr(c)
def detect_preamable(sensor):
byte_msg = ""
#print('Detecting light...')
for _ in range(0,16) :
if sensor.light >= 50:
byte_msg += '1'
else :
byte_msg += '0'
print('Light value: {0}'.format(sensor.light))
time.sleep(st)
if(byte_msg == '1010101010101010'):
return True
else:
return False
def main():
from grove.helper import SlotHelper
sh = SlotHelper(SlotHelper.ADC)
pin = sh.argv2pin()
num =''
msg = ''
sensor = GroveLightSensor(pin)
while sensor.light < 100:
continue
print('Ready')
while not detect_preamable(sensor):
print('Not preamable: ')
# length of message
for _ in range(0,2):
num += (byte_decode(detect_one_byte(sensor)))
for _ in range(0,int(num)):
# msg += (detect_one_byte(sensor))
msg += (detect_bytes(sensor))
print('Your msg is: ' + msg)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment