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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "serialInterface.h"
  25. #ifdef winHelper
  26. #include "winSerial.c"
  27. #else
  28. #include "unixSerial.c"
  29. #endif
  30. JNIEXPORT jstring JNICALL Java_HelperUtility_getThePorts(JNIEnv *env, jclass class, jstring s) {
  31. jboolean tmp;
  32. char **ports = getSerialPorts((*env)->GetStringUTFChars(env, s, &tmp));
  33. char *string = NULL;
  34. int length = 0, leng2 = 0, lengthabs = 0;
  35. // printf("JNI: Got serial ports...\n");
  36. // Count how much memory we need for string of all ports, with \n in between
  37. while (ports[length] != NULL) {
  38. // printf("JNI: Starting count... (%d at %p)\n", length, (void *)ports[length]);
  39. while (ports[length][leng2] != '\0') {
  40. leng2++;
  41. }
  42. // printf("JNI: Counted %s\n", ports[length]);
  43. lengthabs += leng2;
  44. leng2 = 0;
  45. length++;
  46. }
  47. length += lengthabs;
  48. // printf("JNI: Counted serial ports...\n");
  49. string = (char *)malloc((length + 1) * sizeof(char));
  50. if (string == NULL) {
  51. printf("JNI: Not enough memory!\n");
  52. return (*env)->NewStringUTF(env, NULL);
  53. }
  54. length = 0;
  55. lengthabs = 0;
  56. while (ports[length] != NULL) {
  57. leng2 = 0;
  58. while (ports[length][leng2] != '\0') {
  59. string[lengthabs++] = ports[length][leng2++];
  60. }
  61. string[lengthabs++] = '\n';
  62. length++;
  63. }
  64. string[lengthabs] = '\0';
  65. // printf("JNI: %s\n", string);
  66. jstring ret = (*env)->NewStringUTF(env, string);
  67. return ret;
  68. }
  69. JNIEXPORT jshortArray JNICALL Java_HelperUtility_readDataNative(JNIEnv *env, jclass class, jint length) {
  70. jshortArray arr = (*env)->NewShortArray(env, length);
  71. int toBeRead = 0, read, i, error = 0;
  72. char *data = (char *)malloc(length * sizeof(char));
  73. jshort *data2 = (jshort *)malloc(length * sizeof(jshort));
  74. while (length > 0) {
  75. read = serialRead(data + toBeRead, length);
  76. if (read == -1) {
  77. error++;
  78. if (error > 10) {
  79. return (*env)->NewShortArray(env, 0);
  80. }
  81. } else {
  82. toBeRead += read;
  83. length -= read;
  84. }
  85. }
  86. for (i = 0; i < (*env)->GetArrayLength(env, arr); i++) {
  87. data2[i] = data[i];
  88. }
  89. (*env)->SetShortArrayRegion(env, arr, 0, (*env)->GetArrayLength(env, arr), data2);
  90. return arr;
  91. }
  92. JNIEXPORT jboolean JNICALL Java_HelperUtility_writeDataNative(JNIEnv *env, jclass class, jshortArray data, jint length) {
  93. int toWrite = length, written = 0, now, i, error = 0;
  94. char *dat = (char *)malloc(length * sizeof(char));
  95. jshort *dat2 = (jshort *)malloc(length * sizeof(jshort));
  96. // Get unwritten data from argument, place in dat2
  97. // move from dat2 into dat1
  98. // write dat1
  99. // repeat
  100. while (toWrite > 0) {
  101. (*env)->GetShortArrayRegion(env, data, written, length, dat2); // Unwritten part of data in dat2
  102. for (i = 0; i < (length - written); i++) {
  103. dat[i] = dat2[i];
  104. }
  105. now = serialWrite(dat, toWrite);
  106. if (now == -1) {
  107. error++;
  108. if (error > 10) {
  109. return JNI_FALSE;
  110. }
  111. } else {
  112. written += now;
  113. toWrite -= now;
  114. }
  115. }
  116. return JNI_TRUE;
  117. }
  118. JNIEXPORT void JNICALL Java_HelperUtility_closePortNative(JNIEnv * env, jclass class) {
  119. serialClose();
  120. }
  121. JNIEXPORT jboolean JNICALL Java_HelperUtility_openPortNative(JNIEnv *env, jclass class, jstring name) {
  122. jboolean isCopy;
  123. const char *path = (*env)->GetStringUTFChars(env, name, &isCopy);
  124. int ret = serialOpen((char *)path);
  125. if (ret == 0) {
  126. return JNI_TRUE;
  127. } else {
  128. return JNI_FALSE;
  129. }
  130. }