This commit is contained in:
redfast00 2021-09-16 14:27:35 +02:00
parent 44ef5aa6b1
commit ec7290984e
No known key found for this signature in database
GPG key ID: 5946E0E34FD0553C
8 changed files with 26758 additions and 19 deletions

View file

@ -1,4 +1,5 @@
cmake_minimum_required(VERSION 3.7) cmake_minimum_required(VERSION 3.7)
set (CMAKE_CXX_STANDARD 20)
project( SharingLeds ) project( SharingLeds )

13
demo/0.lua Normal file
View file

@ -0,0 +1,13 @@
function all_leds(red, green, blue)
for bar=1,ledamount() do
led(bar, red, green, blue)
end
end
while true do
print(ledamount())
all_leds(255, 128, 0)
delay(1000)
all_leds(0, 128, 255)
delay(1000)
end

11
demo/1.lua Normal file
View file

@ -0,0 +1,11 @@
while true do
for i=1,ledamount() do
if i > 1 then
led(i - 1, 0, 0, 0)
end
led(i, 255, 0, 0)
delay(100)
end
led(ledamount(), 0, 0, 0)
end

26640
main/json.hpp Normal file

File diff suppressed because it is too large Load diff

View file

@ -50,7 +50,6 @@ private:
std::vector<std::string> buffer; std::vector<std::string> buffer;
std::stringstream os; std::stringstream os;
unsigned int size = 255; unsigned int size = 255;
bool last_char_is_newline = true;
}; };
#endif #endif

View file

