Initial import of Cocoa code

This commit is contained in:
Neil Co 2008-12-09 17:11:33 +00:00
parent 7d3b181094
commit f08ef9f1e8
17 changed files with 6685 additions and 0 deletions

25
cocoa/AppController.h Normal file
View file

@ -0,0 +1,25 @@
//
// AppController.h
// Current Cost
//
// Created by Neil on 05/12/2008.
// Copyright 2008 Neil Cowburn. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#include "NCCurrentCost.h"
#include "CurrentCostView.h"
@interface AppController : NSObject <NCCurrentCostDelegate> {
IBOutlet CurrentCostView *ccView;
IBOutlet NSButton *captureButton;
@private
NCCurrentCost *ccm;
BOOL continueLoop;
}
@property (nonatomic, retain) NCCurrentCost *ccm;
- (IBAction)startDataCapture:(id)sender;
@end

57
cocoa/AppController.m Normal file
View file

@ -0,0 +1,57 @@
//
// AppController.m
// Current Cost
//
// Created by Neil on 05/12/2008.
// Copyright 2008 Neil Cowburn. All rights reserved.
//
#import "AppController.h"
#include "NSString+Trim.h"
@interface AppController ()
- (void)readData:(id)anObject;
@end
@implementation AppController
@synthesize ccm;
- (id)init
{
if (self = [super init]) {
ccm = [[NCCurrentCost alloc] init:@"/dev/cu.usbserial-0000101D" withBaudRate:2400];
continueLoop = YES;
[ccm setDelegate:self];
}
return self;
}
- (IBAction)startDataCapture:(id)sender
{
continueLoop = YES;
[ccm open];
[NSThread detachNewThreadSelector:@selector(readData:) toTarget:self withObject:nil];
}
- (void)dataWasReceived:(NSXMLDocument *)xml
{
NSString *tmp = [NSString stringWithFormat:@"Energy Now: %@ Watts", [NSString stringByTrimmingLeadingZeros:[[[xml nodesForXPath:@"/msg/ch1/watts" error:nil] objectAtIndex:0] stringValue]]];
[ccView setWatts:tmp];
}
- (void)readData:(id)anObject
{
NSAutoreleasePool *thisPool = [[NSAutoreleasePool alloc] init];
while (continueLoop) {
[ccm read];
}
[thisPool release];
}
@end

