Skip to content

Commit

Permalink
Merge pull request #24 from Beardmix/tab/single_user
Browse files Browse the repository at this point in the history
Tab/single user
  • Loading branch information
OnizukaX committed Aug 9, 2018
2 parents 6c705c0 + 67fc90d commit 7d8bf79
Show file tree
Hide file tree
Showing 19 changed files with 368 additions and 132 deletions.
40 changes: 36 additions & 4 deletions periph_sw/nRF52_RGB_Strip/eeprom_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class Settings
public:
unsigned int num_pixels;
String device_name;
// TrafficMode indices, [1; num_pixels].
unsigned int traffic_front_lower;
unsigned int traffic_front_upper;
unsigned int traffic_rear_lower;
unsigned int traffic_rear_upper;

Settings()
{
Expand All @@ -24,6 +29,10 @@ class Settings
{
num_pixels = 50;
device_name = "MyFahrrad";
traffic_front_lower = 1;
traffic_rear_upper = num_pixels;
traffic_front_upper = int(num_pixels/4) - traffic_front_lower;
traffic_rear_lower = traffic_rear_upper - int(num_pixels/4);
}
};

Expand All @@ -34,10 +43,14 @@ class EEPROM_Handler
{
}

void static load(Settings &settings)
void configure(void)
{
// Initialize Nffs
Nffs.begin();
}

