-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoKeysLibPoNETAsync.c
More file actions
138 lines (124 loc) · 4.9 KB
/
Copy pathPoKeysLibPoNETAsync.c
File metadata and controls
138 lines (124 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "PoKeysLibHal.h"
#include "PoKeysLibAsync.h"
#include <string.h>
/*
* Asynchronous PoNET helper functions.
* These mirror the blocking PoNET routines found in PoKeysLibPoNET.c
* but use the non-blocking CreateRequestAsync/SendRequestAsync framework.
*
* Designed for realtime threads: minimal CPU usage and no blocking IO.
*/
static int PK_PoNET_StatusParse(sPoKeysDevice *dev, const uint8_t *resp)
{
if (!dev || !resp) return PK_ERR_GENERIC;
dev->PoNETmodule.PoNETstatus = resp[8];
return PK_OK;
}
static int PK_PoNET_ModuleSettingsParse(sPoKeysDevice *dev, const uint8_t *resp)
{
if (!dev || !resp) return PK_ERR_GENERIC;
dev->PoNETmodule.i2cAddress = resp[8];
dev->PoNETmodule.moduleType = resp[9];
dev->PoNETmodule.moduleSize = resp[10];
dev->PoNETmodule.moduleOptions= resp[11];
return PK_OK;
}
static int PK_PoNET_ModuleStatusParse(sPoKeysDevice *dev, const uint8_t *resp)
{
if (!dev || !resp) return PK_ERR_GENERIC;
if (resp[3] != 1 || resp[8] != 0) return PK_ERR_GENERIC;
memcpy(dev->PoNETmodule.statusIn, resp + 9, 16);
return PK_OK;
}
static int PK_PoNET_ModuleLightParse(sPoKeysDevice *dev, const uint8_t *resp)
{
if (!dev || !resp) return PK_ERR_GENERIC;
if (resp[8] != 0) return PK_ERR_GENERIC;
dev->PoNETmodule.lightValue = resp[9];
return PK_OK;
}
int PK_PoNETGetPoNETStatusAsync(sPoKeysDevice* device)
{
if (!device) return PK_ERR_NOT_CONNECTED;
uint8_t params[1] = { PONET_OP_GET_STATUS };
int req = CreateRequestAsync(device, PK_CMD_POI2C_COMMUNICATION,
params, 1, NULL, 0,
PK_PoNET_StatusParse);
if (req < 0) return req;
return SendRequestAsync(device, req);
}
int PK_PoNETGetModuleSettingsAsync(sPoKeysDevice* device)
{
if (!device) return PK_ERR_NOT_CONNECTED;
uint8_t params[2] = { PONET_OP_GET_MODULE_SETTINGS,
device->PoNETmodule.moduleID };
int req = CreateRequestAsync(device, PK_CMD_POI2C_COMMUNICATION,
params, 2, NULL, 0,
PK_PoNET_ModuleSettingsParse);
if (req < 0) return req;
return SendRequestAsync(device, req);
}
int PK_PoNETGetModuleStatusRequestAsync(sPoKeysDevice* device)
{
if (!device) return PK_ERR_NOT_CONNECTED;
uint8_t params[3] = { PONET_OP_GET_MODULE_DATA, 0x10,
device->PoNETmodule.moduleID };
int req = CreateRequestAsync(device, PK_CMD_POI2C_COMMUNICATION,
params, 3, NULL, 0, NULL);
if (req < 0) return req;
return SendRequestAsync(device, req);
}
int PK_PoNETGetModuleStatusAsync(sPoKeysDevice* device)
{
if (!device) return PK_ERR_NOT_CONNECTED;
uint8_t params[2] = { PONET_OP_GET_MODULE_DATA, 0x30 };
int req = CreateRequestAsync(device, PK_CMD_POI2C_COMMUNICATION,
params, 2, NULL, 0,
PK_PoNET_ModuleStatusParse);
if (req < 0) return req;
return SendRequestAsync(device, req);
}
int PK_PoNETSetModuleStatusAsync(sPoKeysDevice* device)
{
if (!device) return PK_ERR_NOT_CONNECTED;
uint8_t params[2] = { PONET_OP_SET_MODULE_DATA,
device->PoNETmodule.moduleID };
int req = CreateRequestAsyncWithPayload(device, PK_CMD_POI2C_COMMUNICATION,
params, 2,
device->PoNETmodule.statusOut, 16,
NULL);
if (req < 0) return req;
return SendRequestAsync(device, req);
}
int PK_PoNETSetModulePWMAsync(sPoKeysDevice* device)
{
if (!device) return PK_ERR_NOT_CONNECTED;
uint8_t params[3] = { PONET_OP_SET_PWM_VALUE,
device->PoNETmodule.moduleID,
device->PoNETmodule.PWMduty };
int req = CreateRequestAsync(device, PK_CMD_POI2C_COMMUNICATION,
params, 3, NULL, 0, NULL);
if (req < 0) return req;
return SendRequestAsync(device, req);
}
int PK_PoNETGetModuleLightRequestAsync(sPoKeysDevice* device)
{
if (!device) return PK_ERR_NOT_CONNECTED;
uint8_t params[3] = { PONET_OP_GET_LIGHT_SENSOR, 0x10,
device->PoNETmodule.moduleID };
int req = CreateRequestAsync(device, PK_CMD_POI2C_COMMUNICATION,
params, 3, NULL, 0, NULL);
if (req < 0) return req;
return SendRequestAsync(device, req);
}
int PK_PoNETGetModuleLightAsync(sPoKeysDevice* device)
{
if (!device) return PK_ERR_NOT_CONNECTED;
uint8_t params[3] = { PONET_OP_GET_LIGHT_SENSOR, 0x30,
device->PoNETmodule.moduleID };
int req = CreateRequestAsync(device, PK_CMD_POI2C_COMMUNICATION,
params, 3, NULL, 0,
PK_PoNET_ModuleLightParse);
if (req < 0) return req;
return SendRequestAsync(device, req);
}