@ -6,28 +6,41 @@
#include <csignal> #include <csignal>
#include <unordered_map> #include <unordered_map>
#include <atomic> #include <atomic>
#include <optional>
#include "lua.hpp" #include "lua.hpp"
#include "ws2811.h" #include "ws2811.h"
#include "interpreter_config.h" #include "interpreter_config.h"
#include "log.h" #include "log.h"
#include "json.hpp"
#include "httplib.h" #include "httplib.h"
using json = nlohmann::json;
#define TARGET_FREQ WS2811_TARGET_FREQ #define TARGET_FREQ WS2811_TARGET_FREQ
#define GPIO_PIN 18 #define GPIO_PIN 18
#define DMA 10 #define DMA 10
#define STRIP_TYPE WS2812_STRIP // WS2812/SK6812RGB integrated chip+leds #define STRIP_TYPE WS2812_STRIP // WS2812/SK6812RGB integrated chip+leds
#define LED_COUNT 690 #define LED_COUNT 30*5
#define FPS 15 #define FPS 15
int frametime = 1000 / FPS; int frametime = 1000 / FPS;
struct LedSegment {
std::optional<std::thread> lua_thread;
InterpreterConfig* interpreter_config;
std::string owner;
std::string lua_code;
};
std::vector<LedSegment*> ledsegments;
// this map is used to be able to see allowed leds from lua
std::unordered_map<lua_State*, InterpreterConfig*> statemap; std::unordered_map<lua_State*, InterpreterConfig*> statemap;
std::atomic<std::uint64_t> framecounter = {0}; std::atomic<std::uint64_t> framecounter = {0};
ws2811_t ledstring = ws2811_t ledstring =
{ {
.freq = TARGET_FREQ, .freq = TARGET_FREQ,
@ -39,7 +52,7 @@ ws2811_t ledstring =
.invert = 0, .invert = 0,
.count = LED_COUNT, .count = LED_COUNT,
.strip_type = STRIP_TYPE, .strip_type = STRIP_TYPE,
.brightness = 255, .brightness = 10,
}, },
[1] = { [1] = {
.gpionum = 0, .gpionum = 0,
@ -97,7 +110,6 @@ extern "C" {
} }
unsigned int real_location = config->begin + virtual_location - 1; unsigned int real_location = config->begin + virtual_location - 1;
// TODO remove this debugging line // TODO remove this debugging line
printf("Setting led %d to %d %d %d\n", real_location, red, green, blue);
ledstring.channel[0].leds[real_location] = (red << 16) | (green << 8)| blue; ledstring.channel[0].leds[real_location] = (red << 16) | (green << 8)| blue;
return 0; return 0;
} }
@ -211,16 +223,62 @@ void starthttpserver() {
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
InterpreterConfig* config = new InterpreterConfig { unsigned int amount = 10;
.begin = 0, unsigned int leds_per_segment = LED_COUNT / amount;
.length = LED_COUNT, for (unsigned int i = 0; i < amount; i++) {
.enabled = true InterpreterConfig* config = new InterpreterConfig {
}; .begin = leds_per_segment*i,
.length = leds_per_segment,
.enabled = true
};
cout << config->begin << endl;
LedSegment* segment = new LedSegment {
.lua_thread = std::nullopt,
.interpreter_config = config,
.owner = "",
.lua_code = ""
};
ledsegments.push_back(segment);
}
svr.Get("/api/segments.json", [](const httplib::Request &, httplib::Response &res) {
json j;
int i = 0;
for (LedSegment* ledsegment : ledsegments) {
json o;
o["begin"] = ledsegment->interpreter_config->begin;
o["length"] = ledsegment->interpreter_config->length;
o["owner"] = ledsegment->owner;
o["code"] = ledsegment->lua_code;
o["id"] = i;
i++;
j.push_back(o);
}
res.set_content(j.dump(), "text/json");
});
svr.Put("/api/code.json", [](const httplib::Request &req, httplib::Response &res, const httplib::ContentReader &content_reader) {
cout << "put request" << endl;
if (req.is_multipart_form_data()) {
res.set_content("No multipart forms allowed", "text/plain");
return true;
} else {
content_reader([&](const char *raw_data, size_t data_length) {
std::string data(raw_data, data_length);
auto j = json::parse(data);
cout << j << endl;
LedSegment* selected = ledsegments.at(j["id"].get<unsigned int>());
if (selected->lua_thread.has_value()) {
// TODO kill lua thread
}
selected->owner = j["owner"].get<std::string>();
selected->lua_code = j["code"].get<std::string>();
selected->lua_thread = spawn_lua_tread(selected->lua_code.c_str(), selected->interpreter_config);
svr.Get("/hi", [](const httplib::Request &, httplib::Response &res) { return true;
res.set_content("Hello World!", "text/plain"); });
}
return true;
}); });
std::thread httpserverthread(starthttpserver); std::thread httpserverthread(starthttpserver);
@ -239,8 +297,6 @@ int main(int argc, char** argv)
ledstring.channel[0].leds[11] = 0x00002000; ledstring.channel[0].leds[11] = 0x00002000;
ledstring.channel[0].leds[12] = 0x00000020; ledstring.channel[0].leds[12] = 0x00000020;
std::thread t = spawn_lua_tread("while true do\nprint(\"begin\")\nled(2, 3, 4, 5)\ndelay(1000)\nled(2, 255, 128, 0)\ndelay(1000)\nprint(\"done\")\nend", config);
while (true) { while (true) {
ws2811_render(&ledstring); ws2811_render(&ledstring);
std::this_thread::sleep_for(std::chrono::milliseconds(frametime)); std::this_thread::sleep_for(std::chrono::milliseconds(frametime));

24
submit.py Normal file
View file

@ -0,0 +1,24 @@
import json
import requests
import time
for i in range(10):
try:
with open(f'demo/{i}.lua') as codefile:
c = codefile.read()
except:
continue
time.sleep(0.3)
j = {
"code": c,
"owner": "j",
"id": i
}
r = requests.put('http://10.1.0.212:8080/api/code.json', json=j)
print(r)
print(r.text)

View file

@ -1,5 +0,0 @@
print("hello world")
led(1, 255, 255, 3)
print("There are " .. tostring(ledamount()) .. " leds")
delay(500) -- Waits X millissecs
waitframes(5) -- waits X frames, where a 'frame' is defined in full refreshes