Skip to content

Commit

Permalink
Added get_brightness, modified internal brightness to double to avoid…
Browse files Browse the repository at this point in the history
… crazy floaty imprecision
  • Loading branch information
Gadgetoid committed Dec 13, 2014
1 parent 728ef8e commit b202dac
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 26 deletions.
4 changes: 2 additions & 2 deletions python/UnicornHat/setup.py
Expand Up @@ -4,12 +4,12 @@

setup(
name = 'unicornhat',
version = '1.0.2',
version = '1.1.0',
author = 'Philip Howard',
author_email = 'phil@pimoroni.com',
url = 'https://github.com/pimoroni/ws2812-RPi/',
description = """The Python library for UnicornHat a matrix of 64, eye-burning, ws2812 LEDs!""",
long_description=open('README').read() + open('CHANGELOG').read(),
py_modules = [ 'unicornhat' ],
install_requires = ['ws2812'],
install_requires = ['ws2812 >= 1.1.0'],
)
7 changes: 7 additions & 0 deletions python/UnicornHat/unicornhat.py
Expand Up @@ -64,6 +64,13 @@ def brightness(b = 0.2):
return
ws2812.setBrightness(b)

def get_brightness():
'''
Get the display brightness value
Returns a float between 0.0 and 1.0
'''
return ws2812.getBrightness()