Binary file not shown.

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,332 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 45;
objects = {
/* Begin PBXBuildFile section */
1DDD58160DA1D0A300B32029 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1DDD58140DA1D0A300B32029 /* MainMenu.xib */; };
285C3E860EE99338007971BE /* NCCurrentCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 285C3E850EE99338007971BE /* NCCurrentCost.m */; };
285C3E8A0EE9935F007971BE /* AppController.m in Sources */ = {isa = PBXBuildFile; fileRef = 285C3E890EE9935F007971BE /* AppController.m */; };
285C403D0EE9B42A007971BE /* NSString+Trim.m in Sources */ = {isa = PBXBuildFile; fileRef = 285C403C0EE9B42A007971BE /* NSString+Trim.m */; };
285C40990EE9C0C5007971BE /* CurrentCostView.m in Sources */ = {isa = PBXBuildFile; fileRef = 285C40980EE9C0C5007971BE /* CurrentCostView.m */; };
8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */; };
8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; };
8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
089C165DFE840E0CC02AAC07 /* English */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = English; path = English.lproj/InfoPlist.strings; sourceTree = "<group>"; };
1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
13E42FB307B3F0F600E4EEF1 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = "<absolute>"; };
1DDD58150DA1D0A300B32029 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/MainMenu.xib; sourceTree = "<group>"; };
285C3E840EE99338007971BE /* NCCurrentCost.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NCCurrentCost.h; sourceTree = "<group>"; };
285C3E850EE99338007971BE /* NCCurrentCost.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NCCurrentCost.m; sourceTree = "<group>"; };
285C3E880EE9935F007971BE /* AppController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppController.h; sourceTree = "<group>"; };
285C3E890EE9935F007971BE /* AppController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppController.m; sourceTree = "<group>"; };
285C403B0EE9B42A007971BE /* NSString+Trim.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+Trim.h"; sourceTree = "<group>"; };
285C403C0EE9B42A007971BE /* NSString+Trim.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+Trim.m"; sourceTree = "<group>"; };
285C40970EE9C0C5007971BE /* CurrentCostView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CurrentCostView.h; sourceTree = "<group>"; };
285C40980EE9C0C5007971BE /* CurrentCostView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CurrentCostView.m; sourceTree = "<group>"; };
29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = "<absolute>"; };
29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
32CA4F630368D1EE00C91783 /* Current Cost_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Current Cost_Prefix.pch"; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8D1107320486CEB800E47090 /* Current Cost.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Current Cost.app"; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
8D11072E0486CEB800E47090 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
1058C7A0FEA54F0111CA2CBB /* Linked Frameworks */ = {
isa = PBXGroup;
children = (
1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */,
);
name = "Linked Frameworks";
sourceTree = "<group>";
};
1058C7A2FEA54F0111CA2CBB /* Other Frameworks */ = {
isa = PBXGroup;
children = (
29B97324FDCFA39411CA2CEA /* AppKit.framework */,
13E42FB307B3F0F600E4EEF1 /* CoreData.framework */,
29B97325FDCFA39411CA2CEA /* Foundation.framework */,
);
name = "Other Frameworks";
sourceTree = "<group>";
};
19C28FACFE9D520D11CA2CBB /* Products */ = {
isa = PBXGroup;
children = (
8D1107320486CEB800E47090 /* Current Cost.app */,
);
name = Products;
sourceTree = "<group>";
};
285C3E870EE99340007971BE /* Framework */ = {
isa = PBXGroup;
children = (
285C3E840EE99338007971BE /* NCCurrentCost.h */,
285C3E850EE99338007971BE /* NCCurrentCost.m */,
);
name = Framework;
sourceTree = "<group>";
};
28D9A4520EEED38100C1FFE7 /* Categories */ = {
isa = PBXGroup;
children = (
285C403B0EE9B42A007971BE /* NSString+Trim.h */,
285C403C0EE9B42A007971BE /* NSString+Trim.m */,
);
name = Categories;
sourceTree = "<group>";
};
28D9A4530EEED39800C1FFE7 /* Views */ = {
isa = PBXGroup;
children = (
285C40970EE9C0C5007971BE /* CurrentCostView.h */,
285C40980EE9C0C5007971BE /* CurrentCostView.m */,
);
name = Views;
sourceTree = "<group>";
};
28D9A4540EEED3A600C1FFE7 /* Controllers */ = {
isa = PBXGroup;
children = (
285C3E880EE9935F007971BE /* AppController.h */,
285C3E890EE9935F007971BE /* AppController.m */,
);
name = Controllers;
sourceTree = "<group>";
};
28D9A4970EEED8BB00C1FFE7 /* Classes */ = {
isa = PBXGroup;
children = (
285C3E870EE99340007971BE /* Framework */,
28D9A4540EEED3A600C1FFE7 /* Controllers */,
28D9A4530EEED39800C1FFE7 /* Views */,
28D9A4520EEED38100C1FFE7 /* Categories */,
);
name = Classes;
sourceTree = "<group>";
};
29B97314FDCFA39411CA2CEA /* Current Cost */ = {
isa = PBXGroup;
children = (
28D9A4970EEED8BB00C1FFE7 /* Classes */,
29B97315FDCFA39411CA2CEA /* Other Sources */,
29B97317FDCFA39411CA2CEA /* Resources */,
29B97323FDCFA39411CA2CEA /* Frameworks */,
19C28FACFE9D520D11CA2CBB /* Products */,
);
name = "Current Cost";
sourceTree = "<group>";
};
29B97315FDCFA39411CA2CEA /* Other Sources */ = {
isa = PBXGroup;
children = (
32CA4F630368D1EE00C91783 /* Current Cost_Prefix.pch */,
29B97316FDCFA39411CA2CEA /* main.m */,
);
name = "Other Sources";
sourceTree = "<group>";
};
29B97317FDCFA39411CA2CEA /* Resources */ = {
isa = PBXGroup;
children = (
8D1107310486CEB800E47090 /* Info.plist */,
089C165CFE840E0CC02AAC07 /* InfoPlist.strings */,
1DDD58140DA1D0A300B32029 /* MainMenu.xib */,
);
name = Resources;
sourceTree = "<group>";
};
29B97323FDCFA39411CA2CEA /* Frameworks */ = {
isa = PBXGroup;
children = (
1058C7A0FEA54F0111CA2CBB /* Linked Frameworks */,
1058C7A2FEA54F0111CA2CBB /* Other Frameworks */,
);
name = Frameworks;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
8D1107260486CEB800E47090 /* Current Cost */ = {
isa = PBXNativeTarget;
buildConfigurationList = C01FCF4A08A954540054247B /* Build configuration list for PBXNativeTarget "Current Cost" */;
buildPhases = (
8D1107290486CEB800E47090 /* Resources */,
8D11072C0486CEB800E47090 /* Sources */,
8D11072E0486CEB800E47090 /* Frameworks */,
);
buildRules = (
);
dependencies = (
);
name = "Current Cost";
productInstallPath = "$(HOME)/Applications";
productName = "Current Cost";
productReference = 8D1107320486CEB800E47090 /* Current Cost.app */;
productType = "com.apple.product-type.application";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
29B97313FDCFA39411CA2CEA /* Project object */ = {
isa = PBXProject;
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "Current Cost" */;
compatibilityVersion = "Xcode 3.1";
hasScannedForEncodings = 1;
mainGroup = 29B97314FDCFA39411CA2CEA /* Current Cost */;
projectDirPath = "";
projectRoot = "";
targets = (
8D1107260486CEB800E47090 /* Current Cost */,
);
};
/* End PBXProject section */
/* Begin PBXResourcesBuildPhase section */
8D1107290486CEB800E47090 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */,
1DDD58160DA1D0A300B32029 /* MainMenu.xib in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
8D11072C0486CEB800E47090 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
8D11072D0486CEB800E47090 /* main.m in Sources */,
285C3E860EE99338007971BE /* NCCurrentCost.m in Sources */,
285C3E8A0EE9935F007971BE /* AppController.m in Sources */,
285C403D0EE9B42A007971BE /* NSString+Trim.m in Sources */,
285C40990EE9C0C5007971BE /* CurrentCostView.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin PBXVariantGroup section */
089C165CFE840E0CC02AAC07 /* InfoPlist.strings */ = {
isa = PBXVariantGroup;
children = (
089C165DFE840E0CC02AAC07 /* English */,
);
name = InfoPlist.strings;
sourceTree = "<group>";
};
1DDD58140DA1D0A300B32029 /* MainMenu.xib */ = {
isa = PBXVariantGroup;
children = (
1DDD58150DA1D0A300B32029 /* English */,
);
name = MainMenu.xib;
sourceTree = "<group>";
};
/* End PBXVariantGroup section */
/* Begin XCBuildConfiguration section */
C01FCF4B08A954540054247B /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
COPY_PHASE_STRIP = NO;
GCC_DYNAMIC_NO_PIC = NO;
GCC_ENABLE_FIX_AND_CONTINUE = YES;
GCC_MODEL_TUNING = G5;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Current Cost_Prefix.pch";
INFOPLIST_FILE = Info.plist;
INSTALL_PATH = "$(HOME)/Applications";
PRODUCT_NAME = "Current Cost";
};
name = Debug;
};
C01FCF4C08A954540054247B /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
GCC_MODEL_TUNING = G5;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Current Cost_Prefix.pch";
INFOPLIST_FILE = Info.plist;
INSTALL_PATH = "$(HOME)/Applications";
PRODUCT_NAME = "Current Cost";
};
name = Release;
};
C01FCF4F08A954540054247B /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
GCC_C_LANGUAGE_STANDARD = c99;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
ONLY_ACTIVE_ARCH = YES;
PREBINDING = NO;
SDKROOT = macosx10.5;
};
name = Debug;
};
C01FCF5008A954540054247B /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
GCC_C_LANGUAGE_STANDARD = c99;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
PREBINDING = NO;
SDKROOT = macosx10.5;
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
C01FCF4A08A954540054247B /* Build configuration list for PBXNativeTarget "Current Cost" */ = {
isa = XCConfigurationList;
buildConfigurations = (
C01FCF4B08A954540054247B /* Debug */,
C01FCF4C08A954540054247B /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
C01FCF4E08A954540054247B /* Build configuration list for PBXProject "Current Cost" */ = {
isa = XCConfigurationList;
buildConfigurations = (
C01FCF4F08A954540054247B /* Debug */,
C01FCF5008A954540054247B /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = 29B97313FDCFA39411CA2CEA /* Project object */;
}

View file

@ -0,0 +1,7 @@
//
// Prefix header for all source files of the 'Current Cost' target in the 'Current Cost' project
//
#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#endif

23
cocoa/CurrentCostView.h Normal file
View file

@ -0,0 +1,23 @@
//
// CurrentCostView.h
// Current Cost
//
// Created by Neil on 05/12/2008.
// Copyright 2008 Neil Cowburn. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface CurrentCostView : NSView {
NSString *watts;
NSString *temp;
NSString *time;
NSMutableDictionary *attributes;
}
@property (readwrite, copy) NSString *watts;
@property (readwrite, copy) NSString *temp;
@property (readwrite, copy) NSString *time;
@end

111
cocoa/CurrentCostView.m Normal file
View file

@ -0,0 +1,111 @@
//
// CurrentCostView.m
// Current Cost
//
// Created by Neil on 05/12/2008.
// Copyright 2008 Neil Cowburn. All rights reserved.
//
#import "CurrentCostView.h"
@interface CurrentCostView ()
- (void)drawStringCenteredIn:(NSRect)rect;
- (void)prepareAttributes;
@end
@implementation CurrentCostView
@synthesize watts, temp, time;
- (id)initWithFrame:(NSRect)rect
{
if (![super initWithFrame:rect]) {
return nil;
}
[self prepareAttributes];
watts = @"";
time = @"00:00";
temp = @"0˚C";
return self;
}
- (void)dealloc
{
[watts release];
[temp release];
[time release];
[attributes release];
[super dealloc];
}
- (void)drawRect:(NSRect)rect
{
NSRect bounds = [self bounds];
[[NSColor whiteColor] set];
[self drawStringCenteredIn:bounds];
if (([[self window] firstResponder] == self) && [NSGraphicsContext currentContextDrawingToScreen]) {
[NSGraphicsContext saveGraphicsState];
NSSetFocusRingStyle(NSFocusRingOnly);
[NSBezierPath fillRect:bounds];
[NSGraphicsContext restoreGraphicsState];
}
}
- (BOOL)isOpaque
{
return NO;
}
- (void)setWatts:(NSString *)power
{
power = [power copy];
[watts release];
watts = power;
[self setNeedsDisplay:YES];
}
- (BOOL)acceptsFirstResponder
{
return NO;
}
- (BOOL)resignFirstResponder
{
[self setNeedsDisplay:YES];
return NO;
}
- (BOOL)becomeFirstResponder
{
[self setNeedsDisplay:YES];
return NO;
}
- (void)drawStringCenteredIn:(NSRect)rect
{
NSSize wattsSize = [watts sizeWithAttributes:attributes];
NSPoint origin;
origin.x = rect.origin.x + (rect.size.width - wattsSize.width) / 2;
origin.y = rect.origin.y + (rect.size.height - wattsSize.height) / 2;
[watts drawAtPoint:origin withAttributes:attributes];
}
- (void)prepareAttributes
{
attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:[NSFont fontWithName:@"Helvetica" size:30] forKey:NSFontAttributeName];
[attributes setObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName];
}
@end

Binary file not shown.

File diff suppressed because it is too large Load diff

28
cocoa/Info.plist Normal file
View file

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.${PRODUCT_NAME:identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>

54
cocoa/NCCurrentCost.h Normal file
View file

@ -0,0 +1,54 @@
//
// CCSerialPort.h
// CCSerialPort
//
// Created by Neil on 02/12/2008.
// Copyright 2008 Neil Cowburn. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <paths.h>
#include <termios.h>
#include <sys/time.h>
#include <sysexits.h>
#include <sys/param.h>
// #define CCDEBUG 1
#define MAX_CC_BUFFER_SIZE 2048
@protocol NCCurrentCostDelegate
- (void)dataWasReceived:(NSXMLDocument *)newData;
@end
@interface NCCurrentCost : NSObject {
id delegate;
@private
NSString *bsdPath;
int baud;
NSString *serviceName;
NSString *serviceType;
int fd;
struct termios * __strong options;
struct termios * __strong originalOptions;
NSFileHandle *handle;
}
@property (readonly,retain) id delegate;
- (id)init:(NSString *)path withBaudRate:(int)baudRate;
- (void)open;
- (void)close;
- (void)read;
- (void)setDelegate:(id)newDelegate;
@end

154
cocoa/NCCurrentCost.m Normal file
View file

@ -0,0 +1,154 @@
//
// CCSerialPort.m
// CCSerialPort
//
// Created by Neil on 02/12/2008.
// Copyright 2008 Neil Cowburn. All rights reserved.
//
#import "NCCurrentCost.h"
@implementation NCCurrentCost
@synthesize delegate;
#pragma mark Constructors and destructors
- (id)init:(NSString *)path withBaudRate:(int)baudRate
{
if (self = [super init]) {
bsdPath = [path copy];
baud = baudRate;
options = (struct termios*)malloc(sizeof(*options));
originalOptions = (struct termios*)malloc(sizeof(*originalOptions));
}
return self;
}
- (void)dealloc
{
[self close];
free(originalOptions);
free(options);
[serviceType release];
[serviceName release];
[bsdPath release];
[super dealloc];
}
#pragma mark Public Methods
- (void)open
{
const char *path = [bsdPath fileSystemRepresentation];
fd = open(path, O_RDONLY | O_NOCTTY);
if (fd == -1) {
// Error opening the serial port
NSLog(@"Error opening serial port");
return;
}
if (tcgetattr(fd, originalOptions) == -1) {
// Error getting options
NSLog(@"Error getting options");
return;
}
*options = *originalOptions;
handle = [[NSFileHandle alloc] initWithFileDescriptor:fd];
// Set the baud rate to 2400 bps
cfsetspeed(options, B2400);
options->c_cc[VMIN] = 1;
options->c_cc[VTIME] = 10;
// 8 data bits
options->c_cflag |= CS8;
// No parity
options->c_cflag &= ~PARENB;
// One stop bit
options->c_cflag &= ~CSTOPB;
// No flow control
options->c_cflag &= ~(IXON | IXOFF | IXANY);
if (tcsetattr(fd,TCSANOW, options) != 0) {
// Error setting options
NSLog(@"Error setting options");
return;
}
}
- (void)close
{
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
[handle closeFile];
[handle release];
handle = nil;
close(fd);
fd = -1;
}
- (void)read
{
char buffer[MAX_CC_BUFFER_SIZE];
char *bufPtr;
ssize_t numBytes;
memset(&buffer, 0, sizeof(buffer));
bufPtr = buffer;
do
{
numBytes = read(fd, bufPtr, MAX_CC_BUFFER_SIZE);
if (numBytes > 0)
{
bufPtr += numBytes;
if (*(bufPtr - 1) == '\n')
{
NSRange range = [[NSString stringWithUTF8String:buffer] rangeOfString:@"\n"];
if ([delegate respondsToSelector:@selector(dataWasReceived:)]) {
NSData *rawData = [[NSData alloc] initWithBytes:buffer length:range.location];
NSXMLDocument *xml = [[NSXMLDocument alloc] initWithData:rawData options:0 error:nil];
if (xml != nil) {
[delegate performSelectorOnMainThread:@selector(dataWasReceived:) withObject:xml waitUntilDone:YES];
}
[xml release];
[rawData release];
}
#ifdef CCDEBUG
NSString *sentence = [data substringToIndex:range.location - 1];
NSLog(@"%@", sentence);
[sentence release];
#endif
break;
}
}
} while (numBytes > 0);
}
- (void)setDelegate:(id)newDelegate
{
[newDelegate retain];
[delegate release];
delegate = newDelegate;
}
@end

17
cocoa/NSString+Trim.h Normal file
View file

@ -0,0 +1,17 @@
//
// NSString+Trim.h
// Current Cost
//
// Created by Neil on 05/12/2008.
// Copyright 2008 Neil Cowburn. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface NSString (Trim)
+ (NSString *)stringByTrimmingLeadingZeros:(NSString *)numericString;
- (NSString *)stringByTrimmingLeadingZeros;
@end

37
cocoa/NSString+Trim.m Normal file
View file

@ -0,0 +1,37 @@
//
// NSString+Trim.m
// Current Cost
//
// Created by Neil on 05/12/2008.
// Copyright 2008 Neil Cowburn. All rights reserved.
//
#import "NSString+Trim.h"
@implementation NSString (Trim)
- (NSString *)stringByTrimmingLeadingZeros
{
return [NSString stringByTrimmingLeadingZeros:self];
}
+ (NSString *)stringByTrimmingLeadingZeros:(NSString *)numericString
{
NSMutableString *copy = [numericString mutableCopy];
NSCharacterSet *naturalNumberSet = [NSCharacterSet characterSetWithCharactersInString:@"123456789"];
NSInteger start = -1;
for (NSInteger i = 0; i < [copy length] - 1; i++) {
unichar chr = [copy characterAtIndex:i];
if ([naturalNumberSet characterIsMember:chr] == YES) {
start = i;
break;
}
}
[copy deleteCharactersInRange:NSMakeRange(0, start)];
return [NSString stringWithString:copy];
}
@end

14
cocoa/main.m Normal file
View file

@ -0,0 +1,14 @@
//
// main.m
// Current Cost
//
// Created by Neil on 05/12/2008.
// Copyright Neil Cowburn Ltd 2008. All rights reserved.
//
#import <Cocoa/Cocoa.h>
int main(int argc, char *argv[])
{
return NSApplicationMain(argc, (const char **) argv);
}