Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

serialHelper.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * serialHelper.c
  3. *
  4. * Copyright 2012 Thomas Buck <xythobuz@me.com>
  5. *
  6. * This file is part of LED-Cube.
  7. *
  8. * LED-Cube is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * LED-Cube is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <jni.h>
  24. #include <time.h>
  25. #include "serialInterface.h"
  26. #ifdef winHelper
  27. #include "winSerial.c"
  28. #else
  29. #include "unixSerial.c"
  30. #endif
  31. JNIEXPORT jstring JNICALL Java_HelperUtility_getThePorts(JNIEnv *env, jclass class, jstring s) {
  32. jboolean tmp;
  33. char **ports = getSerialPorts((*env)->GetStringUTFChars(env, s, &tmp));
  34. char *string = NULL;
  35. int length = 0, leng2 = 0, lengthabs = 0;
  36. // printf("JNI: Got serial ports...\n");
  37. // Count how much memory we need for string of all ports, with \n in between
  38. while (ports[length] != NULL) {
  39. // printf("JNI: Starting count... (%d at %p)\n", length, (void *)ports[length]);
  40. while (ports[length][leng2] != '\0') {
  41. leng2++;
  42. }
  43. // printf("JNI: Counted %s\n", ports[length]);
  44. lengthabs += leng2;
  45. leng2 = 0;
  46. length++;
  47. }
  48. length += lengthabs;
  49. // printf("JNI: Counted serial ports...\n");
  50. string = (char *)malloc((length + 1) * sizeof(char));
  51. if (string == NULL) {
  52. printf("JNI: Not enough memory!\n");
  53. return (*env)->NewStringUTF(env, NULL);
  54. }
  55. length = 0;
  56. lengthabs = 0;
  57. while (ports[length] != NULL) {
  58. leng2 = 0;
  59. while (ports[length][leng2] != '\0') {
  60. string[lengthabs++] = ports[length][leng2++];
  61. }
  62. string[lengthabs++] = '\n';
  63. length++;
  64. }
  65. string[lengthabs] = '\0';
  66. // printf("JNI: %s\n", string);
  67. jstring ret = (*env)->NewStringUTF(env, string);
  68. return ret;
  69. }
  70. JNIEXPORT jshortArray JNICALL Java_HelperUtility_readDataNative(JNIEnv *env, jclass class, jint length) {
  71. jshortArray arr = (*env)->NewShortArray(env, length);
  72. int toBeRead = 0, read, i, error = 0;
  73. unsigned char *data = (char *)malloc(length * sizeof(char));
  74. jshort *data2 = (jshort *)malloc(length * sizeof(jshort));
  75. time_t startTime = time(NULL);
  76. time_t diff = (length / 3000) + 1;
  77. while (length > 0) {
  78. read = serialRead((char *)data + toBeRead, length);
  79. if (read == -1) {
  80. error++;
  81. if (error > 10) {
  82. return (*env)->NewShortArray(env, 0);
  83. }
  84. } else {
  85. toBeRead += read;
  86. length -= read;
  87. }
  88. if (difftime(time(NULL), startTime) > diff) {
  89. return NULL;
  90. }
  91. }
  92. for (i = 0; i < (*env)->GetArrayLength(env, arr); i++) {
  93. data2[i] = data[i];
  94. }
  95. (*env)->SetShortArrayRegion(env, arr, 0, (*env)->GetArrayLength(env, arr), data2);
  96. return arr;
  97. }
  98. JNIEXPORT jboolean JNICALL Java_HelperUtility_writeDataNative(JNIEnv *env, jclass class, jshortArray data, jint length) {
  99. int toWrite = length, written = 0, now, i, error = 0;
  100. char *dat = (char *)malloc(length * sizeof(char));
  101. jshort *dat2 = (jshort *)malloc(length * sizeof(jshort));
  102. // Get unwritten data from argument, place in dat2
  103. // move from dat2 into dat1
  104. // write dat1
  105. // repeat
  106. while (toWrite > 0) {
  107. (*env)->GetShortArrayRegion(env, data, written, length, dat2); // Unwritten part of data in dat2
  108. for (i = 0; i < (length - written); i++) {
  109. dat[i] = dat2[i];
  110. }
  111. now = serialWrite(dat, toWrite);
  112. if (now == -1) {
  113. error++;
  114. if (error > 10) {
  115. return JNI_FALSE;
  116. }
  117. } else {
  118. written += now;
  119. toWrite -= now;
  120. }
  121. }
  122. return JNI_TRUE;
  123. }
  124. JNIEXPORT void JNICALL Java_HelperUtility_closePortNative(JNIEnv * env, jclass class) {
  125. serialClose();
  126. }
  127. JNIEXPORT jboolean JNICALL Java_HelperUtility_openPortNative(JNIEnv *env, jclass class, jstring name) {
  128. jboolean isCopy;
  129. const char *path = (*env)->GetStringUTFChars(env, name, &isCopy);
  130. int ret = serialOpen((char *)path);
  131. if (ret == 0) {
  132. return JNI_TRUE;
  133. } else {
  134. return JNI_FALSE;
  135. }
  136. }