Skip to content

Commit

Permalink
Script can now control baud rate and changing the script or serial po…
Browse files Browse the repository at this point in the history
…rt results in serial port reconnection. Fixes kuym#2, fixes kuym#3
  • Loading branch information
kuym committed Jan 15, 2014
1 parent c2b6f92 commit 8c99a5b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 14 deletions.
9 changes: 8 additions & 1 deletion Resources/devices/Denon-AVR-3806.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ end

function onEvent(event, value)

if(event == "key") then
if(event == "init") then

print("init:", value);
init();

elseif(event == "key") then

if(value == 11) then --volume down
print("volume down key");
Expand Down Expand Up @@ -133,6 +138,8 @@ end

--initialize here
init();
setSerialBaud(9600);

serialWrite("MV?\r"); --seed volume data by querying it

setStatus("Denon AVR-3806 pending.");
3 changes: 3 additions & 0 deletions Source/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
- (NSString*)serialPort;
- (void)setSerialPort:(NSString*)value;

- (int)baudRate;
- (void)setBaudRate:(int)baud;

- (NSString*)deviceModel;
- (void)setDeviceModel:(NSString*)value;

Expand Down
14 changes: 14 additions & 0 deletions Source/Model.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ @implementation AmpControllerModel
{
@private
NSString* _deviceStatus;
int _baud;
}

- (id)init
{
_deviceStatus = @"Disconnected";
_baud = 9600;
return(self);
}

Expand Down Expand Up @@ -78,6 +80,18 @@ - (void)setSerialPort:(NSString*)value
[self change:@"serialPort" on:self];
}

- (int)baudRate
{
return(_baud);
}

- (void)setBaudRate:(int)baud
{
_baud = baud;

[self change:@"baudRate" on:self];
}

- (NSString*)deviceModel
{
return([[NSUserDefaults standardUserDefaults] stringForKey:@"devicemodel"]);
Expand Down
44 changes: 33 additions & 11 deletions Source/Scripting.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ int luaSetStatus(lua_State* L)
return(0);
}

int luaSetSerialBaud(lua_State* L)
{
int n = lua_gettop(L);
if((n < 1) || !lua_isnumber(L, 1))
{
lua_pushstring(L, "setStatus must be called with an integer argument");
return(lua_error(L)); // longjmps, doesn't actually return
}

int baud = (int)lua_tointeger(L, 1);

Scripting* s = (__bridge Scripting*)lua_touserdata(L, lua_upvalueindex(1));
[s setBaudRate:baud];

return(0);
}

int luaSerialWrite(lua_State* L)
{
int n = lua_gettop(L);
Expand Down Expand Up @@ -156,6 +173,13 @@ - (void)unloadScript
}
}

void addGlobalMethod(lua_State* L, void* context, char const* name, lua_CFunction fn)
{
lua_pushlightuserdata(L, context);
lua_pushcclosure(L, fn, 1);
lua_setglobal(L, name);
}

- (BOOL)loadLuaScript:(NSString*)scriptPath
{
if(_luaState != 0)
Expand All @@ -179,17 +203,10 @@ - (BOOL)loadLuaScript:(NSString*)scriptPath
luaL_openlibs(_luaState); //load default libraries
lua_gc(_luaState, LUA_GCRESTART, 0);

lua_pushlightuserdata(_luaState, (__bridge void*)self);
lua_pushcclosure(_luaState, &luaPrint, 1);
lua_setglobal(_luaState, "print");

lua_pushlightuserdata(_luaState, (__bridge void*)self);
lua_pushcclosure(_luaState, &luaSetStatus, 1);
lua_setglobal(_luaState, "setStatus");

lua_pushlightuserdata(_luaState, (__bridge void*)self);
lua_pushcclosure(_luaState, &luaSerialWrite, 1);
lua_setglobal(_luaState, "serialWrite");
addGlobalMethod(_luaState, (__bridge void*)self, "print", &luaPrint);
addGlobalMethod(_luaState, (__bridge void*)self, "setStatus", &luaSetStatus);
addGlobalMethod(_luaState, (__bridge void*)self, "setSerialBaud", &luaSetSerialBaud);
addGlobalMethod(_luaState, (__bridge void*)self, "serialWrite", &luaSerialWrite);

ScriptLoadClosure* closure = (ScriptLoadClosure*)malloc(sizeof(ScriptLoadClosure));
closure->file = file;
Expand Down Expand Up @@ -262,6 +279,11 @@ - (void)setStatus:(NSString*)value
[_model setDeviceStatus:value];
}

- (void)setBaudRate:(int)baud
{
[_model setBaudRate:baud];
}

- (void)serialWrite:(NSData*)data
{
[_model postSerialOutput:data];
Expand Down
28 changes: 26 additions & 2 deletions Source/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ - (void)applicationDidFinishLaunching:(NSNotification*)aNotification
[_model onChanged:@"autostart" call:@selector(onUpdateAutoStart:) on:self];

[_model onChanged:@"serialPort" call:@selector(onSerialPortChanged:) on:self];
[_model onChanged:@"baudRate" call:@selector(onBaudRateChanged:) on:self];
[_model onChanged:@"serialOutput" call:@selector(onSerialOutput:) on:self];
[self openSerialPort:[_model serialPort] baudRate:9600]; //@@script controlled baud

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(onSystemWillSleep:) name:NSWorkspaceWillSleepNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(onSystemDidWake:) name:NSWorkspaceDidWakeNotification object:nil];
Expand Down Expand Up @@ -228,7 +228,22 @@ - (void)onSerialPortChanged:(NSNotification*)notification
[_serialPort unbindListener:self];
}

[self openSerialPort:[_model serialPort] baudRate:9600]; //@@script controlled baud
[self openSerialPort:[_model serialPort] baudRate:[_model baudRate]];
//poke script, let it reinitialize if necessary
[_model postGenericEvent:@"init" value:@"serialPort"];
}

- (void)onBaudRateChanged:(NSNotification*)notification
{
if(_serialPort != nil)
{
[_serialPort close];
[_serialPort unbindListener:self];
}

[self openSerialPort:[_model serialPort] baudRate:[_model baudRate]];

//no script event because baud rate changes are currently script-initiated only
}

- (void)onSerialInput:(NSNotification*)notification
Expand Down Expand Up @@ -369,6 +384,15 @@ - (id)initWithModel:(AmpControllerModel*)model
- (void)showWindow:(id)sender
{
[super showWindow:sender];

if([self window] != nil)
{
// set initial dropdown/popup state
[self generateSerialPortMenu];

// set initial device model state
[self generateDeviceModelMenu];
}
}

- (void)awakeFromNib
Expand Down

0 comments on commit 8c99a5b

Please sign in to comment.