Import WiringPI and Chacond

This commit is contained in:
2016-03-01 23:18:02 +01:00
parent 0baec13e8e
commit f70a267304
184 changed files with 24179 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 2.8.11)
project(chacond)
include (libubus)
include (libubox)
include (wiringPi)
include_directories(../../src)
file(
GLOB_RECURSE
source_files
../../src/main.c
../../src/UBusModel.c
../../src/Chacon.c
)
add_executable (
chacond
${source_files}
)
target_link_libraries (chacond
LINK_PUBLIC
ubus
wiringPi
rt
pthread
)

BIN
src/chacond/libwiringPi.a Normal file

Binary file not shown.

BIN
src/chacond/radioEmission Executable file

Binary file not shown.

View File

@@ -0,0 +1,231 @@
#include <wiringPi.h>
#include <iostream>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <sched.h>
#include <sstream>
#include <unistd.h>
/*
Par Idleman (idleman@idleman.fr - http://blog.idleman.fr)
Licence : CC by sa
Toutes question sur le blog ou par mail, possibilité de m'envoyer des bières via le blog
*/
using namespace std;
int pin;
bool bit2[26]={}; // 26 bit Identifiant emetteur
bool bit2Interruptor[4]={};
int interruptor;
int sender;
string onoff;
void log(string a){
//Décommenter pour avoir les logs
cout << a << endl;
}
void scheduler_realtime() {
struct sched_param p;
p.__sched_priority = sched_get_priority_max(SCHED_RR);
if( sched_setscheduler( 0, SCHED_RR, &p ) == -1 ) {
perror("Failed to switch to realtime scheduler.");
}
}
void scheduler_standard() {
struct sched_param p;
p.__sched_priority = 0;
if( sched_setscheduler( 0, SCHED_OTHER, &p ) == -1 ) {
perror("Failed to switch to normal scheduler.");
}
}
//Envois d'une pulsation (passage de l'etat haut a l'etat bas)
//1 = 310µs haut puis 1340µs bas
//0 = 310µs haut puis 310µs bas
void sendBit(bool b) {
if (b) {
digitalWrite(pin, HIGH);
delayMicroseconds(310); //275 orinally, but tweaked.
digitalWrite(pin, LOW);
delayMicroseconds(1340); //1225 orinally, but tweaked.
}
else {
digitalWrite(pin, HIGH);
delayMicroseconds(310); //275 orinally, but tweaked.
digitalWrite(pin, LOW);
delayMicroseconds(310); //275 orinally, but tweaked.
}
}
//Calcul le nombre 2^chiffre indiqué, fonction utilisé par itob pour la conversion decimal/binaire
unsigned long power2(int power){
unsigned long integer=1;
for (int i=0; i<power; i++){
integer*=2;
}
return integer;
}
//Convertis un nombre en binaire, nécessite le nombre, et le nombre de bits souhaité en sortie (ici 26)
// Stocke le résultat dans le tableau global "bit2"
void itob(unsigned long integer, int length)
{
for (int i=0; i<length; i++){
if ((integer / power2(length-1-i))==1){
integer-=power2(length-1-i);
bit2[i]=1;
}
else bit2[i]=0;
}
}
void itobInterruptor(unsigned long integer, int length)
{
for (int i=0; i<length; i++){
if ((integer / power2(length-1-i))==1){
integer-=power2(length-1-i);
bit2Interruptor[i]=1;
}
else bit2Interruptor[i]=0;
}
}
//Envoie d'une paire de pulsation radio qui definissent 1 bit réel : 0 =01 et 1 =10
//c'est le codage de manchester qui necessite ce petit bouzin, ceci permet entre autres de dissocier les données des parasites
void sendPair(bool b) {
if(b)
{
sendBit(true);
sendBit(false);
}
else
{
sendBit(false);
sendBit(true);
}
}
//Fonction d'envois du signal
//recoit en parametre un booleen définissant l'arret ou la marche du matos (true = on, false = off)
void transmit(int blnOn)
{
int i;
// Sequence de verrou anoncant le départ du signal au recepeteur
digitalWrite(pin, HIGH);
delayMicroseconds(275); // un bit de bruit avant de commencer pour remettre les delais du recepteur a 0
digitalWrite(pin, LOW);
delayMicroseconds(9900); // premier verrou de 9900µs
digitalWrite(pin, HIGH); // high again
delayMicroseconds(275); // attente de 275µs entre les deux verrous
digitalWrite(pin, LOW); // second verrou de 2675µs
delayMicroseconds(2675);
digitalWrite(pin, HIGH); // On reviens en état haut pour bien couper les verrous des données
// Envoie du code emetteur (272946 = 1000010101000110010 en binaire)
for(i=0; i<26;i++)
{
sendPair(bit2[i]);
}
// Envoie du bit définissant si c'est une commande de groupe ou non (26em bit)
sendPair(false);
// Envoie du bit définissant si c'est allumé ou eteint 27em bit)
sendPair(blnOn);
// Envoie des 4 derniers bits, qui représentent le code interrupteur, ici 0 (encode sur 4 bit donc 0000)
// nb: sur les télécommandes officielle chacon, les interrupteurs sont logiquement nommés de 0 à x
// interrupteur 1 = 0 (donc 0000) , interrupteur 2 = 1 (1000) , interrupteur 3 = 2 (0100) etc...
for(i=0; i<4;i++)
{
if(bit2Interruptor[i]==0){
sendPair(false);
}else{
sendPair(true);
}
}
digitalWrite(pin, HIGH); // coupure données, verrou
delayMicroseconds(275); // attendre 275µs
digitalWrite(pin, LOW); // verrou 2 de 2675µs pour signaler la fermeture du signal
}
int main (int argc, char** argv)
{
if (setuid(0))
{
perror("setuid");
return 1;
}
scheduler_realtime();
log("Demarrage du programme");
pin = atoi(argv[1]);
sender = atoi(argv[2]);
interruptor = atoi(argv[3]);
onoff = argv[4];
//Si on ne trouve pas la librairie wiringPI, on arrête l'execution
if(wiringPiSetup() == -1)
{
log("Librairie Wiring PI introuvable, veuillez lier cette librairie...");
return -1;
}
pinMode(pin, OUTPUT);
log("Pin GPIO configure en sortie");
itob(sender,26); // convertion du code de l'emetteur (ici 8217034) en code binaire
itobInterruptor(interruptor,4);
if(onoff=="on"){
system("/etc/lcd/screen -p \"Radio signal ON...\"");
log("envois du signal ON");
for(int i=0;i<5;i++){
transmit(true); // envoyer ON
delay(10); // attendre 10 ms (sinon le socket nous ignore)
}
}else{
system("/etc/lcd/screen -p \"Radio signal OFF...\"");
log("envois du signal OFF");
for(int i=0;i<5;i++){
transmit(false); // envoyer OFF
delay(10); // attendre 10 ms (sinon le socket nous ignore)
}
}
log("fin du programme"); // execution terminée.
scheduler_standard();
}

