Skip to content

Commit

Permalink
Neopixel with single thread controller queue (#1368)
Browse files Browse the repository at this point in the history
* Neopixel with single thread controller queu

* forgot this one

* min updates to inmoov for new neopixel

* updated log statement
  • Loading branch information
supertick authored Nov 25, 2023
1 parent 3e3e911 commit 408fb76
Show file tree
Hide file tree
Showing 11 changed files with 850 additions and 588 deletions.
70 changes: 63 additions & 7 deletions src/main/java/org/myrobotlab/codec/CodecUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.myrobotlab.codec;

import java.awt.Color;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -188,7 +189,7 @@ public class CodecUtils {
private static final ObjectMapper mapper = new ObjectMapper();

/**
* The pretty printer to be used with {@link #mapper}
* The pretty printer to be used with {@link #mapper}
*/
private static final PrettyPrinter jacksonPrettyPrinter = new JacksonPrettyPrinter();

Expand Down Expand Up @@ -964,9 +965,8 @@ public static String getSafeReferenceName(String name) {
}

/**
* Serializes the specified object to JSON, using
* {@link #mapper} with {@link #jacksonPrettyPrinter} to pretty-ify the
* result.
* Serializes the specified object to JSON, using {@link #mapper} with
* {@link #jacksonPrettyPrinter} to pretty-ify the result.
*
* @param ret
* The object to be serialized
Expand Down Expand Up @@ -1095,12 +1095,13 @@ static public Message pathToMsg(String from, String path) {
// path parts less than 3 is a dir or ls
if (parts.length < 3) {
// this morphs a path which has less than 3 parts
// into a runtime "ls" method call to do reflection of services or service methods
// into a runtime "ls" method call to do reflection of services or
// service methods
// e.g. /clock -> /runtime/ls/"/clock"
// e.g. /clock/ -> /runtime/ls/"/clock/"

msg.method = "ls";
msg.data = new Object[] { "\"" + path + "\""};
msg.data = new Object[] { "\"" + path + "\"" };
return msg;
}

Expand Down Expand Up @@ -1483,7 +1484,8 @@ public static boolean isLocal(String name, String id) {
}

public static ServiceConfig readServiceConfig(String filename) throws IOException {
return readServiceConfig(filename, new StaticType<>() {});
return readServiceConfig(filename, new StaticType<>() {
});
}

/**
Expand Down Expand Up @@ -1629,4 +1631,58 @@ public static byte[] fromBase64(String input) {
return Base64.getDecoder().decode(input);
}

public static int[] getColor(String value) {
String hex = getColorHex(value);
if (hex != null) {
return hexToRGB(hex);
}
return hexToRGB(value);
}

public static List<String> getColorNames() {
Field[] colorFields = Color.class.getDeclaredFields();
List<String> colorNames = new ArrayList<>();

for (Field field : colorFields) {
if (field.getType().equals(Color.class)) {
colorNames.add(field.getName());
}
}
return colorNames;
}

public static String getColorHex(String colorName) {
Color color;
try {
color = (Color) Color.class.getField(colorName.toLowerCase()).get(null);
} catch (Exception e) {
return null;
}
return String.format("#%06X", (0xFFFFFF & color.getRGB()));
}

public static int[] hexToRGB(String hexValue) {
if (hexValue == null) {
return null;
}
int[] rgb = new int[3];
try {
// Check if the hex value starts with '#' and remove it if present
if (hexValue.startsWith("#")) {
hexValue = hexValue.substring(1);
}

if (hexValue.startsWith("0x")) {
hexValue = hexValue.substring(2);
}

// Parse the hex string into integers for red, green, and blue components
rgb[0] = Integer.parseInt(hexValue.substring(0, 2), 16); // Red
rgb[1] = Integer.parseInt(hexValue.substring(2, 4), 16); // Green
rgb[2] = Integer.parseInt(hexValue.substring(4, 6), 16); // Blue
} catch (NumberFormatException | StringIndexOutOfBoundsException e) {
log.error("Invalid hex color value {}", hexValue);
}
return rgb;
}
}
16 changes: 13 additions & 3 deletions src/main/java/org/myrobotlab/service/AudioFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -44,17 +45,17 @@
import org.myrobotlab.audio.AudioProcessor;
import org.myrobotlab.audio.PlaylistPlayer;
import org.myrobotlab.framework.Service;
import org.myrobotlab.framework.interfaces.Attachable;
import org.myrobotlab.io.FileIO;
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.LoggingFactory;
import org.myrobotlab.net.Http;
import org.myrobotlab.service.config.AudioFileConfig;
import org.myrobotlab.service.config.ServiceConfig;
import org.myrobotlab.service.data.AudioData;
import org.myrobotlab.service.interfaces.AudioControl;
import org.myrobotlab.service.interfaces.AudioListener;
import org.myrobotlab.service.interfaces.AudioPublisher;
import org.slf4j.Logger;
import java.util.Random;
/**
*
* AudioFile - This service can be used to play an audio file such as an mp3.
Expand Down Expand Up @@ -128,7 +129,16 @@ public class AudioFile extends Service<AudioFileConfig> implements AudioPublishe

final private transient PlaylistPlayer playlistPlayer = new PlaylistPlayer(this);


public void attach(Attachable attachable) {
if (attachable instanceof AudioListener) {
attachAudioListener(attachable.getName());
}
}

public void attach(AudioListener listener) {
attachAudioListener(listener.getName());
}

public void setPeakMultiplier(double peakMultiplier) {
AudioFileConfig c = (AudioFileConfig)config;
c.peakMultiplier = peakMultiplier;
Expand Down
22 changes: 4 additions & 18 deletions src/main/java/org/myrobotlab/service/InMoov2.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ public static boolean loadFile(String file) {

protected Long lastPirActivityTime;

protected LedDisplayData led = new LedDisplayData();

/**
* supported locales
*/
Expand Down Expand Up @@ -932,14 +930,8 @@ public void onPeak(double volume) {
* onPirOn flash neopixel
*/
public void onPirOn() {
led.action = "flash";
led.red = 50;
led.green = 100;
led.blue = 150;
led.count = 5;
led.interval = 500;
// FIXME flash on config.flashOnBoot
invoke("publishFlash");
invoke("publishFlash", "pir");
ProgramAB chatBot = (ProgramAB)getPeer("chatBot");
if (chatBot != null) {
String botState = chatBot.getPredicate("botState");
Expand Down Expand Up @@ -1233,18 +1225,12 @@ public String publishEvent(String event) {
*
* @return
*/
public LedDisplayData publishFlash() {
return led;
public String publishFlash(String flashName) {
return flashName;
}

public String publishHeartbeat() {
led.action = "flash";
led.red = 180;
led.green = 10;
led.blue = 30;
led.count = 1;
led.interval = 50;
invoke("publishFlash");
invoke("publishFlash", "heartbeat");
return getName();
}

Expand Down
Loading

0 comments on commit 408fb76

Please sign in to comment.