Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface MbedJSONValue SDFileSystem mbed-rtos mbed
Fork of Gate_Aidik_FRDM_v2 by
Diff: main.cpp
- Revision:
- 1:021273f6b9f4
- Parent:
- 0:fbb75122015f
--- a/main.cpp Mon Aug 22 09:27:26 2016 +0000
+++ b/main.cpp Tue Feb 28 06:55:23 2017 +0000
@@ -1,91 +1,61 @@
+/*
+ FRDM v4 menggunakan board v2 yang dimodif lednya.
+ Led ga pake digital inout, diganti jadi digitalout.
+*/
+
#include "mbed.h"
#include "EthernetInterface.h"
-#include "SDFileSystem.h"
-#include <string>
+//#include "SDFileSystem.h"
+#include "MbedJSONValue.h"
+#include <sstream>
+#include <iostream>
+#include <fstream>
#define IP_CONF_PORT 50004
#define ijo 0
#define merah 1
-UDPSocket sock, ip_conf, trx_ser; //gen_con,
-Endpoint multi, server; //,multi_data;
+UDPSocket sock, ip_conf, trx_ser;
+Endpoint multi, server;
+
+//SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
-SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
-
-DigitalOut Stdby_R(LED2), Sopir_R(LED3), Res_R(LED1), MT_R(LED4), Gate(PTB9), Sleep(PTA1), Reset(PTB23, 1);
-DigitalInOut Std_G(PTC5), Std_R(PTC7), Spr_G(PTC0), Spr_R(PTC9), Mt_G(PTC8), Mt_R(PTC1), Pow(PTB19), iBtn(PTB18);
+DigitalOut Stdby_R(LED2), Sopir_R(LED3), Res_R(LED1), MT_R(LED4);
+DigitalOut Std_G(PTC5), Std_R(PTC7), Spr_G(PTC0), Spr_R(PTC9), Mt_G(PTC8), Mt_R(PTC1), Pow(PTB19);
+DigitalOut Gate(PTB9), Sleep(PTA1), Reset(PTB23, 1);
+DigitalInOut iBtn(PTB18);
DigitalIn print_er(PTC2), print_done(PTB2);
Serial dbg(USBTX, USBRX);
-Serial pc(PTC17, PTC16);
+Serial printer(PTC17, PTC16);
-char IP[16], SUBNET[16], GATEWAY[16], MCAST[16];
+char IP[16], SUBNET[16], GATEWAY[16], MCAST[16], IDX[4];
char PortListen[6], PortSend[6], PortHeartB[6];
-char IDX[4];
-char Baud[6];
int BAUD = 19200;
int width, TICK = 0;
unsigned char ID[8], idx, idy, id_addr = 0x33, ix, er_tmp1 = 0, er_tmp2 = 0,
- sprmt = 0; // state = 1,
-char text[26], ch, rx_buff[6], tx_buff[7], //86
- hex[17] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
- 'C', 'D', 'E', 'F' }; //,
-
-/*Contoh format data
- <02>GI01S0123456789ABCDEF00<03>
- <02>GI01M0123456789ABCDEF00<03>
- <02> ->start
- GI -> Lokasi <G = Gate> <I = Input> <O = Output>
- 01 -> ID
- S -> Supir <S = Supir> <M = MT>
- 0123456789ABCDEF -> iButton data
- 00 -> reserved
- <03> -> stop
-
- format heartbeat
- <02>GI0100000000<03>
-
- format validasi
- <02>V01<03>
- 1, idle
- 2, stdby/ idle
- 3, merah/sopir
- 4, bitu/ MT
- 5, buka gate
-
- format ipconfig
- <02> 192.168.1.116, 255.255.255.0, 192.168.1.1, 192.168.1.10, 50000, 50001, 50003, GI01$<03>
- #192.168.1.116, 255.255.255.0, 192.168.1.1, 192.168.1.10, 50000, 50001, 50003, GI01$
- 2, stdby
- 3, merah/sopir
- 4, biru MT
- */
+ sprmt = 0;
+char text[26], ch, rx_buff[6], tx_buff[7],
+ _hex[17] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
+ 'C', 'D', 'E', 'F' };
void Standby(bool warna, bool nyala) {
switch (nyala) {
case 0:
if (warna == ijo) {
Stdby_R = 0; // led 2
- Std_G.output();
Std_G.write(0);
- Std_G.input();
- Std_G.mode(PullUp);
} else if (warna == merah) {
- Std_R.output();
Std_R.write(0);
- Std_R.input();
- Std_R.mode(PullUp);
}
break;
case 1:
if (warna == ijo) {
Stdby_R = 1;
- Std_G.output();
Std_G.write(1);
} else if (warna == merah) {
- Std_R.output();
Std_R.write(1);
}
break;
@@ -97,24 +67,16 @@
case 0:
if (warna == ijo) {
Sopir_R = 0; // led 3
- Spr_G.output();
Spr_G.write(0);
- Spr_G.input();
- Spr_G.mode(PullUp);
} else if (warna == merah) {
- Spr_R.output();
Spr_R.write(0);
- Spr_R.input();
- Spr_R.mode(PullUp);
}
break;
case 1:
if (warna == ijo) {
Sopir_R = 1;
- Spr_G.output();
Spr_G.write(1);
} else if (warna == merah) {
- Spr_R.output();
Spr_R.write(1);
}
break;
@@ -126,101 +88,106 @@
case 0:
if (warna == ijo) {
MT_R = 0; // led 4
- Mt_G.output();
Mt_G.write(0);
- Mt_G.input();
- Mt_G.mode(PullUp);
} else if (warna == merah) {
- Mt_R.output();
Mt_R.write(0);
- Mt_R.input();
- Mt_R.mode(PullUp);
}
break;
case 1:
if (warna == ijo) {
MT_R = 1;
- Mt_G.output();
Mt_G.write(1);
} else if (warna == merah) {
- Mt_R.output();
Mt_R.write(1);
}
break;
}
}
-void mbed_config_init() {
- char chr[256];
- int ilx = 0;
- unsigned char n = 0, x = 0;
- FILE *fp = fopen("/sd/IPCONF.TXT", "r");
- if (fp == NULL)
- dbg.printf("Error opening file");
- else {
- //dbg.printf("File Exist!\r\n");
- do {
- ilx++;
- chr[ilx] = fgetc(fp);
- //dbg.printf("%c",chr[ilx]);
- } while (chr[ilx] != '$');
- fclose(fp);
+
+
+string open_file(const char *location)
+{
+ string rtn = "\0";
+
+// dbg.printf("OPEN FILE %s\r\n", location);
+ ifstream stream(location, std::ifstream::binary);
+
+ if ( !stream.is_open() ) {
+ dbg.printf("File Opening Error\n\r");
+ stream.close();
+ }
+ else
+ {
+ stream.seekg(0,stream.end);
+ int len = stream.tellg();
+ stream.seekg(0,stream.beg);
+
+ char *_stream = new char [len];
+
+ stream.read(_stream,len);
+ _stream[len] = '\0';
+
+ rtn += _stream;
+ stream.close();
+ delete [] _stream;
}
- ilx = 1;
- do {
- if (chr[ilx] == '#') {
- ilx = ilx + 1;
- n = 1;
- dbg.printf("Start file\r\n");
- } else if (chr[ilx] == ',') {
- n++;
- x = 0;
- ilx = ilx + 2;
+
+ return rtn;
+}
+
+bool mbed_config_init()
+{
+ dbg.printf("Masuk mbed config init\r\n");
+
+ MbedJSONValue config_json;
+ string dataSet = open_file("/sd/IPCONF.TXT");
+ dbg.printf("isi file :\r\n%s\r\n", dataSet.c_str());
+
+ if(dataSet.size() > 1)
+ {
+ string err;
+ parse(config_json, dataSet.c_str(), dataSet.c_str() + strlen(dataSet.c_str()), &err);
+ if(err.size() > 0)
+ {
+ dbg.printf("res error? %s\r\n", err.c_str());
+ return false;
+ }
+ else
+ {
+ const char *ip_temp = config_json["IP"].get<string>().c_str();
+ const char *sub_temp = config_json["SUBNET"].get<string>().c_str();
+ const char *gate_temp = config_json["GATEWAY"].get<string>().c_str();
+ const char *mcast_temp = config_json["MCAST"].get<string>().c_str();
+ const char *idx_temp = config_json["IDX"].get<string>().c_str();
+// PortListen = config_json["PortListen"].get<int>();
+// PortSend = config_json["PortSend"].get<int>();
+// PortHeartB = config_json["PortHeartBit"].get<int>();
+ BAUD = config_json["BAUD"].get<int>();
+
+ sprintf(IP,"%s", ip_temp);
+ sprintf(SUBNET, "%s", sub_temp);
+ sprintf(GATEWAY, "%s", gate_temp);
+ sprintf(MCAST, "%s", mcast_temp);
+ sprintf(IDX, "%s", idx_temp);
+
+ dbg.printf("IP Device = %s\r\n", IP);
+ dbg.printf("Subnet = %s\r\n", SUBNET);
+ dbg.printf("Gateway = %s\r\n", GATEWAY);
+ dbg.printf("Multicast IP = %s\r\n", MCAST);
+// dbg.printf("Server to mBed = %d\r\n", PortListen);
+// dbg.printf("mBed to Multicast = %d\r\n", PortSend);
+// dbg.printf("HeartBeat Port = %d\r\n", PortHeartB);
+ dbg.printf("device IDX = %s\r\n", IDX);
+ dbg.printf("Serial Baudrate = %d\r\n", BAUD);
+
+ return true;
}
- switch (n) {
- case 0:
- break;
- case 1:
- IP[x] = chr[ilx];
- break;
- case 2:
- SUBNET[x] = chr[ilx];
- break;
- case 3:
- GATEWAY[x] = chr[ilx];
- break;
- case 4:
- MCAST[x] = chr[ilx];
- break;
- case 5:
- PortListen[x] = chr[ilx];
- break;
- case 6:
- PortSend[x] = chr[ilx];
- break;
- case 7:
- PortHeartB[x] = chr[ilx];
- break;
- case 8:
- IDX[x] = chr[ilx];
- break;
- case 9:
- Baud[x] = chr[ilx];
- break;
- }
- x++;
- ilx++;
- } while (chr[ilx] != '$');
-
- dbg.printf("IP Device = %s\r\n", IP);
- dbg.printf("Subnet = %s\r\n", SUBNET);
- dbg.printf("Gateway = %s\r\n", GATEWAY);
- dbg.printf("Multicast IP = %s\r\n", MCAST);
- dbg.printf("Server to mBed = %s\r\n", PortListen);
- dbg.printf("mBed to Multicast = %s\r\n", PortSend);
- dbg.printf("HeartBeat Port = %s\r\n", PortHeartB);
- dbg.printf("device IDX = %s\r\n", IDX);
- dbg.printf("Serial Baudrate = %s\r\n", Baud);
- BAUD = atoi(Baud);
+ }
+ else
+ {
+ dbg.printf("Config is not found\r\n");
+ return false;
+ }
}
unsigned char reset(void) {
@@ -288,11 +255,11 @@
}
for (idx = 0; idx < 8; idx++) {
- text[((7 - idx) * 2) + 7] = hex[ID[idx] & 0x0f];
- text[((7 - idx) * 2) + 6] = hex[(ID[idx] >> 4) & 0x0f];
+ text[((7 - idx) * 2) + 7] = _hex[ID[idx] & 0x0f];
+ text[((7 - idx) * 2) + 6] = _hex[(ID[idx] >> 4) & 0x0f];
}
- std::string str;
- std::size_t found1;
+ string str;
+ size_t found1;
str += text;
found1 = str.find("FFFFFFFFFF");
@@ -415,19 +382,15 @@
return 0;
}
-void write_config(char buff[90]){
- FILE *fp = fopen("/sd/IPCONF.TXT", "w");
- fprintf(fp, buff);
- fclose(fp);
-}
-
int set_IP() {
char ptr_data;
//server.set_address(SERVER,ER_SEND_PORT);
//char print_rx_buff[128];
- char conf_buff[1024] = { 0 }, print_init[20] = { 0x1b, 0x40, 0x1b, 0x54,
- 0x1b/*,0x57*/, 0x00, 0x1b, 0x77, 0x00, 0x12, 0x1b, 0x43, 0x00, 0x06,
- 0x1b, 0x6c, 0x01, 0x12 };
+ char conf_buff[1024] = { 0 };
+// char print_init[20] = { 0x1b, 0x40, 0x1b, 0x54,
+// 0x1b, 0x57, 0x00, 0x1b, 0x77, 0x00, 0x12, 0x1b, 0x43, 0x00, 0x06,
+// 0x1b, 0x6c, 0x01, 0x12 }
+//
int n = ip_conf.receiveFrom(server, conf_buff, sizeof(conf_buff));
if (n >= 0) {
//printf("Packet from \"%s\": %s\n", server.get_address(), conf_buff);
@@ -441,14 +404,9 @@
int stop = 0;
while(stop != 1)
{
- Spr_G.input();
- Spr_G.mode(PullUp);
-
- Mt_G.input();
- Mt_G.mode(PullUp);
-
- Std_G.input();
- Std_G.mode(PullUp);
+ Spr_G = 0;
+ Mt_G = 0;
+ Std_G = 0;
ip_conf.receiveFrom(server, conf_buff, sizeof(conf_buff));
@@ -478,11 +436,9 @@
Sleep = 0;
//sock.bind(atoi(PortListen));
}
- } else if (conf_buff[0] == '#') {
- write_config(conf_buff);
} else if (conf_buff[0] == 0x0f) {
- dbg.printf("%s", print_init);
- pc.printf("%s", conf_buff);
+// printer.printf("%s", print_init);
+ printer.printf("%s", conf_buff);
ptr_data = *strchr(conf_buff, 0x1d);
if (ptr_data != NULL) {
tx_buff[4] = 'D';
@@ -517,9 +473,9 @@
void Set_IP_Manual()
{
- sprintf(IP,"192.168.0.14");
+ sprintf(IP,"192.168.1.14");
sprintf(SUBNET, "255.255.255.0");
- sprintf(GATEWAY, "192.168.0.1");
+ sprintf(GATEWAY, "192.168.1.1");
sprintf(MCAST, "224.1.1.8");
sprintf(PortListen, "50007");
sprintf(PortSend, "50001");
@@ -535,17 +491,17 @@
dbg.printf("mBed to Multicast = %s\r\n", PortSend);
dbg.printf("HeartBeat Port = %s\r\n", PortHeartB);
dbg.printf("device IDX = %s\r\n", IDX);
+ dbg.printf("Printer Baud = %d\r\n", BAUD);
}
int main(void) {
- mkdir("/sd/", 0777);
+// mkdir("/sd/", 0777);
- mbed_config_init(); // Digunakan jika setting ip melalui micro sd
+// mbed_config_init(); // Digunakan jika setting ip melalui micro sd
Set_IP_Manual(); // Digunakan jika setting ip secara manual
- dbg.printf("\r\nPrinter Baud = %d\r\n",BAUD);
- pc.baud(BAUD);
- dbg.baud(BAUD);
+ printer.baud(BAUD);
+ dbg.baud(9600);
dbg.printf("System Start \r\n");
EthernetInterface eth;
