diff options
author | Jakob Stendahl <jakob@Jakobs-MacBook-Pro.local> | 2016-09-20 00:08:31 +0200 |
---|---|---|
committer | Jakob Stendahl <jakob@Jakobs-MacBook-Pro.local> | 2016-09-20 00:08:31 +0200 |
commit | 87f1ce2ef934d476ceb715bc291fcb5a274d3bba (patch) | |
tree | 4655cbb129ee586c98bccf4fd8449a8a8fe8a2cb | |
parent | d3c16fd665c98e1ec51e8b817ed2ffe3a774f7e8 (diff) | |
download | i2c-Neopixel-87f1ce2ef934d476ceb715bc291fcb5a274d3bba.tar.gz i2c-Neopixel-87f1ce2ef934d476ceb715bc291fcb5a274d3bba.zip |
initial commit
-rw-r--r-- | Arduino/i2cNeopixelSlave.ino | 95 | ||||
-rw-r--r-- | Raspberry Pi/python copy.py | 45 | ||||
-rw-r--r-- | Raspberry Pi/python.py | 39 |
3 files changed, 179 insertions, 0 deletions
diff --git a/Arduino/i2cNeopixelSlave.ino b/Arduino/i2cNeopixelSlave.ino new file mode 100644 index 0000000..dd9f7e0 --- /dev/null +++ b/Arduino/i2cNeopixelSlave.ino @@ -0,0 +1,95 @@ +#include <Wire.h> +#include <Adafruit_NeoPixel.h> + + +#define PIN 3 +#define SLAVE_ADDRESS 0x04 + +// Parameter 1 = number of pixels in strip +// Parameter 2 = pin number (most are valid) +// Parameter 3 = pixel type flags, add together as needed: +// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) +// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) +// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) +// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) +Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, PIN, NEO_GRB + NEO_KHZ800); + +int number = 0; +int state = 0; + +void setup() { + + pinMode(13, OUTPUT); + Serial.begin(9600); + + Wire.begin(SLAVE_ADDRESS); + + Wire.onReceive(receiveData); + Wire.onRequest(sendData); + + strip.begin(); + strip.show(); // Initialize all pixels to 'off' + + Serial.println("Ready!"); + +} + +void loop() { + delay(100); +} + +void receiveData(int byteCount) { + int bytes[byteCount]; + int i = 0; + + while (Wire.available()) { + number = Wire.read(); + bytes[i] = number; + + Serial.println(number); + i++; + } + + switch (bytes[0]) { + + case 0x01: + Serial.println("Life is discovered"); + number = 1; + break; + + case 0x02: + /*long byteN = bytes[1]; + int byteState = bytes[2]; + int byteGreen = bytes[3]; + int byteRed = bytes[4]; + int byteBlue = bytes[5];*/ + Serial.println("0x02"); + + strip.setPixelColor(bytes[2], bytes[3], bytes[4], bytes[5]); + Serial.println(bytes[1]); + Serial.println(bytes[2]); + Serial.println(bytes[3]); + Serial.println(bytes[4]); + break; + + case 0x03: + strip.show(); + break; + + default: + Serial.println("Nothing New"); + break; + + } + +} + +void sendData() { + Wire.write(number); +} + +void sendString(int Data) { + Wire.write(Data); +} + + diff --git a/Raspberry Pi/python copy.py b/Raspberry Pi/python copy.py new file mode 100644 index 0000000..6a53c02 --- /dev/null +++ b/Raspberry Pi/python copy.py @@ -0,0 +1,45 @@ +import smbus +import time + +def version(): + print "i2cPixel is Version 1.0.0" + +def setAddress(address): + global arduinoAddress + arduinoAddress = address + +def setBus(n): + global bus + bus = smbus.SMBus(n) + +def greeting(): + """ Send heartbeat """ + bus.write_byte(arduinoAddress, 0x01) + + """ Wait for response """ + try: + response = bus.read_byte(arduinoAddress) + if response == 0x01: + returnMsg = True + else: + returnMsg = False + except: + returnMsg = False + """ Return if heartbeat was received """ + return returnMsg + +def setPixel(n, red, green, blue): + """ Send values for switching a pixel on """ + bus.write_block_data(arduinoAddress, 0x02, [n, red, green, blue]) + +def showPixel(): + bus.write_byte(arduinoAddress, 0x03) + +def waitForSensor(): + + while True: + try: + sensorData = bus.read_byte(arduinoAddress) + if sensorData = 0x02: + """ sjekk hvilken sensor """ + diff --git a/Raspberry Pi/python.py b/Raspberry Pi/python.py new file mode 100644 index 0000000..9189f8e --- /dev/null +++ b/Raspberry Pi/python.py @@ -0,0 +1,39 @@ +""" Imports """ +import smbus +import time +import i2cPixel + +""" Decalrations """ +pixels = 10 + +def hexToRgb(value): + value = value.lstrip('#') + lv = len(value) + return tuple(int(value[i:i+lv/3], 16) for i in range(0, lv, lv/3)) + +def main(): + + """ Setup i2c communication """ + i2cPixel.version() + i2cPixel.setBus(1) + i2cPixel.setAddress(0x04) + + """ Wait for heartbeat from Arduino """ + while True: + if i2cPixel.greeting(): + print "Arduino is Online" + break + +while True: + test = raw_input() + colour = hexToRgb(test) + + i = 0 + while i < 10: + i2cPixel.setPixel(i, colour[0], colour[1], colour[2]) + i = i + 1 + i2cPixel.showPixel() + + +""" Start script """ +main() |