Skip to content

Supernight led demo #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/adc2backlight.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
LCD = require "lcd"

lcd = LCD:new(storm.i2c.EXT, 0x7c, storm.i2c.EXT, 0xc4)
cord.new(function() lcd:init(2,1) end)

cord.new(function()
while true do
storm.n.adcife_init()
val = storm.n.adcife_sample_an0(0)
rgb = storm.n.val2rgb(val)
red, green, blue = storm.n.val2rgb(val)
lcd:setBackColor(red, green, blue)
cord.await(storm.os.invokeLater, 100*storm.os.MILLISECOND)
end
end)

sh = require "stormsh"
sh.start()
cord.enter_loop()
35 changes: 35 additions & 0 deletions app/fsr_supernight.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "cord"

storm.io.set_mode(storm.io.OUTPUT, storm.io.D2)
storm.io.set_mode(storm.io.OUTPUT, storm.io.D3)

strip = storm.n.led_init(50, 0x10000, 0x1000)

cord.new(function()
while true do
storm.n.adcife_init()
val = storm.n.adcife_sample_an0(0)
if val < 2070 then
val = 2070
end
if val > 4000 then
val = 4000
end
val = val - 2070
level = 50 * val/(4000-2070)
red, green, blue = storm.n.val2rgb(val, level, 50)
for i=0,49 do
if i <= level then
storm.n.led_set(strip, i, red/8, green/8, blue/8)
else
storm.n.led_set(strip, i, 0, 0, 0)
end
end
storm.n.led_show(strip)
cord.await(storm.os.invokeLater, 100*storm.os.MILLISECOND)
end
end)

sh = require "stormsh"
sh.start()
cord.enter_loop()
24 changes: 24 additions & 0 deletions app/ledstrip.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--[[
Application for supernight LED strips
Authors: Jose Oyola, Jochem van Gaalen, Naren Vasanad
This application shows how supernight LEDs can be used
First : create an LED strip object using led_init
Second: set the LEDs with the index and the color
Note: color is from 0 to 31 since only 5 bits are used per color
Third : show the LEDs so that the strip gets updated
]]

require "cord" -- scheduler / fiber library

storm.io.set_mode(storm.io.OUTPUT, storm.io.D2)
storm.io.set_mode(storm.io.OUTPUT, storm.io.D3)

-- SCLK: D2 and SDO: D3
strip = storm.n.led_init(50, 0x10000, 0x1000)
storm.n.led_set(strip, 0, 31, 20, 0)
storm.n.led_set(strip, 2, 31, 10, 0)
storm.n.led_show(strip)

sh = require "stormsh"
sh.start()
cord.enter_loop() -- start event/sleep loop
235 changes: 234 additions & 1 deletion native.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
//Include some libs as C files into this file
#include "natlib/util.c"
#include "natlib/svcd.c"
#include "natlib/led.c"
#include "natlib/analog/analog.c"


////////////////// BEGIN FUNCTIONS /////////////////////////////

int contrib_fourth_root_m1000(lua_State *L) //mandatory signature
Expand All @@ -53,6 +53,73 @@ int contrib_fourth_root_m1000(lua_State *L) //mandatory signature
return 1; //one return value
}

// This function takes in a single value in the range of 2048 to 4096
// and converts it into an RGB value
// The function returns three values: red, green and blue
int contrib_val2rgb(lua_State *L)
{
int i;
double f, p, q, t, s, v;
int red, green, blue;
// Get the single input
int val = (int) luaL_checknumber(L, 1);
int cur_level = (int) luaL_checknumber(L, 2);
int max_level = (int) luaL_checknumber(L, 3);
// Multiply by 6 since there are 6 different possible ranges
// Also correcting for the ADC value that was between 2048 and 4096
//double h = 6*(val-2048)/2048.0;
double h = 6*cur_level/max_level;
// Just in case the ADC value was lower
if (h < 0) {
h = 0.0;
}
s = 1.0;
v = 255.0;

// Actual color converstion algorithm starts here
i = (int) h;
f = h - i; // factorial part of h
p = (unsigned char)(v * ( 1 - s ));
q = (unsigned char)(v * ( 1 - s * f ));
t = (unsigned char)(v * ( 1 - s * ( 1 - f ) ));

switch(i) {
case 0:
red = v;
green = t;
blue = p;
break;
case 1:
red = q;
green = v;
blue = p;
break;
case 2:
red = p;
green = v;
blue = t;
break;
case 3:
red = p;
green = q;
blue = v;
break;
case 4:
red = t;
green = p;
blue = v;
break;
default: // case 5:
red = v;
green = p;
blue = q;
break;
}
lua_pushnumber(L, red);
lua_pushnumber(L, green);
lua_pushnumber(L, blue);
return 3; //three return values
}

int contrib_run_foobar(lua_State *L)
{
Expand Down Expand Up @@ -99,6 +166,167 @@ int contrib_makecounter(lua_State *L)
return 1; //return the closure
}

/*
Authors: Jose Oyola, Jochem van Gaalen, Naren Vasanad
*Creates a new LED_Strip and returns it and sets all the LEDs to 0
*Format: size, sclk, sdo
*Format: uint32_t, uint32_t, uint16_t
*Return: struct LED_Strip * strip
*/
static int contrib_led_init(lua_State *L)
{
uint16_t size = lua_tonumber(L, 1);
uint32_t sclk = lua_tonumber(L, 2);
uint32_t sdo = lua_tonumber(L, 3);
struct LED_Strip * strip = LED_init(size, sclk, sdo);
lua_pushlightuserdata(L, strip);
return 1;
}

/*
Authors: Jose Oyola, Jochem van Gaalen, Naren Vasanad
*Shows the LEDs with the colors they've been set to
*Format: strip
*Format: struct LED_Strip *
*/
static int contrib_led_show(lua_State *L)
{
struct LED_Strip * strip = lua_touserdata(L, 1);
LED_show(strip);
return 0;
}

/*
Authors: Jose Oyola, Jochem van Gaalen, Naren Vasanad
*Sets a particular LED with a color
*Have to run show after this to actually update the colors
*Format: strip, index, reg, green, blue
*Format: struct LED_Strip *, uint32_t, char, char, char
*/
static int contrib_led_set(lua_State *L)
{
struct LED_Strip * strip = lua_touserdata(L, 1);
uint16_t index = lua_tonumber(L, 2);
char r = lua_tonumber(L, 3);
char g = lua_tonumber(L, 4);
char b = lua_tonumber(L, 5);
LED_set(strip, index, r, g, b);
return 0;
}

void delay_led(uint16_t ticks)
{
int i;
for(i=0;i<ticks;i++);
}

//This is a function to test the supernight led strip
//Actual implementation is in led.c
static int contrib_led_strip_write(lua_State *L)
{
// Initialization
uint32_t volatile * set = (uint32_t volatile *)(0x400E1000 + 0x0 + 0x54);
uint32_t volatile * clear = (uint32_t volatile *)(0x400E1000 + 0x0 + 0x58);

/////////////////////////////////////////////////////////////////////
// Variables that need to be passed to this function upon abstraction
uint32_t SCLK = 0x10000; //D2
uint32_t SDO = 0x1000; //D3

uint32_t nDots = 5; // number of lights in strip
char dr = (0x00 & 0x1f); // red data (0 - 31)
char dg = (0xff & 0x1f); // green data (0 - 31)
char db = (0x00 & 0x1f); // blue data (0 - 31)
//////////////////////////////////////////////////////////////////////

uint32_t ticks = 50;
char mask = 0x10; // mask for data transmission
uint32_t i,j,k;

*clear = SCLK;
delay_led(ticks);
*clear = SDO;
delay_led(ticks);
printf("Start of program\n");
for(i=0; i<32; i++){

// write 1 as start bit
*set = SCLK;
delay_led(ticks);
*clear = SCLK;
delay_led(ticks);
}

for (k=0; k<nDots; k++){
mask = 0x10;
*set = SDO;
delay_led(ticks);
*set = SCLK;
delay_led(ticks);
*clear = SCLK;
delay_led(ticks);
// output 5 bits of color data (red, green then blue)
for(j=0; j<5; j++){

if((mask & db) != 0) *set = SDO;
else *clear = SDO;
delay_led(ticks);

*set = SCLK;
delay_led(ticks);
*clear = SCLK;
delay_led(ticks);

mask >>= 1;
}
mask = 0x10;
for(j=0; j<5; j++){

if((mask & dr) != 0) *set = SDO;
else *clear = SDO;
delay_led(ticks);

*set = SCLK;
delay_led(ticks);
*clear = SCLK;
delay_led(ticks);

mask >>= 1;
}
mask = 0x10;
//printf("Inside loop\n");
for(j=0; j<5; j++){

if((mask & dg) != 0) *set = SDO;
else *clear = SDO;
delay_led(ticks);

*set = SCLK;
delay_led(ticks);
*clear = SCLK;
delay_led(ticks);

mask >>= 1;
}
}

// add pulse
*clear = SDO;
delay_led(ticks);

for(i=0; i<nDots;i++){
*set = SCLK;
delay_led(ticks);
*clear = SCLK;
delay_led(ticks);
}
printf("End of program\n");
// transport data finish
// Delay(); // replace?
// here add some delay, or transfer to something else, refresh after about 1/30 s
return 0;
}

/**
* Prints out hello world
*
Expand Down Expand Up @@ -177,8 +405,13 @@ const LUA_REG_TYPE contrib_native_map[] =
{ LSTRKEY( "hello" ), LFUNCVAL ( contrib_hello ) },
{ LSTRKEY( "helloX" ), LFUNCVAL ( contrib_helloX_entry ) },
{ LSTRKEY( "fourth_root"), LFUNCVAL ( contrib_fourth_root_m1000 ) },
{ LSTRKEY( "val2rgb"), LFUNCVAL ( contrib_val2rgb ) },
{ LSTRKEY( "run_foobar"), LFUNCVAL ( contrib_run_foobar ) },
{ LSTRKEY( "makecounter"), LFUNCVAL ( contrib_makecounter ) },
{ LSTRKEY( "led_init"), LFUNCVAL ( contrib_led_init ) },
{ LSTRKEY( "led_show"), LFUNCVAL ( contrib_led_show ) },
{ LSTRKEY( "led_set"), LFUNCVAL ( contrib_led_set ) },


SVCD_SYMBOLS
ADCIFE_SYMBOLS
Expand Down
Loading