-
-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
756ff4b
commit 9e06b04
Showing
14 changed files
with
534 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <SPI.h> | ||
#include <HostedClient.h> | ||
#include <Data/Stream/MemoryDataStream.h> | ||
|
||
extern HostedClient* hostedClient; | ||
|
||
// define the static singleton | ||
SPIClass SPI; | ||
|
||
void SPIClass::begin() | ||
{ | ||
NEW_HD_COMMAND(message, SpiBeginTransaction, { | ||
command->has_settings = 0; | ||
}); | ||
|
||
hostedClient->send(&message); | ||
} | ||
|
||
void SPIClass::beginTransaction(SPISettings mySettings) | ||
{ | ||
NEW_HD_COMMAND(message, SpiBeginTransaction, { | ||
command->has_settings = 1; | ||
command->settings.speed = mySettings.speed; | ||
command->settings.byteOrder = (SpiSettings_ByteOrder)mySettings.byteOrder; | ||
command->settings.dataMode = (SpiSettings_DataMode)mySettings.dataMode; | ||
}); | ||
|
||
hostedClient->send(&message); | ||
} | ||
|
||
/** @brief transfer(uint8_t *buffer, size_t numberBytes) | ||
* @param buffer in/out | ||
* @param numberBytes length of buffer | ||
* | ||
* SPI transfer is based on a simultaneous send and receive: | ||
* The buffered transfers does split up the conversation internaly into 64 byte blocks. | ||
* The received data is stored in the buffer passed by reference. | ||
* (the data past in is replaced with the data received). | ||
* | ||
* SPI.transfer(buffer, size) : memory buffer of length size | ||
*/ | ||
void SPIClass::transfer(uint8_t* buffer, size_t numberBytes) | ||
{ | ||
PbData data; | ||
data.value = buffer; | ||
data.length = numberBytes; | ||
|
||
NEW_HD_COMMAND(message, SpiTransfer, { | ||
command->data.arg = (void*)&data; | ||
command->data.funcs.encode = &pbEncodeData; | ||
}); | ||
|
||
hostedClient->send(&message); | ||
HostedCommand response = hostedClient->wait(); | ||
|
||
MemoryDataStream* resultData = (MemoryDataStream*)response.payload.responseSpiTransfer.data.arg; | ||
if(resultData == nullptr) { | ||
memset(buffer, 0, numberBytes); | ||
return; | ||
} | ||
|
||
resultData->readBytes((char *)buffer, numberBytes); | ||
delete resultData; | ||
} | ||
|
||
|
||
uint32_t SPIClass::transfer32(uint32_t val, uint8_t bits) | ||
{ | ||
// TODO: | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "DigitalHosted.h" | ||
|
||
namespace Hosted { | ||
namespace Digital { | ||
|
||
void registerCommands(HostedServer& server) | ||
{ | ||
// Register Command Handlers | ||
server.registerCommand(HostedCommand_requestPinMode_tag, [](HostedCommand *request, HostedCommand *response)-> int { | ||
pinMode((uint16_t)request->payload.requestPinMode.pin, (uint8_t)request->payload.requestPinMode.mode); | ||
return 0; | ||
}); | ||
|
||
server.registerCommand(HostedCommand_requestDigitalWrite_tag, [](HostedCommand *request, HostedCommand *response)-> int { | ||
digitalWrite((uint16_t)request->payload.requestDigitalWrite.pin, (uint8_t)request->payload.requestDigitalWrite.value); | ||
return 0; | ||
}); | ||
|
||
server.registerCommand(HostedCommand_requestDigitalRead_tag, [](HostedCommand *request, HostedCommand *response)-> int { | ||
uint8_t result = digitalRead((uint16_t)request->payload.requestDigitalRead.pin); | ||
response->which_payload = HostedCommand_responseDigitalRead_tag; | ||
response->payload.responseDigitalRead.value = result; | ||
|
||
return 0; | ||
}); | ||
} | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#include <SmingCore.h> | ||
#include <HostedServer.h> | ||
#include <Digital.h> | ||
|
||
namespace Hosted { | ||
namespace Digital { | ||
|
||
void registerCommands(HostedServer& server); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "SpiHosted.h" | ||
#include <Data/Stream/MemoryDataStream.h> | ||
|
||
namespace Hosted { | ||
namespace Spi { | ||
|
||
void registerCommands(HostedServer& server) | ||
{ | ||
// Register Command Handlers | ||
server.registerCommand(HostedCommand_requestSpiBeginTransaction_tag, [](HostedCommand *request, HostedCommand *response)-> int { | ||
if(request->payload.requestSpiBeginTransaction.has_settings) { | ||
SPI.begin(); | ||
} | ||
else { | ||
SPISettings settings(request->payload.requestSpiBeginTransaction.settings.speed, | ||
request->payload.requestSpiBeginTransaction.settings.byteOrder, | ||
request->payload.requestSpiBeginTransaction.settings.dataMode); | ||
SPI.beginTransaction(settings); | ||
} | ||
|
||
return 0; | ||
}); | ||
|
||
server.registerCommand(HostedCommand_requestSpiTransfer_tag, [](HostedCommand *request, HostedCommand *response)-> int { | ||
PbData* responseData = new PbData; | ||
responseData->length = 0; | ||
|
||
MemoryDataStream* data = (MemoryDataStream*)request->payload.requestSpiTransfer.data.arg; | ||
if(data != nullptr) { | ||
size_t available = data->available(); | ||
uint8_t* buffer = new uint8_t[available]; | ||
size_t length = data->readBytes((char *)&buffer, available); | ||
SPI.transfer(buffer, length); | ||
responseData->value = buffer; | ||
responseData->length = length; | ||
|
||
delete data; | ||
} | ||
|
||
response->payload.responseSpiTransfer.data.funcs.encode = &pbEncodeData; | ||
response->payload.responseSpiTransfer.data.arg = (void* )responseData; | ||
|
||
return 0; | ||
}); | ||
} | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#include <SmingCore.h> | ||
#include <HostedServer.h> | ||
#include <SPI.h> | ||
|
||
namespace Hosted { | ||
namespace Spi { | ||
|
||
void registerCommands(HostedServer& server); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.