def clear():
'''
Clear the buffer
Expand Down
6 changes: 5 additions & 1 deletion python/ws2812/lib/ws2812-RPi.c
Expand Up @@ -152,7 +152,7 @@ void * map_peripheral(uint32_t base, uint32_t len) {
// =================================================================================================

// Set brightness
unsigned char setBrightness(float b) {
unsigned char setBrightness(double b) {
if(b < 0) {
printf("Brightness can't be set below 0.\n");
return false;
Expand All @@ -165,6 +165,10 @@ unsigned char setBrightness(float b) {
return true;
}

double getBrightness(){
return brightness;
}

// Zero out the PWM waveform buffer
void clearPWMBuffer(void) {
memset(PWMWaveform, 0, NUM_DATA_WORDS * 4); // Times four because memset deals in bytes.
Expand Down
5 changes: 3 additions & 2 deletions python/ws2812/lib/ws2812-RPi.h
Expand Up @@ -395,7 +395,7 @@ void * map_peripheral(uint32_t base, uint32_t len);

// Brightness - I recommend 0.2 for direct viewing at 3.3v.
#define DEFAULT_BRIGHTNESS 0.2
float brightness;
double brightness;

// LED buffer (this will be translated into pulses in PWMWaveform[])
typedef struct {
Expand All @@ -414,7 +414,8 @@ Color_t LEDBuffer[LED_BUFFER_LENGTH];
// 1024 (4096 bytes) should be enough for over 400 elements. It can be bumped up if you need more!
unsigned int PWMWaveform[NUM_DATA_WORDS];

unsigned char setBrightness(float b);
unsigned char setBrightness(double b);
double getBrightness();
void clearPWMBuffer(void);
void clear(void);
void clearLEDBuffer(void);
Expand Down
2 changes: 1 addition & 1 deletion python/ws2812/setup.py
Expand Up @@ -18,7 +18,7 @@

setup(
name = 'ws2812',
version = '1.0.0',
version = '1.1.0',
author = 'Philip Howard',
author_email = 'phil@pimoroni.com',
url = 'https://github.com/pimoroni/ws2812-RPi/',
Expand Down
3 changes: 2 additions & 1 deletion python/ws2812/ws2812-RPi.i
Expand Up @@ -27,7 +27,8 @@ extern void rainbow(uint8_t wait);
extern void rainbowCycle(uint8_t wait);
extern void theaterChase(Color_t c, uint8_t wait);
extern void theaterChaseRainbow(uint8_t wait);
extern unsigned char setBrightness(float b);
extern unsigned char setBrightness(double b);
extern double getBrightness();

extern Color_t RGB2Color(unsigned char r, unsigned char g, unsigned char b);
extern Color_t Color(unsigned char r, unsigned char g, unsigned char b);
Expand Down
39 changes: 20 additions & 19 deletions python/ws2812/ws2812-RPi_wrap.c
Expand Up @@ -3190,20 +3190,7 @@ SWIG_AsVal_int (PyObject * obj, int *val)
}


SWIGINTERN int
SWIG_AsVal_float (PyObject * obj, float *val)
{
double v;
int res = SWIG_AsVal_double (obj, &v);
if (SWIG_IsOK(res)) {
if ((v < -FLT_MAX || v > FLT_MAX)) {
return SWIG_OverflowError;
} else {
if (val) *val = (float)(v);
}
}
return res;
}
#define SWIG_From_double PyFloat_FromDouble


SWIGINTERN int
Expand Down Expand Up @@ -3632,18 +3619,18 @@ SWIGINTERN PyObject *_wrap_theaterChaseRainbow(PyObject *SWIGUNUSEDPARM(self), P

SWIGINTERN PyObject *_wrap_setBrightness(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
float arg1 ;
float val1 ;
double arg1 ;
double val1 ;
int ecode1 = 0 ;
PyObject * obj0 = 0 ;
unsigned char result;

if (!PyArg_ParseTuple(args,(char *)"O:setBrightness",&obj0)) SWIG_fail;
ecode1 = SWIG_AsVal_float(obj0, &val1);
ecode1 = SWIG_AsVal_double(obj0, &val1);
if (!SWIG_IsOK(ecode1)) {
SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "setBrightness" "', argument " "1"" of type '" "float""'");
SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "setBrightness" "', argument " "1"" of type '" "double""'");
}
arg1 = (float)(val1);
arg1 = (double)(val1);
result = (unsigned char)setBrightness(arg1);
resultobj = SWIG_From_unsigned_SS_char((unsigned char)(result));
return resultobj;
Expand All @@ -3652,6 +3639,19 @@ SWIGINTERN PyObject *_wrap_setBrightness(PyObject *SWIGUNUSEDPARM(self), PyObjec
}


SWIGINTERN PyObject *_wrap_getBrightness(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
double result;

if (!PyArg_ParseTuple(args,(char *)":getBrightness")) SWIG_fail;
result = (double)getBrightness();
resultobj = SWIG_From_double((double)(result));
return resultobj;
fail:
return NULL;
}


SWIGINTERN PyObject *_wrap_RGB2Color(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
unsigned char arg1 ;
Expand Down Expand Up @@ -4107,6 +4107,7 @@ static PyMethodDef SwigMethods[] = {
{ (char *)"theaterChase", _wrap_theaterChase, METH_VARARGS, NULL},
{ (char *)"theaterChaseRainbow", _wrap_theaterChaseRainbow, METH_VARARGS, NULL},
{ (char *)"setBrightness", _wrap_setBrightness, METH_VARARGS, NULL},
{ (char *)"getBrightness", _wrap_getBrightness, METH_VARARGS, NULL},
{ (char *)"RGB2Color", _wrap_RGB2Color, METH_VARARGS, NULL},
{ (char *)"Color", _wrap_Color, METH_VARARGS, NULL},
{ (char *)"setPixelColor", _wrap_setPixelColor, METH_VARARGS, NULL},
Expand Down
4 changes: 4 additions & 0 deletions python/ws2812/ws2812.py
Expand Up @@ -132,6 +132,10 @@ def setBrightness(*args):
return _ws2812.setBrightness(*args)
setBrightness = _ws2812.setBrightness

def getBrightness():
return _ws2812.getBrightness()
getBrightness = _ws2812.getBrightness

def RGB2Color(*args):
return _ws2812.RGB2Color(*args)
RGB2Color = _ws2812.RGB2Color
Expand Down

0 comments on commit b202dac

Please sign in to comment.