View File

@@ -0,0 +1,187 @@
/*
* Code pour reception d'un signal de sonde de temperature "maison", récupère une temperature et l'envois sur un fichier php passé en paramêtre
* Fréquence : 433.92 mhz
* Protocole : Ridle (Radio Idle, protocole customisé basé sur home easy)
* Licence : CC -by -sa
* Matériel associé : récepteur RF AM 433.92 mhz
* Auteur : Valentin CARRUESCO aka idleman (idleman@idleman.fr - http://blog.idleman.fr)
*/
#include <wiringPi.h>
#include <iostream>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <sched.h>
#include <sstream>
using namespace std;
int pin = 7;
void log(string a){
//Décommenter pour avoir les logs
cout << a << endl;
}
void scheduler_realtime() {
struct sched_param p;
p.__sched_priority = sched_get_priority_max(SCHED_RR);
if( sched_setscheduler( 0, SCHED_RR, &p ) == -1 ) {
perror("Failed to switch to realtime scheduler.");
}
}
void scheduler_standard() {
struct sched_param p;
p.__sched_priority = 0;
if( sched_setscheduler( 0, SCHED_OTHER, &p ) == -1 ) {
perror("Failed to switch to normal scheduler.");
}
}
string longToString(long mylong){
string mystring;
stringstream mystream;
mystream << mylong;
return mystream.str();
}
int pulseIn(int pin, int level, int timeout)
{
struct timeval tn, t0, t1;
long micros;
gettimeofday(&t0, NULL);
micros = 0;
while (digitalRead(pin) != level)
{
gettimeofday(&tn, NULL);
if (tn.tv_sec > t0.tv_sec) micros = 1000000L; else micros = 0;
micros += (tn.tv_usec - t0.tv_usec);
if (micros > timeout) return 0;
}
gettimeofday(&t1, NULL);
while (digitalRead(pin) == level)
{
gettimeofday(&tn, NULL);
if (tn.tv_sec > t0.tv_sec) micros = 1000000L; else micros = 0;
micros = micros + (tn.tv_usec - t0.tv_usec);
if (micros > timeout) return 0;
}
if (tn.tv_sec > t1.tv_sec) micros = 1000000L; else micros = 0;
micros = micros + (tn.tv_usec - t1.tv_usec);
return micros;
}
int main (int argc, char** argv)
{
//scheduler_realtime();
string command;
string path = "php ";
path.append(argv[1]);
log("Demarrage du programme");
pin = atoi(argv[2]);
if(wiringPiSetup() == -1)
{
log("Librairie Wiring PI introuvable, veuillez lier cette librairie...");
return -1;
}
pinMode(pin, INPUT);
log("Pin GPIO configure en entree");
log("Attente d'un signal du transmetteur ...");
for(;;)
{
int i = 0;
unsigned long t = 0;
int prevBit = 0;
int bit = 0;
unsigned long temperature = 0;
unsigned long emiter = 0;
unsigned long positive = 0;
bool group=false;
bool on =false;
unsigned long recipient = 0;
command = path+" ";
t = pulseIn(pin, LOW, 1000000);
while((t < 2550 || t > 2900)){
t = pulseIn(pin, LOW,1000000);
}
while(i < 24)
{
t = pulseIn(pin, LOW, 1000000);
if(t > 500 && t < 1500)
{
bit = 0;
}
else if(t > 2000 && t < 3000)
{
bit = 1;
}
else
{
i = 0;
cout << "FAIL? = " << t << endl;
break;
}
if(i % 2 == 1)
{
if((prevBit ^ bit) == 0)
{
i = 0;
break;
}
if(i < 15)
{
temperature <<= 1;
temperature |= prevBit;
}else if(i == 15){
positive = prevBit;
}else{
emiter <<= 1;
emiter |= prevBit;
}
}
prevBit = bit;
++i;
}
if(i>0){
log("------------------------------");
log("Donnees detectees");
cout << "temperature = " << temperature << " C" << endl;
cout << "positif = " << positive << endl;
cout << "code sonde = " << emiter << endl;
command.append("UPDATE_ENGINE_STATE ");
command.append(" "+longToString(emiter));
command.append(" "+longToString(temperature));
command.append(" "+longToString(positive));
log("Execution de la commande PHP...");
log(command.c_str());
system(command.c_str());
}else{
log("Aucune donnee...");
}
delay(3000);
}
//scheduler_standard();
}

