spacepaste

  1.  
  2. /*-
  3. * Copyright (C) 1999, 2002 Matt Armstrong
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30. #include "conf.h"
  31. #include <stdio.h>
  32. #include <stddef.h>
  33. #include <ctype.h>
  34. #include <stdlib.h>
  35. #include <assert.h>
  36. #if HAVE_FCNTL_H
  37. #include <fcntl.h>
  38. #endif
  39. #if HAVE_SYS_IOCTL_H
  40. #include <sys/ioctl.h>
  41. #endif
  42. #if HAVE_SYS_TIME_H
  43. #include <sys/time.h>
  44. #endif
  45. #if HAVE_SYS_TYPES_H
  46. #include <sys/types.h>
  47. #endif
  48. #if HAVE_UNISTD_H
  49. #include <unistd.h>
  50. #endif
  51. #if HAVE_TERMIO_H
  52. #include <termio.h>
  53. #endif
  54. #include "cm17a.h"
  55. enum SIGNAL {
  56. RESET = 0,
  57. HIGH = TIOCM_RTS,
  58. LOW = TIOCM_DTR,
  59. STANDBY = (TIOCM_RTS | TIOCM_DTR)
  60. };
  61. /* The house code is the high nibble of the command data. */
  62. unsigned short house_codes[16] = {
  63. 0x6000, /* A */
  64. 0x7000, /* B */
  65. 0x4000, /* C */
  66. 0x5000, /* D */
  67. 0x8000, /* E */
  68. 0x9000, /* F */
  69. 0xA000, /* G */
  70. 0xB000, /* H */
  71. 0xE000, /* I */
  72. 0xF000, /* J */
  73. 0xC000, /* K */
  74. 0xD000, /* L */
  75. 0x0000, /* M */
  76. 0x1000, /* N */
  77. 0x2000, /* O */
  78. 0x3000, /* P */
  79. };
  80. /* The device codes are described by bits 3, 4, 6, and 10. */
  81. #define DEVICE_BITS 0x0458
  82. unsigned short device_codes[16] = {
  83. 0x0000, /* 1 */
  84. 0x0010, /* 2 */
  85. 0x0008, /* 3 */
  86. 0x0018, /* 4 */
  87. 0x0040, /* 5 */
  88. 0x0050, /* 6 */
  89. 0x0048, /* 7 */
  90. 0x0058, /* 8 */
  91. 0x0400, /* 9 */
  92. 0x0410, /* 10 */
  93. 0x0408, /* 11 */
  94. 0x0418, /* 12 */
  95. 0x0440, /* 13 */
  96. 0x0450, /* 14 */
  97. 0x0448, /* 15 */
  98. 0x0458, /* 16 */
  99. };
  100. /* The command codes are described by bits 1, 2, 5, 7, 8, 9, and
  101. 11. */
  102. unsigned short command_codes[] = {
  103. 0x0000, /* CM17A_ON */
  104. 0x0020, /* CM17A_OFF */
  105. 0x0088, /* BRIGHT */
  106. 0x0098, /* CM17A_DIM */
  107. };
  108. #define ELEMENT_COUNT(a) (sizeof(a) / sizeof((a)[0]))
  109. static unsigned short
  110. cm17a_command_word(int house,
  111. int device,
  112. enum CM17A_COMMAND command)
  113. {
  114. unsigned short retval;
  115. assert(house >= 0 && house < ELEMENT_COUNT(house_codes));
  116. assert(device >= 0 && device < ELEMENT_COUNT(device_codes));
  117. assert(command >= 0 && command < ELEMENT_COUNT(command_codes));
  118. switch (command) {
  119. case CM17A_BRIGHTEN:
  120. case CM17A_DIM:
  121. retval = house_codes[house] | command_codes[command];
  122. break;
  123. default:
  124. retval = (house_codes[house] |
  125. device_codes[device] |
  126. command_codes[command]);
  127. break;
  128. }
  129. return retval;
  130. }
  131. static void sleep_us(unsigned int nusecs)
  132. {
  133. struct timeval tval;
  134. tval.tv_sec = nusecs / 1000000;
  135. tval.tv_usec = nusecs % 1000000;
  136. select(0, NULL, NULL, NULL, &tval);
  137. }
  138. static void
  139. delay(void)
  140. {
  141. sleep_us(500);
  142. }
  143. static int
  144. set(int fd, enum SIGNAL signal)
  145. {
  146. int lines = signal;
  147. int current;
  148. /* FIXME: we should just remember this in a static
  149. variable. */
  150. if (ioctl(fd, TIOCMGET, &current) != 0) {
  151. return -1;
  152. }
  153. current &= ~RESET;
  154. current |= signal;
  155. return ioctl(fd, TIOCMSET, &lines);
  156. }
  157. static int write_byte(int fd, unsigned char byte)
  158. {
  159. int mask = 0x80;
  160. do {
  161. enum SIGNAL signal = (byte & mask) ? HIGH : LOW;
  162. if (set(fd, signal) != 0) {
  163. return -1;
  164. }
  165. delay();
  166. if (set(fd, STANDBY) != 0) {
  167. return -1;
  168. }
  169. delay();
  170. mask >>= 1;
  171. } while (mask);
  172. return 0;
  173. }
  174. static int
  175. write_stream(int fd,
  176. const unsigned char* data,
  177. size_t byte_count)
  178. {
  179. int i;
  180. for (i = 0; i < byte_count; i++) {
  181. int ret = write_byte(fd, data[i]);
  182. if (ret) {
  183. return ret;
  184. }
  185. }
  186. return 0;
  187. }
  188. static int
  189. cm17a_standby(int fd)
  190. {
  191. if (set(fd, STANDBY) != 0) {
  192. return -1;
  193. }
  194. delay();
  195. return 0;
  196. }
  197. static int
  198. cm17a_header(int fd)
  199. {
  200. /* Header bits taken straight from the CM17A spec. */
  201. const unsigned char header[] = {
  202. 0xD5, 0xAA
  203. };
  204. return write_stream(fd, header, sizeof(header));
  205. }
  206. static int
  207. footer(int fd)
  208. {
  209. /* Footer bits taken straight from the CM17A spec. */
  210. return write_byte(fd, 0xAD);
  211. }
  212. static int
  213. cm17a_write_command(int fd,
  214. int house,
  215. int device,
  216. enum CM17A_COMMAND command)
  217. {
  218. short command_word;
  219. command_word = cm17a_command_word(house, device, command);
  220. if (cm17a_standby(fd)) {
  221. return -1;
  222. }
  223. /* The cm17a seems to need another delay period after a
  224. standby to settle down after the reset. */
  225. delay();
  226. if (cm17a_header(fd) ||
  227. write_byte(fd, command_word >> 8) ||
  228. write_byte(fd, command_word) ||
  229. footer(fd)) {
  230. return -1;
  231. }
  232. sleep_us(500000);
  233. return 0;
  234. }
  235. void
  236. cm17a_command(int fd,
  237. int house,
  238. int device,
  239. enum CM17A_COMMAND cmd,
  240. int steps)
  241. {
  242. switch (cmd) {
  243. case CM17A_ON:
  244. case CM17A_OFF:
  245. if (cm17a_write_command(fd, house, device, cmd) != 0) {
  246. fprintf(stderr, "Command failed.\n");
  247. }
  248. break;
  249. case CM17A_BRIGHTEN:
  250. case CM17A_DIM:
  251. {
  252. if (steps < 1 || steps > 6) {
  253. fprintf(stderr,
  254. "Invalid steps (%d). Must be [1..6].\n",
  255. steps);
  256. return;
  257. }
  258. /* Turn the device we wish to control on first. If we don't
  259. do this, the dim command gets handled by the last device
  260. that got turned on. The cm17a dim/brighten command doesn't
  261. use a device code. */
  262. if (cm17a_write_command(fd, house, device, CM17A_ON) != 0) {
  263. fprintf(stderr, "Command failed.\n");
  264. }
  265. while (--steps >= 0) {
  266. if (cm17a_write_command(fd, house, device, cmd)) {
  267. fprintf(stderr, "Command failed.\n");
  268. }
  269. }
  270. break;
  271. }
  272. default:
  273. fprintf(stderr, "Invalid flip operation\n");
  274. }
  275. }
  276. /*
  277. Local Variables:
  278. mode:c
  279. c-file-style:"linux"
  280. tab-width:8
  281. End:
  282. */
  283.