# The start of a Nanoleaf library and a save problem

**URL:** <https://forum.electromage.com/t/the-start-of-a-nanoleaf-library-and-a-save-problem/1340>\
**Category:** Patterns and Code\
**Created:** [June 26, 2021, 4:11am UTC](https://forum.electromage.com/t/the-start-of-a-nanoleaf-library-and-a-save-problem/1340 "2021-06-26T04:11:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![UnstoppableDrew](https://forum.electromage.com/user_avatar/forum.electromage.com/unstoppabledrew/32/752_2.png) [@UnstoppableDrew](https://forum.electromage.com/u/UnstoppableDrew)\
**Post date:** [June 26, 2021, 4:11am UTC](https://forum.electromage.com/t/the-start-of-a-nanoleaf-library-and-a-save-problem/1340/1 "2021-06-26T04:11:31Z")

</div>

I’ve started working on a simple library for people like myself who have DIY Nanoleaf installations that will let you write patterns that operate at the per-panel level of granularity. So far it just has functions for turning a panel on, off, or toggle current stat, and one to check if a given pixel should be on or not. I’m still thinking of what things would be useful to add, and ways to do per-panel effects/animations. In the code below it’s toggling a random panel every 500ms. The video doesn’t quite match, when I shot it I was still trying to figure out how the timing works, after reviewing all the comments in the intro to PB programming pattern I figured it out.  
One issue I am having though is I can’t save. The preview generator doesn’t seem to be coping with my code, it’s stuck saying Generating with no progress, and when I mouse over the Save Changes button I get a red slash circle.

[![](https://img.youtube.com/vi/nTgnFrosdS4/maxresdefault.jpg "Pixelblaze library for Nanoleaf style panels") ](https://www.youtube.com/watch?v=nTgnFrosdS4)

> var numPanels = 12 // Set to the total number of panels in your set  
> var perPanel = 12 // How many LEDs per panel  
> var activePanels = array(numPanels)
> 
> function activatePanel(panel) {  
> activePanels[panel] = 1  
> }
> 
> function deactivatePanel(panel) {  
> activePanels[panel] = 0  
> }
> 
> function togglePanel(panel) {  
> activePanels[panel] = abs(1 - activePanels[panel])  
> }
> 
> function isLit(index) {  
> return activePanels[trunc(index/perPanel)]  
> }
> 
> elapsedMs = 0  
> export function beforeRender(delta) {  
> t1 = time(.1)  
> elapsedMs += delta  
> if (elapsedMs \> 500) {  
> togglePanel(trunc(random(numPanels)))  
> elapsedMs = 0  
> }  
> }
> 
> export function render(index) {  
> h = t1 + index/pixelCount  
> s = 1  
> v = isLit(index)  
> hsv(h, s, v)  
> }

---

<div class="post-metadata">

**Author:** ![wizard](https://forum.electromage.com/user_avatar/forum.electromage.com/wizard/32/4_2.png) [@wizard](https://forum.electromage.com/u/wizard)\
**Post date:** [June 28, 2021, 3:08pm UTC](https://forum.electromage.com/t/the-start-of-a-nanoleaf-library-and-a-save-problem/1340/2 "2021-06-28T15:08:36Z")

</div>

5 posts were split to a new topic: [Drops off wifi / can’t connect](https://forum.electromage.com/t/drops-off-wifi-cant-connect/1344)