void static load(Settings &settings)
{
NffsFile file;
file.open(FILENAME, FS_ACCESS_READ);

Expand Down Expand Up @@ -67,7 +80,6 @@ class EEPROM_Handler

void static save(Settings &settings)
{
Nffs.begin();
NffsFile file;

Serial.println("Open " FILENAME " file to write ... ");
Expand All @@ -76,7 +88,11 @@ class EEPROM_Handler
{
file.seek(0);
write_setting(file, "num_pixels", String(settings.num_pixels));
write_setting(file, "device_name", settings.device_name);
write_setting(file, "d_name", settings.device_name);
write_setting(file, "traffic_fl", String(settings.traffic_front_lower));
write_setting(file, "traffic_fu", String(settings.traffic_front_upper));
write_setting(file, "traffic_rl", String(settings.traffic_rear_lower));
write_setting(file, "traffic_ru", String(settings.traffic_rear_upper));
file.close();
}
else
Expand Down Expand Up @@ -127,10 +143,26 @@ class EEPROM_Handler
{
settings.num_pixels = setting_val.toInt();
}
else if (setting_name == "device_name")
else if (setting_name == "d_name")
{
settings.device_name = setting_val;
}
else if (setting_name == "traffic_fl")
{
settings.traffic_front_lower = setting_val.toInt();
}
else if (setting_name == "traffic_fu")
{
settings.traffic_front_upper = setting_val.toInt();
}
else if (setting_name == "traffic_rl")
{
settings.traffic_rear_lower = setting_val.toInt();
}
else if (setting_name == "traffic_ru")
{
settings.traffic_rear_upper = setting_val.toInt();
}
else
{
Serial.println("Unknown Setting To Load");
Expand Down
72 changes: 48 additions & 24 deletions periph_sw/nRF52_RGB_Strip/led_lib.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#ifndef LED_H
#define LED_H

#define __ASSERT_USE_STDERR

#include <assert.h>
#include <Adafruit_NeoPixel.h>

#include "eeprom_handler.h"

class CtrlLED
{
public:
int pinDebug;
int pinData;
int numpixels;
const Settings * p_settings;

int valRed;
int valGreen;
Expand All @@ -19,11 +24,13 @@ class CtrlLED

Adafruit_NeoPixel strip;

CtrlLED(int pinData, int pinDebug)
CtrlLED(int pinData, int pinDebug, Settings const * const p_settings)
{
assert(NULL != p_settings);

this->pinDebug = pinDebug;
this->pinData = pinData;
this->numpixels = 1;
this->p_settings = p_settings;

// declare the ledPin as an OUTPUT:
pinMode(pinData, OUTPUT);
Expand All @@ -36,13 +43,12 @@ class CtrlLED
strip.begin(); // This initializes the NeoPixel library.
}

void configure(unsigned int numpixels)
void configure(void)
{
setRGB(255, 255, 255);
neoPixelType pixelType = NEO_GRB + NEO_KHZ800;

this->numpixels = numpixels;
strip.updateLength(numpixels);
strip.updateLength(this->p_settings->num_pixels);
strip.updateType(pixelType);
strip.setPin(this->pinData);
}
Expand Down Expand Up @@ -135,25 +141,25 @@ class CtrlLED
/* Dimmed multi chase. */
void dimmedMultiChase(unsigned int nbChases = 2, unsigned int trainLength = 5)
{
// Minimum parameter values.
nbChases = max(1, nbChases);
trainLength = max(1, trainLength);
// Constraint parameters.
nbChases = constrain(nbChases, 1, this->p_settings->num_pixels);
trainLength = constrain(trainLength, 1, this->p_settings->num_pixels);

// Compute useful info.
int offset = ((global_millis() % (int)period_ms) / (float)period_ms) * numpixels;
int gapBetweenChases = (this->numpixels / nbChases);
int offset = ((global_millis() % (int)period_ms) / (float)period_ms) * this->p_settings->num_pixels;
int gapBetweenChases = (this->p_settings->num_pixels / nbChases);
int intensityStep = 0xFF / trainLength;

this->setPixelsOff(); // Init: switch evertyhing off.

for (int chaseIdx = 0; chaseIdx < nbChases; ++chaseIdx)
{
// Get first chase
int leaderIdx = (offset + (chaseIdx * gapBetweenChases)) % this->numpixels;
int leaderIdx = (offset + (chaseIdx * gapBetweenChases)) % this->p_settings->num_pixels;
// Switch on the leds.
for (int followerIdx = 1; followerIdx < (trainLength + 1); ++followerIdx)
{
int ledIdx = (leaderIdx + followerIdx) % this->numpixels;
int ledIdx = (leaderIdx + followerIdx) % this->p_settings->num_pixels;
int ledIntensity = intensityStep * followerIdx;
strip.setPixelColor(ledIdx, rgbiToColor(valRed, valGreen, valBlue, ledIntensity));
}
Expand All @@ -164,8 +170,8 @@ class CtrlLED

void pileUp()
{
int offset = ((global_millis() % (int)period_ms) / (float)period_ms) * (numpixels);
for (int i = 0; i < numpixels; i++)
int offset = ((global_millis() % (int)period_ms) / (float)period_ms) * (this->p_settings->num_pixels);
for (int i = 0; i < this->p_settings->num_pixels; i++)
{
int intensity = 0;
if (i <= offset)
Expand All @@ -181,21 +187,21 @@ class CtrlLED
void modeRainbow(void)
{
// Determine parameters according to the number of configured leds and colors.
unsigned int nbChases = max(1, this->NB_PRESET_COLORS);
unsigned int trainLength = max(1, this->numpixels / nbChases);
unsigned int offset = ((global_millis() % (int)period_ms) / (float)period_ms) * numpixels;
unsigned int nbChases = constrain(this->NB_PRESET_COLORS, 1, this->p_settings->num_pixels);
unsigned int trainLength = constrain(this->p_settings->num_pixels / nbChases, 1, this->p_settings->num_pixels);
unsigned int offset = ((global_millis() % (int)period_ms) / (float)period_ms) * this->p_settings->num_pixels;

/* Two partitions have to be distinguished.
* Chases might have different trainLength due to a non-null rest of the divison numpixels/nbChases.
* Chases might have different trainLength due to a non-null rest of the divison this->p_settings->num_pixels/nbChases.
* The left-hand-side of the partition will have a (trainLength + 1) length to use the pixels left. */
unsigned int partitionIdx = floor(this->numpixels / nbChases);
unsigned int partitionIdx = floor(this->p_settings->num_pixels / nbChases);

this->setPixelsOff(); // Init: switch evertyhing off.

for (int chaseIdx = 0; chaseIdx < nbChases; ++chaseIdx)
{
Color rgb = this->presetColors[chaseIdx];
int leaderIdx = (offset + (chaseIdx * trainLength)) % this->numpixels; // Chase start.
int leaderIdx = (offset + (chaseIdx * trainLength)) % this->p_settings->num_pixels; // Chase start.
unsigned int lastIdx = leaderIdx + trainLength;
if (chaseIdx < partitionIdx) {
lastIdx += 1;
Expand All @@ -204,14 +210,32 @@ class CtrlLED
// Switch on the leds.
for (int i = leaderIdx; i < lastIdx; ++i)
{
int ledIdx = i % this->numpixels;
int ledIdx = i % this->p_settings->num_pixels;
strip.setPixelColor(ledIdx, rgbiToColor(rgb.r, rgb.g, rgb.b, 0xFF));
}
}

strip.show();
}

void modeTraffic(void)
{
this->setPixelsOff(); // Init: switch evertyhing off.

// Front.
for (int i = std::max(this->p_settings->traffic_front_lower - 1, 0U); i < this->p_settings->traffic_front_upper; ++i)
{
strip.setPixelColor(i, strip.Color(255, 255, 255));
}
// Rear.
for (int i = std::max(this->p_settings->traffic_rear_lower - 1, 0U); i < this->p_settings->traffic_rear_upper; ++i)
{
strip.setPixelColor(i, strip.Color(255, 0, 0));
}

strip.show();
}

void setTimeOffset(int utc_millis)
{
time_offset = utc_millis - millis();
Expand Down Expand Up @@ -254,7 +278,7 @@ class CtrlLED

void writeEach(uint32_t color)
{
for (int i = 0; i < numpixels; i++)
for (int i = 0; i < this->p_settings->num_pixels; i++)
{
strip.setPixelColor(i, color);
}
Expand All @@ -272,7 +296,7 @@ class CtrlLED
// Set all pixels off. Not calling show.
void setPixelsOff(void)
{
for (unsigned int i = 0; i < numpixels; ++i)
for (unsigned int i = 0; i < this->p_settings->num_pixels; ++i)
{
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
Expand Down
Loading

0 comments on commit 7d8bf79

Please sign in to comment.