Skip to content

Commit

Permalink
Merge pull request #71 from bwhitman/alles2
Browse files Browse the repository at this point in the history
Alles 2
  • Loading branch information
bwhitman authored Jun 9, 2024
2 parents 3032f37 + bb6d9c6 commit bcf8d9c
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 153 deletions.
8 changes: 4 additions & 4 deletions alles-flashing.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ To do this, you need to set up the following things:

ESP-IDF is the set of open source tools and libraries that work on the CPU powering Alles, the ESP32. You should first install ESP-IDF on your system if you haven't already.

Download the supported version of ESP-IDF. That is currently 5.1-rc2. [You can download it directly here.](https://dl.espressif.com/github_assets/espressif/esp-idf/releases/download/v5.1-rc2/esp-idf-v5.1-rc2.zip) Unpack it to a folder. I like to keep them in `~/esp/`, as you'll likely want to use different versions eventually. So we'll assume it's in `~/esp/esp-idf-v5.1-rc2`.
Download the supported version of ESP-IDF. That is currently 5.2. [You can download it directly here.](https://dl.espressif.com/github_assets/espressif/esp-idf/releases/download/v5.2/esp-idf-v5.2.zip) Unpack it to a folder. I like to keep them in `~/esp/`, as you'll likely want to use different versions eventually. So we'll assume it's in `~/esp/esp-idf-v5.2`.

```
# install ESP-IDF that comes with our repository
~/esp/esp-idf-v5.1-rc2/install.sh esp32
source ~/esp/esp-idf-v5.1-rc2/export.sh
~/esp/esp-idf-v5.2/install.sh esp32
source ~/esp/esp-idf-v5.2/export.sh
```

If you have trouble, for more info, or for other platforms, you should follow [the detailed instructions to download and set up `esp-idf`](http://esp-idf.readthedocs.io/en/latest/get-started/).
Expand All @@ -44,7 +44,7 @@ The cable you may have received from us for the hardware speaker is charge only.

### Flash

Then, in the `esp` folder you created during installing the ESP-IDF above (e.g. `cd ~/esp`), run `. ./esp-idf-v5.1-rc2/export.sh`. Now, in the same terminal window (export.sh sets some environment variables), cd back into the alles repository folder you downloaded and run `idf.py flash` to build and flash to the board. It will take a couple of minutes and show you progress. The board will reboot into the latest firmware.
Then, in the `esp` folder you created during installing the ESP-IDF above (e.g. `cd ~/esp`), run `. ./esp-idf-v5.2/export.sh`. Now, in the same terminal window (export.sh sets some environment variables), cd back into the alles repository folder you downloaded and run `idf.py flash` to build and flash to the board. It will take a couple of minutes and show you progress. The board will reboot into the latest firmware.

(If the flashing process doesn't work, it's likely not finding your UART location. Type `ls /dev/*usb*` to find something like `/dev/tty.usbserial.XXXXX` or `/dev/cu.usbserialXXXX`. You'll want to find the tty that appears when you connect the speaker to computer. Copy this location and try flashing again with `idf.py -p /dev/YOUR_SERIAL_TTY flash`.)

Expand Down
23 changes: 14 additions & 9 deletions alles.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import socket, struct, datetime, os, time, sys
import socket, struct, datetime, os, time, sys, datetime
sys.path.append('amy')
import amy
from amy import *
Expand Down Expand Up @@ -28,20 +28,19 @@ def flush(retries=1):
transmit(send_buffer)
send_buffer = ""

def send(retries=1, **kwargs):
def alles_send(message, retries=1):
global send_buffer
m = message(**kwargs)
if(buffer_size > 0):
if(len(send_buffer + m) > buffer_size):
if(len(send_buffer + message) > buffer_size):
transmit(send_buffer, retries=retries)
send_buffer = m
send_buffer = message
else:
send_buffer = send_buffer + m
else:
transmit(m,retries=retries)
transmit(message,retries=retries)

# We override AMY's send function to send out to the mesh instead of locally
amy.override_send = send
amy.override_send = alles_send



Expand Down Expand Up @@ -118,6 +117,12 @@ def decode_battery_mask(mask):
if (mask & 0x80): level = 1
return(state, level)

def millis():
now = datetime.datetime.now()
midnight = datetime.datetime.combine(now.date(), datetime.time.min)
delta = now - midnight
milliseconds = (delta.total_seconds() * 1000) + (delta.microseconds / 1000)
return int(milliseconds)

def sync(count=10, delay_ms=100):
global sock
Expand All @@ -136,7 +141,7 @@ def sync(count=10, delay_ms=100):
if((tic - last_sent) > delay_ms):
time_sent[i] = millis()
#print ("sending %d at %d" % (i, time_sent[i]))
output = "s%di%dZ" % (time_sent[i], i)
output = "U%di%dZ" % (time_sent[i], i)
sock.sendto(output.encode('ascii'), get_multicast_group())
i = i + 1
last_sent = tic
Expand All @@ -147,7 +152,7 @@ def sync(count=10, delay_ms=100):
if(data[0] == '_'):
data = data[:-1]
try:
[_, client_time, sync_index, client_id, ipv4, battery] = re.split(r'[sicry]',data)
[_, client_time, sync_index, client_id, ipv4, battery] = re.split(r'[Uigry]',data)
except ValueError:
print("What! %s" % (data))
if(int(sync_index) <= i): # skip old ones from a previous run
Expand Down
2 changes: 1 addition & 1 deletion amy
Submodule amy updated from f2acbf to 33fb9b
9 changes: 8 additions & 1 deletion main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ idf_component_register(SRCS alles.c
buttons.c
sounds.c
power.c
../amy/src/log2_exp2.c
../amy/src/amy.c
../amy/src/custom.c
../amy/src/delay.c
../amy/src/patches.c
../amy/src/algorithms.c
../amy/src/oscillators.c
../amy/src/pcm.c
Expand All @@ -20,7 +24,10 @@ list(APPEND IDF_COMPONENTS
mdns
)

target_compile_definitions(${COMPONENT_TARGET} PUBLIC "-DALLES")
target_compile_options(${COMPONENT_TARGET} PUBLIC
-DALLES
-Wno-uninitialized
)

set_source_files_properties(alles_esp32.c alles.c ../amy/src/amy.c
PROPERTIES COMPILE_FLAGS
Expand Down
3 changes: 2 additions & 1 deletion main/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ CC = gcc
CFLAGS = -g -Wall -Wno-strict-aliasing -I$(AMY) -I.

OBJECTS = $(patsubst %.c, %.o, multicast_desktop.c alles_desktop.c alles.c sounds.c $(AMY)/algorithms.c $(AMY)/delay.c \
$(AMY)/amy.c $(AMY)/envelope.c $(AMY)/filters.c $(AMY)/oscillators.c $(AMY)/pcm.c $(AMY)/partials.c $(AMY)/libminiaudio-audio.c)
$(AMY)/amy.c $(AMY)/envelope.c $(AMY)/filters.c $(AMY)/oscillators.c $(AMY)/pcm.c $(AMY)/partials.c $(AMY)/libminiaudio-audio.c \
$(AMY)/log2_exp2.c $(AMY)/custom.c $(AMY)/patches.c)
HEADERS = alles.h $(wildcard amy/*.h)

UNAME_S := $(shell uname -s)
Expand Down
22 changes: 12 additions & 10 deletions main/alles.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ amy_err_t sync_init() {



void update_map(uint8_t client, uint8_t ipv4, int64_t time) {
void update_map(int16_t client, uint8_t ipv4, int64_t time) {
// I'm called when I get a sync response or a regular ping packet
// I update a map of booted devices.

Expand Down Expand Up @@ -72,7 +72,7 @@ void handle_sync(int64_t time, int8_t index) {
// Before I send, i want to update the map locally
update_map(client_id, ipv4_quartet, sysclock);
// Send back sync message with my time and received sync index and my client id & battery status (if any)
sprintf(message, "_s%lldi%dc%dr%dy%dZ", sysclock, index, client_id, ipv4_quartet, battery_mask);
sprintf(message, "_U%lldi%dg%dr%dy%dZ", sysclock, index, client_id, ipv4_quartet, battery_mask);
mcast_send(message, strlen(message));
// Update computed delta (i could average these out, but I don't think that'll help too much)
//int64_t old_cd = computed_delta;
Expand All @@ -81,10 +81,11 @@ void handle_sync(int64_t time, int8_t index) {
//if(old_cd != computed_delta) printf("Changed computed_delta from %lld to %lld on sync\n", old_cd, computed_delta);
}

// It's ok that r & y are used by AMY, this is only to return values
void ping(int64_t sysclock) {
char message[100];
//printf("[%d %d] pinging with %lld\n", ipv4_quartet, client_id, sysclock);
sprintf(message, "_s%lldi-1c%dr%dy%dZ", sysclock, client_id, ipv4_quartet, battery_mask);
sprintf(message, "_U%lldi-1g%dr%dy%dZ", sysclock, client_id, ipv4_quartet, battery_mask);
update_map(client_id, ipv4_quartet, sysclock);
mcast_send(message, strlen(message));
last_ping_time = sysclock;
Expand All @@ -101,25 +102,26 @@ void alles_parse_message(char *message, uint16_t length) {
uint16_t c = 0;

// Parse the AMY stuff out of the message first
struct i_event e = amy_parse_message(message);
struct event e = amy_parse_message(message);
uint8_t sync_response = 0;

// Then pull out any alles-specific modes in this message - c,i,r,s, _
// Then pull out any alles-specific modes in this message
while(c < length+1) {
uint8_t b = message[c];
if(b == '_' && c==0) sync_response = 1;
if( ((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')) || b == 0) { // new mode or end
if(mode=='c') client = atoi(message + start);
if(mode=='i') sync_index = atoi(message + start);
if(mode=='r') ipv4=atoi(message + start);
if(mode=='s') sync = atol(message + start);
if(mode=='g') client = atoi(message + start);
if(sync_response) if(mode=='i') sync_index = atoi(message + start);
if(sync_response) if(mode=='r') ipv4=atoi(message + start);
if(mode=='U') sync = atol(message + start);
mode = b;
start = c + 1;
}
c++;
}
if(sync_response) {
// If this is a sync response, let's update our local map of who is booted
//printf("got sync response client %d ipv4 %d sync %lld\n", client, ipv4, sync);
update_map(client, ipv4, sync);
length = 0; // don't need to do the rest
}
Expand Down Expand Up @@ -150,7 +152,7 @@ void alles_parse_message(char *message, uint16_t length) {
if(client_id % (client-255) == 0) for_me = 1;
}
}
if(for_me) amy_add_i_event(e);
if(for_me) amy_add_event(e);
}
}
}
4 changes: 2 additions & 2 deletions main/alles.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "driver/gpio.h"


#define MAX_TASKS 9
#define MAX_TASKS 8

// Pins & buttons
#define BUTTON_WAKEUP 34
Expand Down Expand Up @@ -94,7 +94,7 @@ extern int16_t client_id;
void ping(int64_t sysclock);
amy_err_t sync_init();

extern void update_map(uint8_t client, uint8_t ipv4, int64_t time);
extern void update_map(int16_t client, uint8_t ipv4, int64_t time);
extern void handle_sync(int64_t time, int8_t index);
extern void mcast_send(char * message, uint16_t len);
#ifndef ESP_PLATFORM
Expand Down
6 changes: 3 additions & 3 deletions main/alles_desktop.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ uint8_t status = RUNNING;
uint8_t debug_on = 0;

// AMY synth states
extern struct state global;
extern struct state amy_global;
extern uint32_t event_counter;
extern uint32_t message_counter;
extern int16_t amy_device_id;
Expand All @@ -27,9 +27,9 @@ char *local_ip, *raw_file;

int main(int argc, char ** argv) {
sync_init();
amy_start();
amy_start(1,0,1);
amy_reset_oscs();
global.latency_ms = ALLES_LATENCY_MS;
amy_global.latency_ms = ALLES_LATENCY_MS;

// For now, indicate ip address via commandline
local_ip = (char*)malloc(sizeof(char)*1025);
Expand Down
Loading

0 comments on commit bcf8d9c

Please sign in to comment.