my prototype Programeable Power Supply from Genesys (TM)

Committer:
irsanjul
Date:
Tue Aug 01 08:09:29 2017 +0000
Revision:
1:a8e632941a27
Parent:
0:f71992296c34
my editing. sorry for developper if it ilegal. call me if you want i delete it. thanks

Who changed what in which revision?

UserRevisionLine numberNew contents of line
irsanjul 0:f71992296c34 1 #include "psu.h"
irsanjul 0:f71992296c34 2 #include <sstream>
irsanjul 0:f71992296c34 3
irsanjul 0:f71992296c34 4 #define OK 0
irsanjul 0:f71992296c34 5 #define Vmax 60 // maximun voltage to 60 Volt DC
irsanjul 1:a8e632941a27 6 #define Imax 40 // maximun current to 5 Ampere
irsanjul 0:f71992296c34 7
irsanjul 0:f71992296c34 8 #define MAX_SIZE 128 //!< max expected messages
irsanjul 1:a8e632941a27 9 //! check for timeout
irsanjul 1:a8e632941a27 10 #define TIMEOUT(t, ms) ((ms != TIMEOUT_BLOCKING) && (ms < t.read_ms()))
irsanjul 1:a8e632941a27 11
irsanjul 0:f71992296c34 12 PSUSer::PSUSer(PinName Tx, PinName Rx, unsigned int baudrate) : PSU_ser(Tx, Rx)
irsanjul 0:f71992296c34 13 {
irsanjul 0:f71992296c34 14 PSU_ser.baud(baudrate);
irsanjul 0:f71992296c34 15 PSU_ser.format(8,SerialBase::None,1);
irsanjul 1:a8e632941a27 16 //PSU_ser.attach(this, &PSUSer::getline, Serial::RxIrq);
irsanjul 0:f71992296c34 17 }
irsanjul 0:f71992296c34 18
irsanjul 0:f71992296c34 19 PSUSer::~PSUSer()
irsanjul 0:f71992296c34 20 {
irsanjul 0:f71992296c34 21
irsanjul 0:f71992296c34 22 }
irsanjul 0:f71992296c34 23
irsanjul 1:a8e632941a27 24 void PSUSer::sendFormated(const char* format)
irsanjul 0:f71992296c34 25 {
irsanjul 0:f71992296c34 26 string frtm;
irsanjul 0:f71992296c34 27 frtm += format;
irsanjul 1:a8e632941a27 28 //dbg.printf("\r\nsend: format\t%s",frtm.c_str());
irsanjul 0:f71992296c34 29 _send(frtm.c_str(), frtm.size());
irsanjul 0:f71992296c34 30 }
irsanjul 0:f71992296c34 31
irsanjul 1:a8e632941a27 32 void PSUSer::getline(char* buffer)
irsanjul 0:f71992296c34 33 {
irsanjul 0:f71992296c34 34 string result = _getline();
irsanjul 1:a8e632941a27 35 strcpy(buffer,result.c_str());
irsanjul 1:a8e632941a27 36 }
irsanjul 1:a8e632941a27 37
irsanjul 1:a8e632941a27 38 int PSUSer::waitResp(_CALLBACKPTR cb, void *param, int timeout_ms)
irsanjul 1:a8e632941a27 39 {
irsanjul 1:a8e632941a27 40 char buf[MAX_SIZE + 64 /* add some more space for framing */];
irsanjul 1:a8e632941a27 41 Timer timer;
irsanjul 1:a8e632941a27 42 timer.start();
irsanjul 1:a8e632941a27 43 do {
irsanjul 1:a8e632941a27 44 getline(buf);
irsanjul 1:a8e632941a27 45 //dbg.printf("\r\nread:\tbuf\t%s\r\n",buf);
irsanjul 1:a8e632941a27 46 if(cb)
irsanjul 1:a8e632941a27 47 {
irsanjul 1:a8e632941a27 48 int len = strlen(buf);
irsanjul 1:a8e632941a27 49 int ret = cb(buf, len, param);
irsanjul 1:a8e632941a27 50 if(WAIT != ret)
irsanjul 1:a8e632941a27 51 return ret;
irsanjul 1:a8e632941a27 52 }
irsanjul 1:a8e632941a27 53
irsanjul 1:a8e632941a27 54 if(strstr(buf, "OK"))
irsanjul 1:a8e632941a27 55 return RESP_OK;
irsanjul 1:a8e632941a27 56
irsanjul 1:a8e632941a27 57 wait_ms(10);
irsanjul 1:a8e632941a27 58 }
irsanjul 1:a8e632941a27 59 while(!TIMEOUT(timer, timeout_ms));
irsanjul 1:a8e632941a27 60
irsanjul 1:a8e632941a27 61 return WAIT;
irsanjul 0:f71992296c34 62 }
irsanjul 0:f71992296c34 63
irsanjul 0:f71992296c34 64 bool PSUSer::init(const char *n)
irsanjul 0:f71992296c34 65 {
irsanjul 0:f71992296c34 66 ok = false;
irsanjul 0:f71992296c34 67 char snd[10];
irsanjul 0:f71992296c34 68 sprintf(snd,"ADR %s\r",n);
irsanjul 0:f71992296c34 69 sendFormated("RST\r");
irsanjul 0:f71992296c34 70 wait_ms(100);
irsanjul 0:f71992296c34 71 sendFormated(snd);
irsanjul 1:a8e632941a27 72 ok = (RESP_OK == waitResp(NULL,NULL,2000));
irsanjul 0:f71992296c34 73 return ok;
irsanjul 0:f71992296c34 74 }
irsanjul 0:f71992296c34 75
irsanjul 1:a8e632941a27 76 bool PSUSer::sendPV(float PV)
irsanjul 0:f71992296c34 77 {
irsanjul 0:f71992296c34 78 ok = false;
irsanjul 0:f71992296c34 79 if(PV > Vmax)
irsanjul 0:f71992296c34 80 PV = Vmax;
irsanjul 0:f71992296c34 81 char snd[10];
irsanjul 0:f71992296c34 82 sprintf(snd,"PV %.2f\r",PV);
irsanjul 0:f71992296c34 83 sendFormated(snd);
irsanjul 1:a8e632941a27 84 ok = (RESP_OK == waitResp(NULL,NULL,1000));
irsanjul 0:f71992296c34 85 return ok;
irsanjul 0:f71992296c34 86 }
irsanjul 0:f71992296c34 87
irsanjul 1:a8e632941a27 88 bool PSUSer::sendPC(float PC)
irsanjul 0:f71992296c34 89 {
irsanjul 0:f71992296c34 90 ok = false;
irsanjul 0:f71992296c34 91 if(PC > Imax)
irsanjul 0:f71992296c34 92 PC = Imax;
irsanjul 0:f71992296c34 93 char snd[10];
irsanjul 0:f71992296c34 94 sprintf(snd,"PC %.2f\r",PC);
irsanjul 0:f71992296c34 95 sendFormated(snd);
irsanjul 1:a8e632941a27 96 ok = (RESP_OK == waitResp(NULL,NULL,1000));
irsanjul 0:f71992296c34 97 return ok;
irsanjul 0:f71992296c34 98 }
irsanjul 0:f71992296c34 99
irsanjul 1:a8e632941a27 100 int PSUSer::_cbVolt(const char* buf, int len, VParam *param)
irsanjul 1:a8e632941a27 101 {
irsanjul 1:a8e632941a27 102 if(param)
irsanjul 1:a8e632941a27 103 {
irsanjul 1:a8e632941a27 104 if (sscanf(buf, " %f",param->volt)==1)
irsanjul 1:a8e632941a27 105 return RESP_OK;
irsanjul 1:a8e632941a27 106 else return WAIT;
irsanjul 1:a8e632941a27 107 }
irsanjul 1:a8e632941a27 108 else
irsanjul 1:a8e632941a27 109 return WAIT;
irsanjul 1:a8e632941a27 110 }
irsanjul 1:a8e632941a27 111
irsanjul 1:a8e632941a27 112 int PSUSer::_cbAmpe(const char* buf, int len, AParam *param)
irsanjul 0:f71992296c34 113 {
irsanjul 1:a8e632941a27 114 if(param)
irsanjul 1:a8e632941a27 115 {
irsanjul 1:a8e632941a27 116 if (sscanf(buf, " %f",param->ampere)==1)
irsanjul 1:a8e632941a27 117 return RESP_OK;
irsanjul 1:a8e632941a27 118 else return WAIT;
irsanjul 1:a8e632941a27 119 }
irsanjul 1:a8e632941a27 120 else
irsanjul 1:a8e632941a27 121 return WAIT;
irsanjul 1:a8e632941a27 122 }
irsanjul 1:a8e632941a27 123
irsanjul 1:a8e632941a27 124 bool PSUSer::readPV(float *val)
irsanjul 1:a8e632941a27 125 {
irsanjul 1:a8e632941a27 126 ok = false;
irsanjul 1:a8e632941a27 127 VParam param;
irsanjul 1:a8e632941a27 128 param.volt = val;
irsanjul 1:a8e632941a27 129 sendFormated("PV?\r");
irsanjul 1:a8e632941a27 130 ok = (RESP_OK == waitResp(_cbVolt,&param));
irsanjul 0:f71992296c34 131 return ok;
irsanjul 0:f71992296c34 132 }
irsanjul 0:f71992296c34 133
irsanjul 1:a8e632941a27 134 bool PSUSer::readPC(float *val)
irsanjul 0:f71992296c34 135 {
irsanjul 0:f71992296c34 136 ok = false;
irsanjul 1:a8e632941a27 137 AParam param;
irsanjul 1:a8e632941a27 138 param.ampere = val;
irsanjul 1:a8e632941a27 139 sendFormated("PC?\r");
irsanjul 1:a8e632941a27 140 ok = (RESP_OK == waitResp(_cbAmpe,&param));
irsanjul 0:f71992296c34 141 return ok;
irsanjul 0:f71992296c34 142 }
irsanjul 0:f71992296c34 143
irsanjul 1:a8e632941a27 144 bool PSUSer::readMV(float *val)
irsanjul 0:f71992296c34 145 {
irsanjul 1:a8e632941a27 146 purge();
irsanjul 1:a8e632941a27 147 ok = false;
irsanjul 1:a8e632941a27 148 VParam param;
irsanjul 1:a8e632941a27 149 param.volt = val;
irsanjul 1:a8e632941a27 150 sendFormated("MV?\r");
irsanjul 1:a8e632941a27 151 ok = (RESP_OK == waitResp(_cbVolt,&param));
irsanjul 1:a8e632941a27 152 return ok;
irsanjul 1:a8e632941a27 153 }
irsanjul 1:a8e632941a27 154
irsanjul 1:a8e632941a27 155 bool PSUSer::readMC(float *val)
irsanjul 1:a8e632941a27 156 {
irsanjul 1:a8e632941a27 157 purge();
irsanjul 1:a8e632941a27 158 ok = false;
irsanjul 1:a8e632941a27 159 AParam param;
irsanjul 1:a8e632941a27 160 param.ampere = val;
irsanjul 0:f71992296c34 161 sendFormated("MC?\r");
irsanjul 1:a8e632941a27 162 ok = (RESP_OK == waitResp(_cbAmpe,&param));
irsanjul 0:f71992296c34 163 return ok;
irsanjul 0:f71992296c34 164 }
irsanjul 0:f71992296c34 165
irsanjul 0:f71992296c34 166 bool PSUSer::power(unsigned int pwr)
irsanjul 0:f71992296c34 167 {
irsanjul 0:f71992296c34 168 ok = false;
irsanjul 0:f71992296c34 169 if(pwr > 1)
irsanjul 0:f71992296c34 170 pwr = 1;
irsanjul 0:f71992296c34 171 char snd[10];
irsanjul 0:f71992296c34 172 sprintf(snd,"OUT %d\r",pwr);
irsanjul 0:f71992296c34 173 sendFormated(snd);
irsanjul 1:a8e632941a27 174 ok = (RESP_OK == waitResp(NULL,NULL,1000));
irsanjul 0:f71992296c34 175 return ok;
irsanjul 0:f71992296c34 176 }
irsanjul 0:f71992296c34 177
irsanjul 0:f71992296c34 178 void PSUSer::_send(const void* buf, int len)
irsanjul 0:f71992296c34 179 {
irsanjul 0:f71992296c34 180 const char * kirim = (const char*)buf;
irsanjul 0:f71992296c34 181 for(int i=0; i<len; i++)
irsanjul 0:f71992296c34 182 {
irsanjul 0:f71992296c34 183 PSU_ser.putc(kirim[i]);
irsanjul 0:f71992296c34 184 }
irsanjul 0:f71992296c34 185 }
irsanjul 0:f71992296c34 186
irsanjul 0:f71992296c34 187 string PSUSer::_getline()
irsanjul 0:f71992296c34 188 {
irsanjul 0:f71992296c34 189 string result;
irsanjul 0:f71992296c34 190 while(PSU_ser.readable())
irsanjul 0:f71992296c34 191 result += PSU_ser.getc();
irsanjul 0:f71992296c34 192 return result;
irsanjul 0:f71992296c34 193 }
irsanjul 0:f71992296c34 194
irsanjul 0:f71992296c34 195 bool PSUSer::readable()
irsanjul 0:f71992296c34 196 {
irsanjul 0:f71992296c34 197 return PSU_ser.readable();
irsanjul 0:f71992296c34 198 }
irsanjul 0:f71992296c34 199
irsanjul 0:f71992296c34 200 int PSUSer::get()
irsanjul 0:f71992296c34 201 {
irsanjul 0:f71992296c34 202 if(!PSU_ser.readable())
irsanjul 0:f71992296c34 203 return EOF;
irsanjul 0:f71992296c34 204 return PSU_ser.getc();
irsanjul 0:f71992296c34 205 }