309
src/chacond/src/Chacon.c Normal file
View File

@@ -0,0 +1,309 @@
/*!
* chacon.c
*
* Copyright (c) 2015, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 20/03/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <stdio.h>
#include <sched.h>
#include <wiringPi.h>
#include "chacon.h"
/*-------------------------------- GLOBALES ---------------------------------*/
static int gGpioPin = 0;
bool gBit2[26]={}; // 26 bit Identifiant emetteur
bool gBit2Interruptor[4]={};
void sendBit (bool aBit);
unsigned long power2 (int aPower);
void itob (unsigned long anInteger, int aLength);
void itobInterruptor (unsigned long anInteger, int aLength);
void sendPair (bool aBit);
void transmit (bool aState);
/*! ----------------------------------------------------------------------------
* @fn SetupChacon
*
* @brief this method setup the Chacon environment.
*/
int SetupChacon (int aGpioPin)
{
gGpioPin = aGpioPin;
if (wiringPiSetup() == -1) {
fprintf (stderr, "Impossible to setup the WiringPI...");
return -1;
}
pinMode (gGpioPin, OUTPUT);
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn Chacon_SetInterruptorState
*
* @brief This function Change the State of the Interruptor.
*/
int Chacon_SetInterruptorState (uint32_t aSender, uint32_t anInterruptor, bool aState)
{
int i;
fprintf (stdout, "Chacon_SetInterruptorState - aSender:%d, anInterruptor:%d, aState:%d\n",
aSender, anInterruptor, aState);
/* convert sender and interruptor ID */
itob (aSender, 26); // convertion du code de l'emetteur (ici 8217034) en code binaire
itobInterruptor (anInterruptor, 4);
/* Send the State Five Time. */
for (i = 0; i < 5; i++) {
transmit (aState);
delay (10); // attendre 10 ms (sinon le socket nous ignore)
}
return 0;
}
/*-------------------------------- LOCALES ---------------------------------*/
/*! ----------------------------------------------------------------------------
* @fn scheduler_realtime
*
* @brief Switch the process to the RealTime Scheduler
*/
void scheduler_realtime (void)
{
struct sched_param theSchedParam;
theSchedParam.__sched_priority = sched_get_priority_max (SCHED_RR);
if( sched_setscheduler (0, SCHED_RR, &theSchedParam) == -1 ) {
perror ("Failed to switch to realtime scheduler.");
}
}
/*! ----------------------------------------------------------------------------
* @fn scheduler_standard
*
* @brief Switch the process to the regular Scheduler
*/
void scheduler_standard (void)
{
struct sched_param theSchedParam;
theSchedParam.__sched_priority = 0;
if( sched_setscheduler (0, SCHED_OTHER, &theSchedParam) == -1 ) {
perror ("Failed to switch to normal scheduler.");
}
}
/*! ----------------------------------------------------------------------------
* @fn sendBit
*
* @brief Envois d'une pulsation (passage de l'etat haut a l'etat bas)
* 1 = 310µs haut puis 1340µs bas
* 0 = 310µs haut puis 310µs bas
*/
void sendBit (bool aBit)
{
if (aBit) {
digitalWrite (gGpioPin, HIGH);
delayMicroseconds (310); //275 orinally, but tweaked.
digitalWrite (gGpioPin, LOW);
delayMicroseconds (1340); //1225 orinally, but tweaked.
}
else {
digitalWrite (gGpioPin, HIGH);
delayMicroseconds (310); //275 orinally, but tweaked.
digitalWrite (gGpioPin, LOW);
delayMicroseconds (310); //275 orinally, but tweaked.
}
}
/*! ----------------------------------------------------------------------------
* @fn power2
*
* @brief Calcul le nombre 2^chiffre indiqué, fonction utilisé par itob pour
* la conversion decimal/binaire
*/
unsigned long power2 (int aPower)
{
int i;
unsigned long theInteger = 1;
for (i = 0; i < aPower; i++) {
theInteger *= 2;
}
return theInteger;
}
/*! ----------------------------------------------------------------------------
* @fn itob
*
* @brief Convertis un nombre en binaire, nécessite le nombre, et le nombre de
* bits souhaité en sortie (ici 26)
* Stocke le résultat dans le tableau global "bit2"
*/
void itob (unsigned long anInteger, int aLength)
{
int i;
for (i = 0; i < aLength; i++) {
if ((anInteger / power2 (aLength - 1 - i)) == 1) {
anInteger -= power2 (aLength - 1 - i);
gBit2[i] = 1;
}
else {
gBit2[i]=0;
}
}
}
/*! ----------------------------------------------------------------------------
* @fn itobInterruptor
*
* @brief TODO
*/
void itobInterruptor (unsigned long anInteger, int aLength)
{
int i;
for (i = 0; i < aLength; i++) {
if ((anInteger / power2 (aLength - 1 - i)) == 1) {
anInteger -= power2 (aLength - 1 - i);
gBit2Interruptor[i] = 1;
}
else {
gBit2Interruptor[i] = 0;
}
}
}
/*! ----------------------------------------------------------------------------
* @fn sendPair
*
* @brief Envoie d'une paire de pulsation radio qui definissent 1 bit réel :
* 0 =01 et 1 =10
* c'est le codage de manchester qui necessite ce petit bouzin, ceci permet
* entre autres de dissocier les données des parasites
*/
void sendPair (bool aBit)
{
if (aBit) {
sendBit (true);
sendBit (false);
}
else {
sendBit (false);
sendBit (true);
}
}
/*! ----------------------------------------------------------------------------
* @fn transmit
*
* @brief Fonction d'envois du signal
* recoit en parametre un booleen définissant l'arret ou la marche du matos
* (true = on, false = off)
*/
void transmit (bool aState)
{
int i;
// Sequence de verrou anoncant le départ du signal au recepeteur
digitalWrite (gGpioPin, HIGH);
delayMicroseconds (275); // un bit de bruit avant de commencer pour remettre les delais du recepteur a 0
digitalWrite (gGpioPin, LOW);
delayMicroseconds (9900); // premier verrou de 9900µs
digitalWrite (gGpioPin, HIGH); // high again
delayMicroseconds (275); // attente de 275µs entre les deux verrous
digitalWrite (gGpioPin, LOW); // second verrou de 2675µs
delayMicroseconds (2675);
digitalWrite (gGpioPin, HIGH); // On reviens en état haut pour bien couper les verrous des données
// Envoie du code emetteur (272946 = 1000010101000110010 en binaire)
for (i = 0; i < 26; i++) {
sendPair (gBit2[i]);
}
// Envoie du bit définissant si c'est une commande de groupe ou non (26em bit)
sendPair (false);
// Envoie du bit définissant si c'est allumé ou eteint 27em bit)
sendPair (aState);
// Envoie des 4 derniers bits, qui représentent le code interrupteur, ici 0 (encode sur 4 bit donc 0000)
// nb: sur les télécommandes officielle chacon, les interrupteurs sont logiquement nommés de 0 à x
// interrupteur 1 = 0 (donc 0000) , interrupteur 2 = 1 (1000) , interrupteur 3 = 2 (0100) etc...
for (i = 0; i < 4; i++) {
if (gBit2Interruptor[i] == 0) {
sendPair (false);
} else {
sendPair (true);
}
}
digitalWrite (gGpioPin, HIGH); // coupure données, verrou
delayMicroseconds(275); // attendre 275µs
digitalWrite (gGpioPin, LOW); // verrou 2 de 2675µs pour signaler la fermeture du signal
}

167
src/chacond/src/UBusModel.c Normal file
View File

@@ -0,0 +1,167 @@
/*!
* UBusModel.c
*
* Copyright (c) 2015, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 26/02/2015
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <unistd.h>
#include <signal.h>
#include <libubox/blobmsg_json.h>
#include <libubus.h>
/*------------------------------- DECLARATION -------------------------------*/
static int gChacon_set (struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
/*--------------------------------- GLOBALS ---------------------------------*/
static struct ubus_context *gCtx;
static struct blob_buf gBuffer;
enum {
SET_SENDER,
SET_INTERRUPTOR,
SET_STATE,
__CHACON_MAX
};
static const struct blobmsg_policy set_chacon_policy[] = {
[SET_SENDER] = { .name = "sender", .type = BLOBMSG_TYPE_INT32 },
[SET_INTERRUPTOR] = { .name = "interruptor", .type = BLOBMSG_TYPE_INT32 },
[SET_STATE] = { .name = "state", .type = BLOBMSG_TYPE_BOOL },
};
static const struct ubus_method gChacon_methods[] = {
UBUS_METHOD("set", gChacon_set, set_chacon_policy),
};
static struct ubus_object_type gChacon_object_type =
UBUS_OBJECT_TYPE ("chacon", gChacon_methods);
static struct ubus_object gChacon_object = {
.name = "chacon",
.type = &gChacon_object_type,
.methods = gChacon_methods,
.n_methods = ARRAY_SIZE (gChacon_methods),
};
/*! ----------------------------------------------------------------------------
* @fn gChacon_set
*
* @brief Method to send a chacon DIO command to a specific device.
*/
static int gChacon_set (struct ubus_context *aCtx, struct ubus_object *anObj,
struct ubus_request_data *aReq, const char *aMethod,
struct blob_attr *aMsg)
{
struct blob_attr *theParams[__CHACON_MAX];
uint32_t theSender;
uint32_t theInterruptor;
bool theState;
/* Parse the Message */
blobmsg_parse (set_chacon_policy, __CHACON_MAX, theParams, blob_data (aMsg), blob_len(aMsg));
/* Sanity Checks. */
if (!theParams[SET_SENDER] || !theParams[SET_INTERRUPTOR]|| !theParams[SET_STATE]) {
printf ("Chacon Set Invalid arguments.\n");
return UBUS_STATUS_INVALID_ARGUMENT;
}
/* Get Parameters. */
theSender = blobmsg_get_u32 (theParams[SET_SENDER]);
theInterruptor = blobmsg_get_u32 (theParams[SET_INTERRUPTOR]);
theState = blobmsg_get_bool (theParams[SET_STATE]);
return Chacon_SetInterruptorState (theSender, theInterruptor, theState);
}
/*! ----------------------------------------------------------------------------
* @fn SetupUbus
*
* @brief Setup the UBus Mechanism.
*/
int SetupUbus (void)
{
uloop_init ();
signal (SIGPIPE, SIG_IGN);
gCtx = ubus_connect (NULL);
if (!gCtx) {
fprintf (stderr, "Failed to connect to ubus\n");
return -1;
}
ubus_add_uloop (gCtx);
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn UBusMain
*
* @brief Main function of the UBus Loop.
*/
int UBusMain (void)
{
int theRet;
theRet = ubus_add_object (gCtx, &gChacon_object);
if (theRet) {
fprintf (stderr, "Failed to add object: %s\n", ubus_strerror (theRet));
}
uloop_run ();
return 0;
}
/*! ----------------------------------------------------------------------------
* @fn TerminateUBus
*
* @brief This function terminate the Ubus mechanisme.
*/
int TerminateUBus (void)
{
ubus_free (gCtx);
uloop_done ();
return 0;
}

View File

@@ -0,0 +1,35 @@
/*!
* UBusModel.h
*
* Copyright (c) 2015, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 26/02/2015
*
*/
#ifndef __UBUSMODEL_H
#define __UBUSMODEL_H
int SetupUbus (void);
int TerminateUBus (void);
int UBusMain (void);
#endif /* __UBUSMODEL_H */

43
src/chacond/src/chacon.h Normal file
View File

@@ -0,0 +1,43 @@
/*!
* chacon.h
*
* Copyright (c) 2015, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 20/03/2015
*
*/
#ifndef __CHACON_H
#define __CHACON_H
/*-------------------------------- INCLUDES ---------------------------------*/
#include <stdint.h>
#include <stdbool.h>
/*-------------------------------- METHODS ---------------------------------*/
int SetupChacon (int aGpioPin);
int Chacon_SetInterruptorState (uint32_t aSender, uint32_t anInterruptor, bool aState);
void scheduler_realtime (void);
void scheduler_standard (void);
#endif /* __CHACON_H */

83
src/chacond/src/main.c Normal file
View File

@@ -0,0 +1,83 @@
/*!
* main.c
*
* Copyright (c) 2015, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 09/12/2014
*
*/
/*-------------------------------- INCLUDES ---------------------------------*/
#include <stdio.h>
#include <unistd.h>
#include "UbusModel.h"
#include "chacon.h"
/*! ----------------------------------------------------------------------------
* @fn main
*
* @brief Main function of Chacon Daemon.
*/
int main (int argc, char **argv)
{
int thePin;
int theRet = 0;
if (argc != 2) {
fprintf (stderr, "You should specify a WiringPI Pin ID\n");
return -1;
}
thePin = atoi (argv[1]);
if (setuid (0)) {
perror ("setuid");
return 1;
}
scheduler_realtime();
/* Setup UBus. */
theRet = SetupUbus ();
if (theRet != 0) {
fprintf (stderr, "Impossible to Setup the UBUS Environment.\n");
return theRet;
}
/* Setup Chacon. */
theRet = SetupChacon (thePin);
if (theRet != 0) {
fprintf (stderr, "Impossible to Setup the Chacon Environment.\n");
return theRet;
}
/* Main loop */
UBusMain ();
scheduler_standard();
return TerminateUBus();
}

187
src/chacond/wiringPi.h Normal file
View File

@@ -0,0 +1,187 @@
/*
* wiringPi:
* Arduino compatable (ish) Wiring library for the Raspberry Pi
* Copyright (c) 2012 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#ifndef __WIRING_PI_H__
#define __WIRING_PI_H__
// Handy defines
// Deprecated
#define NUM_PINS 17
#define WPI_MODE_PINS 0
#define WPI_MODE_GPIO 1
#define WPI_MODE_GPIO_SYS 2
#define WPI_MODE_PHYS 3
#define WPI_MODE_PIFACE 4
#define WPI_MODE_UNINITIALISED -1
// Pin modes
#define INPUT 0
#define OUTPUT 1
#define PWM_OUTPUT 2
#define GPIO_CLOCK 3
#define LOW 0
#define HIGH 1
// Pull up/down/none
#define PUD_OFF 0
#define PUD_DOWN 1
#define PUD_UP 2
// PWM
#define PWM_MODE_MS 0
#define PWM_MODE_BAL 1
// Interrupt levels
#define INT_EDGE_SETUP 0
#define INT_EDGE_FALLING 1
#define INT_EDGE_RISING 2
#define INT_EDGE_BOTH 3
// Threads
#define PI_THREAD(X) void *X (void *dummy)
// Failure modes
#define WPI_FATAL (1==1)
#define WPI_ALMOST (1==2)
// wiringPiNodeStruct:
// This describes additional device nodes in the extended wiringPi
// 2.0 scheme of things.
// It's a simple linked list for now, but will hopefully migrate to
// a binary tree for efficiency reasons - but then again, the chances
// of more than 1 or 2 devices being added are fairly slim, so who
// knows....
struct wiringPiNodeStruct
{
int pinBase ;
int pinMax ;
int fd ; // Node specific
unsigned int data0 ; // ditto
unsigned int data1 ; // ditto
unsigned int data2 ; // ditto
unsigned int data3 ; // ditto
void (*pinMode) (struct wiringPiNodeStruct *node, int pin, int mode) ;
void (*pullUpDnControl) (struct wiringPiNodeStruct *node, int pin, int mode) ;
int (*digitalRead) (struct wiringPiNodeStruct *node, int pin) ;
void (*digitalWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
void (*pwmWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
int (*analogRead) (struct wiringPiNodeStruct *node, int pin) ;
void (*analogWrite) (struct wiringPiNodeStruct *node, int pin, int value) ;
struct wiringPiNodeStruct *next ;
} ;
extern struct wiringPiNodeStruct *wiringPiNodes ;
// Function prototypes
// c++ wrappers thanks to a comment by Nick Lott
// (and others on the Raspberry Pi forums)
#ifdef __cplusplus
extern "C" {
#endif
// Internal
extern int wiringPiFailure (int fatal, const char *message, ...) ;
// Core wiringPi functions
extern struct wiringPiNodeStruct *wiringPiFindNode (int pin) ;
extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ;
extern int wiringPiSetup (void) ;
extern int wiringPiSetupSys (void) ;
extern int wiringPiSetupGpio (void) ;
extern int wiringPiSetupPhys (void) ;
extern void pinModeAlt (int pin, int mode) ;
extern void pinMode (int pin, int mode) ;
extern void pullUpDnControl (int pin, int pud) ;
extern int digitalRead (int pin) ;
extern void digitalWrite (int pin, int value) ;
extern void pwmWrite (int pin, int value) ;
extern int analogRead (int pin) ;
extern void analogWrite (int pin, int value) ;
// PiFace specifics
// (Deprecated)
extern int wiringPiSetupPiFace (void) ;
extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only
// On-Board Raspberry Pi hardware specific stuff
extern int piBoardRev (void) ;
extern int wpiPinToGpio (int wpiPin) ;
extern int physPinToGpio (int physPin) ;
extern void setPadDrive (int group, int value) ;
extern int getAlt (int pin) ;
extern void digitalWriteByte (int value) ;
extern void pwmSetMode (int mode) ;
extern void pwmSetRange (unsigned int range) ;
extern void pwmSetClock (int divisor) ;
extern void gpioClockSet (int pin, int freq) ;
// Interrupts
// (Also Pi hardware specific)
extern int waitForInterrupt (int pin, int mS) ;
extern int wiringPiISR (int pin, int mode, void (*function)(void)) ;
// Threads
extern int piThreadCreate (void *(*fn)(void *)) ;
extern void piLock (int key) ;
extern void piUnlock (int key) ;
// Schedulling priority
extern int piHiPri (const int pri) ;
// Extras from arduino land
extern void delay (unsigned int howLong) ;
extern void delayMicroseconds (unsigned int howLong) ;
extern unsigned int millis (void) ;
extern unsigned int micros (void) ;
#ifdef __cplusplus
}
#endif
#endif