Advertisement
Guest User

Untitled

a guest
Apr 27th, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. /*
  2. Copyright (C) 2019 Cygnusx
  3.  
  4. This file is part of pilight.
  5.  
  6. pilight is free software: you can redistribute it and/or modify it under the
  7. terms of the GNU General Public License as published by the Free Software
  8. Foundation, either version 3 of the License, or (at your option) any later
  9. version.
  10.  
  11. pilight is distributed in the hope that it will be useful, but WITHOUT ANY
  12. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  13. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with pilight. If not, see <http://www.gnu.org/licenses/>
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <math.h>
  23.  
  24. #include "../../core/pilight.h"
  25. #include "../../core/common.h"
  26. #include "../../core/dso.h"
  27. #include "../../core/log.h"
  28. #include "../protocol.h"
  29. #include "../../core/binary.h"
  30. #include "../../core/gc.h"
  31. #include "eken_doorbell.h"
  32.  
  33. #define PULSE_MULTIPLIER 8
  34. #define MIN_PULSE_LENGTH 294
  35. #define MAX_PULSE_LENGTH 414
  36. #define AVG_PULSE_LENGTH 334
  37. #define RAW_LENGTH 26
  38.  
  39. static int validate(void) {
  40. if(eken_doorbell->rawlen == RAW_LENGTH) {
  41. if(eken_doorbell->raw[0] >= MIN_PULSE_LENGTH && eken_doorbell->raw[0] <= MAX_PULSE_LENGTH) {
  42. return 0;
  43. }
  44. }
  45.  
  46. return -1;
  47. }
  48.  
  49. static void createMessage(int unitcode, int state) {
  50. eken_doorbell->message = json_mkobject();
  51. //json_append_member(eken_doorbell->message, "systemcode", json_mknumber(systemcode, 0));
  52. //json_append_member(eken_doorbell->message, "unitcode", json_mknumber(unitcode, 0));
  53. json_append_member(eken_doorbell->message, "id", json_mknumber(unitcode, 0));
  54. if(state == 0) {
  55. json_append_member(eken_doorbell->message, "state", json_mkstring("on"));
  56. } else {
  57. json_append_member(eken_doorbell->message, "state", json_mkstring("off"));
  58. }
  59. }
  60.  
  61. static void parseCode(void) {
  62. int i = 0, x = 0, binary[RAW_LENGTH/2];
  63. int unitcode = 0;
  64.  
  65. if(eken_doorbell->rawlen>RAW_LENGTH) {
  66. logprintf(LOG_ERR, "eken_doorbell: parsecode - invalid parameter passed %d", eken_doorbell->rawlen);
  67. return;
  68. }
  69.  
  70. for(x=0;x<eken_doorbell->rawlen-1;x+=2) {
  71. if(eken_doorbell->raw[x] > (int)((double)AVG_PULSE_LENGTH*((double)PULSE_MULTIPLIER/2))) {
  72. binary[i++] = 1;
  73. } else {
  74. binary[i++] = 0;
  75. }
  76. }
  77. //state = binary[20];
  78. unitcode = binToDecRev(binary, 0, 19);
  79.  
  80. //createMessage(systemcode, unitcode, state);
  81. createMessage(unitcode, 0);
  82. }
  83.  
  84. static void createLow(int s, int e) {
  85. int i = 0;
  86.  
  87. for(i=s;i<=e;i+=2) {
  88. eken_doorbell->raw[i]=(AVG_PULSE_LENGTH);
  89. eken_doorbell->raw[i+1]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH);
  90. }
  91. }
  92.  
  93. static void createHigh(int s, int e) {
  94. int i = 0;
  95.  
  96. for(i=s;i<=e;i+=2) {
  97. eken_doorbell->raw[i]=(PULSE_MULTIPLIER*AVG_PULSE_LENGTH);
  98. eken_doorbell->raw[i+1]=(AVG_PULSE_LENGTH);
  99. }
  100. }
  101.  
  102. static void clearCode(void) {
  103. createLow(0, eken_doorbell->rawlen-2);
  104. }
  105.  
  106. static void createSystemCode(int systemcode) {
  107. int binary[255];
  108. int length=0;
  109. int i = 0, x = 38;
  110.  
  111. length = decToBin(systemcode, binary);
  112. for(i=length;i>=0;i--) {
  113. if(binary[i] == 1) {
  114. createHigh(x, x+1);
  115. }
  116.  
  117. x -= 2;
  118. }
  119. }
  120.  
  121. static void createUnitCode(int unitcode) {
  122. switch(unitcode) {
  123. case 7:
  124. createHigh(42, 47); // Button 1
  125. break;
  126. case 3:
  127. createLow(42, 43); // Button 2
  128. createHigh(44, 47);
  129. break;
  130. case 5:
  131. createHigh(42, 43); // Button 3
  132. createLow(44, 45);
  133. createHigh(46, 47);
  134. break;
  135. case 6:
  136. createHigh(42, 45); // Button 4
  137. createLow(46, 47);
  138. break;
  139. case 0:
  140. createLow(42, 47); // Button ALL OFF
  141. break;
  142. default:
  143. break;
  144. }
  145. }
  146.  
  147. static void createState(int state) {
  148. if(state == 1) {
  149. createLow(40, 41);
  150. } else {
  151. createHigh(40, 41);
  152. }
  153. }
  154.  
  155. static void createFooter(void) {
  156. eken_doorbell->raw[48]=(AVG_PULSE_LENGTH);
  157. eken_doorbell->raw[49]=(PULSE_DIV*AVG_PULSE_LENGTH);
  158. }
  159.  
  160. static int createCode(struct JsonNode *code) {
  161. int systemcode = -1;
  162. int unitcode = -1;
  163. int state = -1;
  164. double itmp = 0;
  165.  
  166. if(json_find_number(code, "systemcode", &itmp) == 0)
  167. systemcode = (int)round(itmp);
  168. if(json_find_number(code, "unitcode", &itmp) == 0)
  169. unitcode = (int)round(itmp);
  170. if(json_find_number(code, "off", &itmp) == 0)
  171. state=1;
  172. else if(json_find_number(code, "on", &itmp) == 0)
  173. state=0;
  174.  
  175. if(systemcode == -1 || unitcode == -1 || state == -1) {
  176. logprintf(LOG_ERR, "eken_doorbell: insufficient number of arguments");
  177. return EXIT_FAILURE;
  178. } else if(systemcode > 2097151 || systemcode < 0) {
  179. logprintf(LOG_ERR, "eken_doorbell: invalid systemcode range");
  180. return EXIT_FAILURE;
  181. } else if(unitcode > 7 || unitcode < 0) {
  182. logprintf(LOG_ERR, "eken_doorbell: invalid unitcode range");
  183. return EXIT_FAILURE;
  184. } else {
  185. createMessage(unitcode, state);
  186. clearCode();
  187. createSystemCode(systemcode);
  188. createUnitCode(unitcode);
  189. createState(state);
  190. createFooter();
  191. eken_doorbell->rawlen = RAW_LENGTH;
  192. }
  193. return EXIT_SUCCESS;
  194. }
  195.  
  196. static void printHelp(void) {
  197. printf("\t -s --systemcode=systemcode\tcontrol a device with this systemcode\n");
  198. printf("\t -u --unitcode=unitcode\t\tcontrol a device with this unitcode\n");
  199. printf("\t -t --on\t\t\tsend an on signal\n");
  200. printf("\t -f --off\t\t\tsend an off signal\n");
  201. }
  202.  
  203. #if !defined(MODULE) && !defined(_WIN32)
  204. __attribute__((weak))
  205. #endif
  206.  
  207. void eken_doorbellInit(void) {
  208. protocol_register(&eken_doorbell);
  209. protocol_set_id(eken_doorbell, "eken_doorbell");
  210. protocol_device_add(eken_doorbell, "eken_doorbell", "Eken doorbell Doorbell");
  211. eken_doorbell->devtype = SWITCH;
  212. eken_doorbell->hwtype = RF433;
  213. eken_doorbell->minrawlen = RAW_LENGTH;
  214. eken_doorbell->maxrawlen = RAW_LENGTH;
  215. eken_doorbell->maxgaplen = MAX_PULSE_LENGTH*PULSE_DIV;
  216. eken_doorbell->mingaplen = MIN_PULSE_LENGTH*PULSE_DIV;
  217.  
  218. options_add(&eken_doorbell->options, 's', "systemcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, NULL);
  219. options_add(&eken_doorbell->options, 'u', "unitcode", OPTION_HAS_VALUE, DEVICES_ID, JSON_NUMBER, NULL, "^[0-7]$");
  220. options_add(&eken_doorbell->options, 't', "on", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL);
  221. options_add(&eken_doorbell->options, 'f', "off", OPTION_NO_VALUE, DEVICES_STATE, JSON_STRING, NULL, NULL);
  222.  
  223. options_add(&eken_doorbell->options, 0, "readonly", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$");
  224. options_add(&eken_doorbell->options, 0, "confirm", OPTION_HAS_VALUE, GUI_SETTING, JSON_NUMBER, (void *)0, "^[10]{1}$");
  225.  
  226. eken_doorbell->parseCode=&parseCode;
  227. eken_doorbell->createCode=&createCode;
  228. eken_doorbell->printHelp=&printHelp;
  229. eken_doorbell->validate=&validate;
  230. }
  231.  
  232. #if defined(MODULE) && !defined(_WIN32)
  233. void compatibility(struct module_t *module) {
  234. module->name = "eken_doorbell";
  235. module->version = "1.1";
  236. module->reqversion = "6.0";
  237. module->reqcommit = "84";
  238. }
  239.  
  240. void init(void) {
  241. eken_doorbellInit();
  242. }
  243. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement