Skip to content
Snippets Groups Projects
Commit f5cce3f8 authored by Pablo's avatar Pablo
Browse files

Proof of Concept:

   - Code für Ansteuern der LEDs mit Aruino mithilfe von Adarfruit_Neopixel Library
   - Code für I2C-Kommunikation von TI Launchpad zu Arduino
   - Skizze der korrekten Verkabelung
parent ed19e5af
No related branches found
No related tags found
No related merge requests found
# Overview
Werden wir mehrere Subfolder in code/ haben? Falls ja, wäre es nicht schlecht, hier eine kurze Übersicht über jeden
Subfolder zu schreiben.
\ No newline at end of file
# Function
Drive a WS2812B-LED strip from an Arduino Uno. Set the colors via I2C from a TI CC1350 Launchpad connected to the Arduino Uno.
This is a "prrof of concept" project to showcase how to drive the LED-strip and communicate via I2C from the launchpad to the Arduino.
# Wiring
A sketch of the wiring of the Launchpad, Arduino UNO, LED strip and the power source can be found in electric/wiring_LaunchpadArduinoLEDPowersource.png.
# Prerequisites
The Arduino relies on two libaries: Adafruit_Neopixel and Wire.
- Wire handles the I2C communication. It should be preinstalled wit the ArduinoIDE.
- Adafruit_Neopixel is a arduino driver library for the LED strip. It is not preinstalled.
- To install the library,, follow the instructions specified here: https://github.com/adafruit/Adafruit_NeoPixel
- download the latest release as a zip
- in the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library
\ No newline at end of file
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
/*
Dieses Programm verwendet die Adadruit Neopixel library, um eine WS2812B-LED-Leiste anzusteuern.
Die momentane Farbe wird durch ein per I2C verbundenes TI-Launchpad gesteuert. Dieses sendet eine Zahl 0-4 per I2C.
Die korrekte Verkabelung aller Komponenten (TI-Launchpad, Arduino UNO, Breadoard, LED-Strip, PowerSource)
ist in electric/wiring_LaunchpadArduinoLEDPowersource.png dargestellt.
*/
#define LED_DATA_PIN 6
#define NUMPIXELS 144
Adafruit_NeoPixel pixels(NUMPIXELS, LED_DATA_PIN, NEO_GRB + NEO_KHZ800);
struct RGB{
uint8_t red;
uint8_t green;
uint8_t blue;
};
struct RGB possibleColors[5] = {
{3, 0, 0},
{0, 3, 3},
{3, 0, 3},
{0, 3, 0},
{0, 0, 3}
};
RGB currentColor = possibleColors[0];
#define DELAYVAL 50
void setup() {
pixels.begin(); // INITIALIZE NeoPixel strip object
Wire.begin(4); // join i2c bus with slave address 4. The sender must be aware of this adress!
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
pixels.clear(); // Set all pixel colors to 'off'
for(int i=0; i<NUMPIXELS; i++) { // For each pixel...
// pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
// careful with this value, even something like 150 hurts my eyes..
pixels.setPixelColor(i, pixels.Color(currentColor.red, currentColor.green, currentColor.blue));
if(i-15>=0){
pixels.setPixelColor(i-15, pixels.Color(0, 0, 0));
}
pixels.show(); // Send the updated pixel colors to the hardware.
delay(DELAYVAL); // Pause before next pass through loop
}
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
Serial.println("Received I2C data!");
uint8_t x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
if(x>4){
Serial.println("Invalid number received! Only numbers 0-4 are valid");
}
else{
currentColor = possibleColors[x];
}
}
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <ti/drivers/GPIO.h>
#include <ti/drivers/I2C.h>
#include "Board.h"
/*
* Dieses Programm sendet eine Zahl aus der Menge {0, 1, 2, 3, 4} per I2C vom TI-Launchpad an demn Arduino.
* Durch Dr�cken des linken Buttons wechselt man die zu sendende Zahl.
* Druch Dr�cken des rechten Buttons l�st man das Senden der Zahl per I2C aus.
*
* Wichtig:
* - Die korrekte Verkabelung der beiden Boards findet man im Repo in electric/wiring_LaunchpadArduinoLEDPowersource.png
* - f�r den Arduino UNO gilt immer: SDA ist A4, SCL ist A5
* - f�r die Pinbelegung des TI CC1350 Launchpad gilt das, was in CC1350LAUNCH.h steht (zu finden mit z.B. strg+F I2C)
* - kann von Projekt zu Projekt anders sein, also immer doublechecken bei neuem Projekt
* - f�r dieses Projekt: SCL ist DI04 und SDA ist DI05
* - Die hier gesetzte I2C-Slaveadresse muss mit der im Arduinocode gesetzten Adresse (per Wire.begin(x)) �bereinstimmen
*/
void gpioButton0Callback(uint_least8_t index);
void gpioButton1Callback(uint_least8_t index);
void configureGPIOButtons();
void configureLEDs();
void setupI2CCommunication();
/* --------- global variables begin --------- */
// program logic
uint8_t numberToSend = 0;
bool shouldSendI2CMessage = false;
// I2C
I2C_Handle i2c;
I2C_Params i2cParams;
I2C_Transaction i2cTransaction;
uint8_t i2cTransmitBuffer[1] = {0};
/* --------- global variables end ---------*/
void gpioButton0Callback(uint_least8_t index)
{
numberToSend = (numberToSend+1) % 5;
}
void gpioButton1Callback(uint_least8_t index)
{
shouldSendI2CMessage = true;
}
void *mainThread(void *arg0)
{
GPIO_init();
I2C_init();
configureLEDs();
configureGPIOButtons();
setupI2CCommunication();
while(1){
if(shouldSendI2CMessage){
i2cTransmitBuffer[0] = numberToSend;
bool success = I2C_transfer(i2c, &i2cTransaction);
if(success){
// toggle green
GPIO_toggle(Board_GPIO_LED1);
sleep(1);
GPIO_toggle(Board_GPIO_LED1);
}
else{
// toggle red
GPIO_toggle(Board_GPIO_LED0);
sleep(1);
GPIO_toggle(Board_GPIO_LED0);
}
shouldSendI2CMessage = false;
sleep(1);
}
}
//I2C_close(i2c);
}
void configureGPIOButtons(){
// Configure buttons
GPIO_setConfig(Board_GPIO_BUTTON0, GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_FALLING);
GPIO_setConfig(Board_GPIO_BUTTON1, GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_FALLING);
/* install Button callback */
GPIO_setCallback(Board_GPIO_BUTTON0, gpioButton0Callback);
GPIO_setCallback(Board_GPIO_BUTTON1, gpioButton1Callback);
GPIO_enableInt(Board_GPIO_BUTTON0);
GPIO_enableInt(Board_GPIO_BUTTON1);
}
void configureLEDs(){
GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
GPIO_setConfig(Board_GPIO_LED1, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
}
void setupI2CCommunication(){
// Erzeuge I2C "Objekt"
I2C_Params_init(&i2cParams);
i2cParams.bitRate = I2C_100kHz;
i2cParams.transferMode = I2C_MODE_BLOCKING;
i2c = I2C_open(Board_I2C0, &i2cParams);
if (i2c == NULL) {
while (1);
}
// die I2C-Slaveadresse, wird im Arduino-Code gesetzt
uint8_t arduinoSlaveAdress = 4;
i2cTransaction.slaveAddress = arduinoSlaveAdress;
// mit writeCount bzw readCount wird festgelegt, wie vile Bytes per I2C geschrieben bzw gelesen werden
// da der Arduino nur Befehle erh�lt und mann nie vom Arduino lesen muss, ist read count 0
i2cTransaction.writeBuf = i2cTransmitBuffer;
i2cTransaction.writeCount = 1;
i2cTransaction.readCount = 0;
}
electric/wiring_LaunchpadArduinoLEDPowersource.png

78.3 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment