support for morse code beeps

This commit is contained in:
2026-05-18 23:57:50 +02:00
parent a6d7278735
commit 60b12ee507
4 changed files with 57 additions and 0 deletions

View File

@@ -124,6 +124,27 @@ static void luaArch_nnToLua(luaArch *arch, lua_State *L, size_t nnIdx) {
}
static int luaArch_computer_beep(lua_State *L) {
if(lua_type(L, 1) == LUA_TSTRING) {
nn_MorseBeep beep = {.frequency = 1000, .beepDuration = 200, .volume = 1};
beep.pattern = lua_tostring(L, 1);
if(lua_isnumber(L, 2)) {
beep.frequency = lua_tonumber(L, 2);
}
if(lua_isnumber(L, 3)) {
beep.beepDuration = lua_tonumber(L, 3);
}
if(lua_isnumber(L, 4)) {
beep.volume = lua_tonumber(L, 4);
}
if(beep.frequency < 20) beep.frequency = 20;
if(beep.beepDuration < 0) beep.beepDuration = 0;
if(beep.volume < 0) beep.volume = 0;
if(beep.frequency > 20000) beep.frequency = 20000;
if(beep.beepDuration > 5) beep.beepDuration = 5;
if(beep.volume > 1) beep.volume = 1;
nn_beepComputerMorse(luaArch_from(L)->computer, beep);
return 0;
}
nn_Beep beep = {.frequency = 1000, .duration = 1, .volume = 1};
if(lua_isnumber(L, 1)) {
beep.frequency = lua_tonumber(L, 1);

View File

@@ -536,6 +536,10 @@ void ne_env(nn_EnvironmentRequest *req) {
printf("beep: %f Hz %fs %.02f%%\n", req->beep.frequency, req->beep.duration, req->beep.volume*100);
return;
}
if(req->action == NN_ENV_BEEPMORSE) {
printf("morse beep: %s, %f Hz %fs %.02f%%\n", req->morseBeep.pattern, req->morseBeep.frequency, req->morseBeep.beepDuration, req->morseBeep.volume*100);
return;
}
if(req->action == NN_ENV_DRAWENERGY) {
accumulatedEnergyCost += req->energy;
totalEnergyLoss += req->energy;

View File

@@ -1430,6 +1430,16 @@ void nn_beepComputer(nn_Computer *computer, nn_Beep beep) {
computer->env.handler(&req);
}
void nn_beepComputerMorse(nn_Computer *computer, nn_MorseBeep beep) {
if(beep.beepDuration < 0) beep.beepDuration = 0;
nn_EnvironmentRequest req;
req.userdata = computer->env.userdata;
req.computer = computer;
req.action = NN_ENV_BEEPMORSE;
req.morseBeep = beep;
computer->env.handler(&req);
}
void nn_destroyComputer(nn_Computer *computer) {
nn_Context *ctx = &computer->universe->ctx;
nn_stopComputer(computer);

View File

@@ -393,17 +393,36 @@ typedef struct nn_Architecture {
extern size_t nn_ramSizes[8];
typedef struct nn_Beep {
// frequenc, in Hz
double frequency;
// duration, in seconds
double duration;
// 0 is mute, 1 is 100%
double volume;
} nn_Beep;
// Morse beep, like a normal beep except it follows a morse code pattern.
// . is a short sound
// - is a long sound, 2x in length
// a space is a pause, 2x in length and dead silent.
// Every sound, including actual pauses, have a 50ms pause between them.
typedef struct nn_MorseBeep {
const char *pattern;
// frequency, in Hz
double frequency;
// duration of a ., in seconds
double beepDuration;
// 0 is mute, 1 is 100%
double volume;
} nn_MorseBeep;
typedef enum nn_EnvironmentAction {
NN_ENV_DRAWENERGY,
NN_ENV_POWERON,
NN_ENV_POWEROFF,
NN_ENV_CRASHED,
NN_ENV_BEEP,
NN_ENV_BEEPMORSE,
} nn_EnvironmentAction;
typedef struct nn_EnvironmentRequest {
@@ -415,6 +434,8 @@ typedef struct nn_EnvironmentRequest {
double energy;
// for BEEP, information about the beep
nn_Beep beep;
// for BEEPMORSE, information about the beep
nn_MorseBeep morseBeep;
};
} nn_EnvironmentRequest;
@@ -517,6 +538,7 @@ void nn_clearCommonDeviceInfo(nn_CommonDeviceInfo *info);
bool nn_removeDeviceInfo(nn_Computer *computer, const char *addr);
void nn_beepComputer(nn_Computer *computer, nn_Beep beep);
void nn_beepComputerMorse(nn_Computer *computer, nn_MorseBeep beep);
// get the userdata pointer
void *nn_getComputerUserdata(nn_Computer *computer);