Paul Paterson / ppCANOpen

Dependents:   ppCANOpen_Example DISCO-F746NG_rtos_test

Committer:
ptpaterson
Date:
Sat Jan 09 17:15:29 2016 +0000
Revision:
4:2034b04c86d2
Parent:
3:12b3c25bdeba
Child:
5:22a337cdc0e3
echo ability

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ptpaterson 2:c724ff3a4e4d 1 /**
ptpaterson 2:c724ff3a4e4d 2 ******************************************************************************
ptpaterson 2:c724ff3a4e4d 3 * @file
ptpaterson 2:c724ff3a4e4d 4 * @author Paul Paterson
ptpaterson 2:c724ff3a4e4d 5 * @version
ptpaterson 2:c724ff3a4e4d 6 * @date 2015-12-22
ptpaterson 2:c724ff3a4e4d 7 * @brief CANOpen implementation library
ptpaterson 2:c724ff3a4e4d 8 ******************************************************************************
ptpaterson 2:c724ff3a4e4d 9 * @attention
ptpaterson 2:c724ff3a4e4d 10 *
ptpaterson 2:c724ff3a4e4d 11 * <h2><center>&copy; COPYRIGHT(c) 2015 Paul Paterson
ptpaterson 2:c724ff3a4e4d 12 *
ptpaterson 2:c724ff3a4e4d 13 * All rights reserved.
ptpaterson 2:c724ff3a4e4d 14
ptpaterson 2:c724ff3a4e4d 15 This program is free software: you can redistribute it and/or modify
ptpaterson 2:c724ff3a4e4d 16 it under the terms of the GNU General Public License as published by
ptpaterson 2:c724ff3a4e4d 17 the Free Software Foundation, either version 3 of the License, or
ptpaterson 2:c724ff3a4e4d 18 (at your option) any later version.
ptpaterson 2:c724ff3a4e4d 19
ptpaterson 2:c724ff3a4e4d 20 This program is distributed in the hope that it will be useful,
ptpaterson 2:c724ff3a4e4d 21 but WITHOUT ANY WARRANTY; without even the implied warranty of
ptpaterson 2:c724ff3a4e4d 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ptpaterson 2:c724ff3a4e4d 23 GNU General Public License for more details.
ptpaterson 2:c724ff3a4e4d 24
ptpaterson 2:c724ff3a4e4d 25 You should have received a copy of the GNU General Public License
ptpaterson 2:c724ff3a4e4d 26 along with this program. If not, see <http://www.gnu.org/licenses/>.
ptpaterson 2:c724ff3a4e4d 27 */
ptpaterson 2:c724ff3a4e4d 28
ptpaterson 2:c724ff3a4e4d 29 #include "ServiceProvider.h"
ptpaterson 2:c724ff3a4e4d 30 #include "Node.h"
ptpaterson 2:c724ff3a4e4d 31
ptpaterson 4:2034b04c86d2 32 #include "CanOpenHandle.h"
ptpaterson 2:c724ff3a4e4d 33 #include "canopen_api.h"
ptpaterson 2:c724ff3a4e4d 34
ptpaterson 2:c724ff3a4e4d 35 #include <string.h>
ptpaterson 3:12b3c25bdeba 36 #include <stdio.h>
ptpaterson 2:c724ff3a4e4d 37
ptpaterson 2:c724ff3a4e4d 38 namespace ppCANOpen
ptpaterson 2:c724ff3a4e4d 39 {
ptpaterson 2:c724ff3a4e4d 40
ptpaterson 4:2034b04c86d2 41 /*=============================================================================
ptpaterson 4:2034b04c86d2 42 * Construction
ptpaterson 4:2034b04c86d2 43 *=============================================================================
ptpaterson 4:2034b04c86d2 44 */
ptpaterson 4:2034b04c86d2 45
ptpaterson 2:c724ff3a4e4d 46 ServiceProvider::ServiceProvider (void) : nodeCount(0)
ptpaterson 2:c724ff3a4e4d 47 {
ptpaterson 4:2034b04c86d2 48
ptpaterson 4:2034b04c86d2 49 //messageCount = 0;
ptpaterson 3:12b3c25bdeba 50
ptpaterson 3:12b3c25bdeba 51 //memset (nodes, 0, sizeof(Node) * SERVICE_MAX_NODES ); // TODO: fix incorrect <--
ptpaterson 3:12b3c25bdeba 52 for (int i = 0; i < SERVICE_MAX_NODES; i++) {
ptpaterson 3:12b3c25bdeba 53 nodes[i] = 0;
ptpaterson 3:12b3c25bdeba 54 }
ptpaterson 3:12b3c25bdeba 55
ptpaterson 3:12b3c25bdeba 56 hCanOpen = new CanOpenHandle;
ptpaterson 3:12b3c25bdeba 57 CanOpenApiInit(hCanOpen);
ptpaterson 3:12b3c25bdeba 58 }
ptpaterson 3:12b3c25bdeba 59
ptpaterson 3:12b3c25bdeba 60 ServiceProvider::~ServiceProvider (void)
ptpaterson 3:12b3c25bdeba 61 {
ptpaterson 3:12b3c25bdeba 62 delete hCanOpen;
ptpaterson 2:c724ff3a4e4d 63 }
ptpaterson 2:c724ff3a4e4d 64
ptpaterson 2:c724ff3a4e4d 65 /*=============================================================================
ptpaterson 2:c724ff3a4e4d 66 * main loop
ptpaterson 2:c724ff3a4e4d 67 *=============================================================================
ptpaterson 2:c724ff3a4e4d 68 */
ptpaterson 2:c724ff3a4e4d 69
ptpaterson 2:c724ff3a4e4d 70 void ServiceProvider::Run (void)
ptpaterson 2:c724ff3a4e4d 71 {
ptpaterson 2:c724ff3a4e4d 72 while (1) {
ptpaterson 3:12b3c25bdeba 73
ptpaterson 2:c724ff3a4e4d 74 /* Check for new messages */
ptpaterson 2:c724ff3a4e4d 75 CanOpenMessage msg;
ptpaterson 3:12b3c25bdeba 76 if (hCanOpen->can) {
ptpaterson 3:12b3c25bdeba 77 if (CanOpenApiRead (hCanOpen, &msg)) {
ptpaterson 3:12b3c25bdeba 78
ptpaterson 4:2034b04c86d2 79 //printf("*** SP.Run: About to dispatch\r\n");
ptpaterson 3:12b3c25bdeba 80 /* if new message exists, give it to the nodes*/
ptpaterson 3:12b3c25bdeba 81 /* update all of the nodes */
ptpaterson 3:12b3c25bdeba 82 for (int i=0; i < nodeCount; i++) {
ptpaterson 3:12b3c25bdeba 83 nodes[i]->DispatchMessage(&msg);
ptpaterson 3:12b3c25bdeba 84 }
ptpaterson 4:2034b04c86d2 85 //printf("*** SP.Run: dispatched\r\n");
ptpaterson 3:12b3c25bdeba 86 }
ptpaterson 3:12b3c25bdeba 87 }
ptpaterson 3:12b3c25bdeba 88
ptpaterson 3:12b3c25bdeba 89 /* update all of the nodes */
ptpaterson 2:c724ff3a4e4d 90 for (int i=0; i < nodeCount; i++) {
ptpaterson 3:12b3c25bdeba 91 nodes[i]->Update();
ptpaterson 2:c724ff3a4e4d 92 }
ptpaterson 4:2034b04c86d2 93
ptpaterson 4:2034b04c86d2 94 /* send out the responses */
ptpaterson 4:2034b04c86d2 95 while (! outbox.empty()) {
ptpaterson 4:2034b04c86d2 96
ptpaterson 4:2034b04c86d2 97 printf("msg id: %d\r\n", outbox.front().id);
ptpaterson 4:2034b04c86d2 98 if (CanOpenApiWrite(hCanOpen, &outbox.front())) {
ptpaterson 4:2034b04c86d2 99 outbox.pop();
ptpaterson 4:2034b04c86d2 100 } else {
ptpaterson 4:2034b04c86d2 101 printf("Could not send last message. Trying again\r\n");
ptpaterson 4:2034b04c86d2 102 }
ptpaterson 4:2034b04c86d2 103 }
ptpaterson 4:2034b04c86d2 104
ptpaterson 2:c724ff3a4e4d 105 }
ptpaterson 2:c724ff3a4e4d 106 }
ptpaterson 2:c724ff3a4e4d 107
ptpaterson 2:c724ff3a4e4d 108 /*=============================================================================
ptpaterson 4:2034b04c86d2 109 * Register a node to get messages and get update calls
ptpaterson 2:c724ff3a4e4d 110 *=============================================================================
ptpaterson 2:c724ff3a4e4d 111 */
ptpaterson 2:c724ff3a4e4d 112
ptpaterson 2:c724ff3a4e4d 113 int ServiceProvider::AddNode (Node * node)
ptpaterson 2:c724ff3a4e4d 114 {
ptpaterson 2:c724ff3a4e4d 115 int result = 1;
ptpaterson 2:c724ff3a4e4d 116
ptpaterson 2:c724ff3a4e4d 117 if (nodeCount < SERVICE_MAX_NODES) {
ptpaterson 2:c724ff3a4e4d 118 nodes[nodeCount++] = node;
ptpaterson 2:c724ff3a4e4d 119 } else {
ptpaterson 2:c724ff3a4e4d 120 result = 0;
ptpaterson 2:c724ff3a4e4d 121 }
ptpaterson 2:c724ff3a4e4d 122
ptpaterson 2:c724ff3a4e4d 123 return result;
ptpaterson 2:c724ff3a4e4d 124 }
ptpaterson 2:c724ff3a4e4d 125
ptpaterson 2:c724ff3a4e4d 126 /* ========================================================================
ptpaterson 4:2034b04c86d2 127 * Adds a message to the message queue outbox
ptpaterson 2:c724ff3a4e4d 128 * ========================================================================
ptpaterson 2:c724ff3a4e4d 129 */
ptpaterson 2:c724ff3a4e4d 130
ptpaterson 4:2034b04c86d2 131 void ServiceProvider::PostMessage (CanOpenMessage * msg)
ptpaterson 2:c724ff3a4e4d 132 {
ptpaterson 4:2034b04c86d2 133 outbox.push(*msg);
ptpaterson 2:c724ff3a4e4d 134 }
ptpaterson 2:c724ff3a4e4d 135
ptpaterson 2:c724ff3a4e4d 136 } /* namspace ppCANOpen */
ptpaterson 2:c724ff3a4e4d 137
ptpaterson 2:c724ff3a4e4d 138
ptpaterson 2:c724ff3a4e4d 139
ptpaterson 2:c724ff3a4e4d 140
ptpaterson 2:c724ff3a4e4d 141
ptpaterson 2:c724ff3a4e4d 142
ptpaterson 2:c724ff3a4e4d 143